View Javadoc

1   /**
2    * @author SrinivasJasti
3    * 
4    */
5   package org.srinivas.siteworks.dbrestserver.webconfig;
6   
7   import javax.servlet.ServletContext;
8   import javax.servlet.ServletRegistration;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  import org.springframework.web.WebApplicationInitializer;
13  import org.springframework.web.context.ContextLoaderListener;
14  import org.springframework.web.context.WebApplicationContext;
15  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
16  import org.springframework.web.servlet.DispatcherServlet;
17  
18  /**
19   * The Class DbServerWebInitializer.
20   */
21  public class DbServerWebInitializer implements WebApplicationInitializer {
22  
23  	private static final Logger logger = LoggerFactory.getLogger(DbServerWebInitializer.class);
24  
25  	/* (non-Javadoc)
26  	 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
27  	 */
28  	@Override
29  	public void onStartup(ServletContext container) {
30  		logger.info("Started to pickup the annotated classes at DbServerWebInitializer");
31  		startServlet(container);
32  	}
33  
34  	/**
35  	 * Start servlet.
36  	 *
37  	 * @param container the container
38  	 */
39  	private void startServlet(final ServletContext container) {
40  		WebApplicationContext dispatcherContext = registerContext(DbServerMvcContextConfiguration.class);
41  		DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
42  		container.addListener(new ContextLoaderListener(dispatcherContext));
43  		container.addListener(new DbServerCamelListener());
44  		ServletRegistration.Dynamic dispatcher;
45  		dispatcher = container.addServlet("dispatcher", dispatcherServlet);
46  		dispatcher.setLoadOnStartup(1);
47  		dispatcher.addMapping("/*");
48  	}
49  
50  	/**
51  	 * Register context.
52  	 *
53  	 * @param annotatedClasses the annotated classes
54  	 * @return the web application context
55  	 */
56  	private WebApplicationContext registerContext(final Class<?>... annotatedClasses) {
57  		logger.info("Using AnnotationConfigWebApplicationContext createContext");
58  		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
59  		context.register(annotatedClasses);
60  		return context;
61  	}
62  
63  }