Skip to content

Instantly share code, notes, and snippets.

@BenDol
Created May 12, 2015 03:05
Show Gist options
  • Save BenDol/d3a73e9d9cb16325b2f5 to your computer and use it in GitHub Desktop.
Save BenDol/d3a73e9d9cb16325b2f5 to your computer and use it in GitHub Desktop.
Spring EntryPoint code
/**
* Module which binds the handlers and configurations.
*/
@Configuration
@Import({
PropertiesModule.class,
HttpModule.class,
WebSocketModule.class,
OptionalModule.class,
ComponentModule.class,
LdapModule.class,
MappingModule.class,
DatabaseModule.class,
HibernateModule.class,
CachingModule.class,
MailModule.class,
ResourceModule.class,
LoggedInInterceptorModule.class
})
public class ServerModule {
@Autowired
private AnnotationSessionFactoryBean sessionFactory;
@Bean(name="viewScripts")
@DependsOn("sessionFactory")
public ViewScripts viewScripts() {
ViewScripts viewScripts = new ViewScripts("nz/co/doltech/actions/server/dbviews.xml");
viewScripts.setSessionFactory(sessionFactory.getObject());
Properties prop = AppProperties.getProperties();
String view2ddl = prop.getProperty(PropertyMap.HIBERNATE_VIEW2DLL);
if (view2ddl.equals("create")) {
viewScripts.executeAll(Type.CREATE);
} else if (view2ddl.equals("update")) {
viewScripts.executeAll(Type.UPDATE);
} else if (view2ddl.equals("drop")) {
viewScripts.executeAll(Type.DROP);
}
return viewScripts;
}
}
public class WebAppInitializer implements WebApplicationInitializer {
private static final String MVC_SERVLET_NAME = "mvcServlet";
private static final String DISPATCH_SERVLET_NAME = "dispatch";
private static final String ATTACHMENT_SERVLET_NAME = "attachmentService";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Setup Custom Configurations
setupCustomListeners(servletContext);
setupCustomServlets(servletContext);
setupCustomFilters(servletContext);
// Setup Spring Configurations
WebApplicationContext rootContext = createWebContext(servletContext);
configureSpring(servletContext, rootContext);
configureSpringMvc(servletContext, rootContext);
}
protected void setupCustomFilters(ServletContext servletContext) {
addFilter(servletContext, new ShallowEtagHeaderFilter(), "etagFilter", "/api/*");
}
protected void setupCustomServlets(ServletContext servletContext) {
addServlet(servletContext, new HttpRequestHandlerServlet(), ATTACHMENT_SERVLET_NAME, "/attachment/*");
}
protected void setupCustomListeners(ServletContext servletContext) {
servletContext.setInitParameter("log4jConfigLocation", "classpath:log4j.properties");
servletContext.setInitParameter("log4jExposeWebAppRoot", "false");
servletContext.addListener(Log4jConfigListener.class);
servletContext.addListener(ClassLoaderLeakPreventor.class);
servletContext.addListener(RequestContextListener.class);
}
protected WebApplicationContext createWebContext(ServletContext servletContext) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebMvcModule.class);
servletContext.addListener(new ContextLoaderListener(context));
return context;
}
protected void configureSpring(ServletContext servletContext, WebApplicationContext rootContext) {
servletContext.setInitParameter("contextClass",
"org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
servletContext.setInitParameter("contextConfigLocation", "nz.co.doltech.actions.server.spring.ServerModule");
addServlet(servletContext, new HttpRequestHandlerServlet(), DISPATCH_SERVLET_NAME, "/dispatch/*");
}
protected void configureSpringMvc(ServletContext servletContext, WebApplicationContext rootContext) {
addServlet(servletContext, new DispatcherServlet(rootContext), MVC_SERVLET_NAME, Rest.ROOT + "/*")
.setLoadOnStartup(1);
}
private static <T extends HttpServlet> ServletRegistration.Dynamic addServlet(
ServletContext servletContext, T servlet, String servletName, String mapping) {
ServletRegistration.Dynamic servletRegistration = servletContext.addServlet(servletName, servlet);
servletRegistration.addMapping(mapping);
return servletRegistration;
}
private static <T extends Filter> FilterRegistration.Dynamic addFilter(
ServletContext servletContext, T filter, String filterName, String mapping) {
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter(filterName, filter);
filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, mapping);
return filterRegistration;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment