public void addServlets(ServletContext webappContext) { addExplicitlyDeclaredServlets(webappContext); addAutoDiscoveredServlets(webappContext); } private void handleServlet(ServletConfiguration servlet,ServletContext webappContext){
public void addFilters(ServletContext webappContext) { addExplicitlyDeclaredFilters(webappContext); addAutoDiscoveredFilters(webappContext); }
private void addListeners(WebappContext webappContext) { new ServletContextListenerConfigurer(serverData, servletContextListenerData, servletRequestListenerData).addListeners(webappContext); }
public void run(CompletableFuture start, JaxRsServletConfigurer jaxRsConfigurer, CompletableFuture end) { WebappContext webappContext = new WebappContext("WebappContext", ""); new ServletContextListenerConfigurer(serverData, servletContextListenerData, servletRequestListenerData); jaxRsConfigurer.addServlet(this.serverData,webappContext); new ServletConfigurer(serverData, servletData).addServlets(webappContext); new FilterConfigurer(serverData, this.filterData).addFilters(webappContext); addListeners(webappContext); HttpServer httpServer = HttpServer.createSimpleServer(null, "0.0.0.0", serverData.getPort()); serverData.getModule().getServerConfigManager().accept(new WebServerProvider(httpServer)); addAccessLog(httpServer); addSSL(httpServer); startServer(webappContext, httpServer, start, end); }
private void addAccessLog(HttpServer httpServer) { try { String accessLogLocation = serverData.getRootContext().getBean(AccessLogLocationBean.class).getAccessLogLocation(); accessLogLocation = accessLogLocation + "/" + replaceSlash(serverData.getModule().getContext()) + "-access.log"; final AccessLogBuilder builder = new AccessLogBuilder(accessLogLocation); builder.rotatedDaily(); builder.rotationPattern("yyyy-MM-dd"); builder.instrument(httpServer.getServerConfiguration()); } catch (Exception e) { logger.error(InternalErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getMessage()); if (e.getCause() != null) logger.error("CAUSED BY: " + InternalErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getCause().getMessage()); } }
public List<Thread> run() { register.ifPresent( reg -> reg.register( apps.stream().map(app -> app.getServerData()) .collect(Collectors.toList()) .toArray(new ServerData[0]))); Map<ServerApplication,CompletableFuture> mapFutures = new HashMap<>(); apps.stream().forEach(app -> mapFutures.put(app,new CompletableFuture())); List<Thread> threads = apps.stream().map(app -> start(app, app.getServerData().getModule(),mapFutures.get(app))).collect(Collectors.toList()); mapFutures.values().forEach(future -> get(future)); logger.info("Started {} Rest applications ", apps.size()); return threads; }
private void addExplicitlyDeclaredFilters(ServletContext webappContext) { for (FilterData filterData : filterData) { Dynamic filterReg = webappContext.addFilter( filterData.getFilterName(), filterData.getFilter()); filterReg.addMappingForUrlPatterns( EnumSet.allOf(DispatcherType.class),true, filterData.getMapping()); logFilter(filterData); } } private void logFilter(FilterData filter) {
@Before public void setUp() { server1Count =0; server2Count =0; ServerData data1 = new ServerData(8080,Arrays.asList(), Mockito.mock(ApplicationContext.class), "url1", () -> "app-context"); ServerData data2 = new ServerData(8081, Arrays.asList(), Mockito.mock(ApplicationContext.class), "url2", () -> "test-context"); serverApplication1 = new GrizzlyApplication(AllData.builder().serverData(data1).build()){ @Override public void run(CompletableFuture start,JaxRsServletConfigurer jaxRsConfigurer, CompletableFuture end) { server1Count++; start.complete(true); } }; serverApplication2 = new GrizzlyApplication(AllData.builder().serverData(data2).build()){ @Override public void run(CompletableFuture start,JaxRsServletConfigurer jaxRsConfigurer,CompletableFuture end) { server2Count++; start.complete(true); } }; serverRunner = new ServerRunner( (array) -> {registered = array; } , Arrays.asList(serverApplication1, serverApplication2), new CompletableFuture()); }
private void addExplicitlyDeclaredServlets(ServletContext webappContext) { for (ServletData servletData : servletData) { ServletRegistration.Dynamic servletReg = webappContext.addServlet( servletData.getServletName(), servletData.getServlet()); servletReg.addMapping(servletData.getMapping()); logServlet(servletData); } }
private void addAutoDiscoveredServlets(ServletContext webappContext) { serverData .getRootContext() .getBeansOfType(ServletConfiguration.class) .values() .forEach(servlet -> handleServlet(servlet,webappContext)); }
private void addAutoDiscoveredFilters(ServletContext webappContext) { serverData .getRootContext() .getBeansOfType(FilterConfiguration.class) .values() .stream() .filter(f->f.getMapping()!=null) .filter(f->f.getMapping().length>0) .peek(this::logFilter) .forEach(config->handleFilter(config,webappContext)); }
private Thread start(ServerApplication next, Module module, CompletableFuture start) { Thread t = new Thread(() -> { ServerThreadLocalVariables.getContext().set(module.getContext()); next.run(start,new JaxRsServletConfigurer(), end); }); t.setName(module.getContext()); t.start(); return t; }
public List<Thread> start() { List<ServerApplication> apps = modules.map(module -> createServer(module)); ServerRunner runner; try { runner = new ServerRunner( springContext.getBean(ApplicationRegister.class), apps, end); } catch (BeansException e) { runner = new ServerRunner( apps, end); } return runner.run(); }
@Bean public AccessLogLocationBean accessLogLocationBean() { return new AccessLogLocationBean(accessLogLocation); } }
private void handleFilter(FilterConfiguration filter,ServletContext webappContext){ filter.getFilter().fold(clazz-> { setInitParameters(webappContext.addFilter(getName(filter), clazz), filter) .addMappingForUrlPatterns( EnumSet.allOf(DispatcherType.class),true, filter.getMapping()); return 1; }, obj-> { Dynamic filterReg = webappContext.addFilter( getName(filter), obj); filterReg.addMappingForUrlPatterns( EnumSet.allOf(DispatcherType.class),true, filter.getMapping()); return 2; }); } private void addAutoDiscoveredFilters(ServletContext webappContext) {
private void handleServlet(ServletConfiguration servlet,ServletContext webappContext){ servlet.getServlet().fold(clazz-> { setInitParameters(webappContext.addServlet(getName(servlet), clazz), servlet) .addMapping(servlet.getMapping()); return 1; }, obj-> { ServletRegistration.Dynamic servletReg = webappContext.addServlet( servlet.getName(), obj); servletReg.addMapping(servlet.getMapping()); return 2; }); } private void addAutoDiscoveredServlets(ServletContext webappContext) {
@Test public void testRun() { serverRunner.run(); assertThat(server1Count,is(1)); assertThat(server2Count,is(1)); } }
private ServerApplication createServer(Module module) { List<ServerApplicationFactory> applications = ReactiveSeq.fromStream(PluginLoader.INSTANCE.plugins.get() .stream()) .filter(m -> m.serverApplicationFactory() != null) .map(Plugin::serverApplicationFactory) .flatMap(Streams::optionalToStream) .toList(); if (applications.size() > 1) { logger.error("ERROR! Multiple server application factories found : The solution is remove one these plugins from your classpath ", applications); System.err.println("ERROR! Multiple server application factories found : The solution is remove one these plugins from your classpath " + applications); throw new IncorrectNumberOfServersConfiguredException( "Multiple server application factories found : The solution is remove one these plugins from your classpath " + applications); } else if (applications.size() == 0) { logger.error("ERROR! No server application factories found. If you using micro-spring-boot don't call MicroserverApp.start() method. A possible solution is add one of micro-grizzly or micro-tomcat to the classpath."); System.err.println("ERROR! No server application factories found. If you using micro-spring-boot don't call MicroserverApp.start() method. A possible solution is add one of micro-grizzly or micro-tomcat to the classpath."); throw new IncorrectNumberOfServersConfiguredException( "No server application factories found. If you using micro-spring-boot don't call MicroserverApp.start() method. A possible solution is add one of micro-grizzly or micro-tomcat to the classpath. "); } ServerApplication app = applications.get(0) .createApp(module, springContext); return app; }
@Bean public AccessLogLocationBean createAccessLogLocationBean( ApplicationContext rootContext) { Properties props = (Properties) rootContext.getBean("propertyFactory"); String location = Optional.ofNullable( (String) props.get("access.log.output")).orElse("./logs/"); return new AccessLogLocationBean(location); }
@Bean public AccessLogLocationBean createAccessLogLocationBean( ApplicationContext rootContext) { Properties props = (Properties) rootContext.getBean("propertyFactory"); String location = Optional.ofNullable( (String) props.get("access.log.output")).orElse("./logs/"); return new AccessLogLocationBean(location); }