public static Server start(JettyProperties properties) throws Throwable { QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setMaxThreads(properties.jettyThreadPoolCount); Server server = new Server(threadPool); ServerConnector http = new ServerConnector(server,new HttpConnectionFactory()); http.setPort(properties.jettyPort); server.addConnector(http); WebAppContext context = new WebAppContext(); // Logging configured per environment: context.addEventListener(new Log4jConfigListener()); context.setInitParameter("log4jConfigLocation", "file:" + properties.log4jConfigPath); context.setInitParameter("log4jExposeWebAppRoot", "false"); context.setDescriptor(new ClassPathResource("src/main/webapp/WEB-INF/web.xml").getURI().toString()); if (properties.customWebappContextPath.isPresent()) { context.setResourceBase(properties.customWebappContextPath.get()); } else { context.setResourceBase(new ClassPathResource("src/main/webapp").getURI().toString()); } context.setContextPath("/"); context.setParentLoaderPriority(true); context.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false"); context.setInitParameter("spring.profiles.active", properties.springProfile); server.setHandler(context); server.start(); return server; }
protected void initLog4j(ServletContext servletContext) { final String log4jConfigLocationParam = "log4jConfigLocation"; final String log4jConfigLocation = servletContext.getInitParameter(log4jConfigLocationParam); if(Strings.isNullOrEmpty(log4jConfigLocation)) { servletContext.setInitParameter(log4jConfigLocationParam, getDefaultLog4jConfigLocation()); } servletContext.setInitParameter("log4jExposeWebAppRoot", "false"); servletContext.addListener(Log4jConfigListener.class); }
protected void addListeners(final ServletContext ctx) { ctx.addListener(new ContextLoaderListener(rootContext)); Log4jConfigListener lcl = new Log4jConfigListener(); ctx.addListener(lcl); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { // Setup Context to Accept Annotated Classes on Input (including plain Spring {@code @Component} // Stereotypes in addition to JSR-330 Compliant Classes using {@code javax.inject} AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); //context.setConfigLocation(APP_CONFIG_LOCATION); context.setConfigLocation(APP_CONFIG_LOCATION); /* * Add a Spring Security Filter using the JEE6 Filter Registration Filter Method from {@code FilterRegistration) that allows filters to be registered * and configured with the specified context */ /* FilterRegistration.Dynamic securityFilter = servletContext.addFilter(ProjectKeyValConsts.SECURITY_FILTER.getKey(), new DelegatingFilterProxy(ProjectKeyValConsts.SECURITY_FILTER.getValue())); securityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, ProjectConsts.BASE_URL_MAPPING_PATTERN.getValue()); // where the filter will be applied */ // Add a Character Encoding Filter that specifies an encoding for mapped requests FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter()); characterEncodingFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, ROOT_CONTEXT); // where the filter will be applied characterEncodingFilter.setInitParameter("encoding", "UTF-8"); characterEncodingFilter.setInitParameter("forceEncoding", Boolean.TRUE.toString()); characterEncodingFilter.setAsyncSupported(true); servletContext.addListener(new ContextLoaderListener(context)); servletContext.setInitParameter("defaultHtmlEscape", Boolean.TRUE.toString()); DispatcherServlet servlet = new DispatcherServlet(); // no explicit configuration reference here: everything is configured in the root container for simplicity servlet.setContextConfigLocation(""); /* TMT From JEE 6 API Docs: * Registers the given servlet instance with this ServletContext under the given servletName. * The registered servlet may be further configured via the returned ServletRegistration object. */ ServletRegistration.Dynamic appServlet = servletContext.addServlet(APP_SERVLET, servlet); appServlet.setLoadOnStartup(1); appServlet.setAsyncSupported(true); Set<String> mappingConflicts = appServlet.addMapping("/"); if (!mappingConflicts.isEmpty()) { throw new IllegalStateException(String.format("The servlet named '%s' cannot be mapped to '/' under Tomcat versions <= 7.0.14", APP_SERVLET)); } // TMT servletContext.addListener(new Log4jConfigListener()); System.out.println("Application inplemented on Spring Version: " + SpringVersion.getVersion()); }
private void registerListener(ServletContext servletContext) { servletContext.addListener(new Log4jConfigListener()); servletContext.addListener(new RequestContextListener()); }