private void initSpringProperties(Module m) { System.setProperty("server.servlet.context-path", "/" + m.getContext()); }
private int getPort(Module module) { return Integer.valueOf(Optional.ofNullable(properties.get(module.getContext() + ".port")).orElse(nextPort++).toString()); } }
private String getHost(Module module) { Try.CheckedSupplier<String,Exception> s = ()->InetAddress.getLocalHost().getHostName(); try{ return Optional.ofNullable(properties.get(module.getContext() + ".host")).orElse(s.get()).toString(); }catch(Throwable e){ throw new RuntimeException(e); } }
public ModuleBean getModuleBean(Module module) { return modulePort.getOrElse(module.getContext(),null); }
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 MicroserverEnvironment(Properties propertyFactory, Collection<ModuleBean> modules) { modulePort = HashMap.fromStream(modules.stream().map(m-> tuple(m.getModule().getContext(),m))); this.properties = propertyFactory; }
private int externalPort(ServerData next) { String ep = props.getProperty("external.port." + next.getModule().getContext()); if (ep == null) { return next.getPort(); } try { return Integer.valueOf(ep); } catch (NumberFormatException e) { return next.getPort(); } } }
@Override public void register(ServerData[] data) { try { final String hostname = Optional.ofNullable(customHostname) .orElse(InetAddress.getLocalHost() .getHostName()); application = new Application( Stream.of(data) .map(next -> new RegisterEntry( next.getPort(), hostname, next.getModule().getContext(), next.getModule().getContext(), null, targetEndpoint, externalPort(next))) .collect(Collectors.toList())); logger.info("Registered application {} ", application); } catch (UnknownHostException e) { throw ExceptionSoftener.throwSoftenedException(e); } }
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()); } }
private void startServer(WebappContext webappContext, HttpServer httpServer, CompletableFuture start, CompletableFuture end) { webappContext.deploy(httpServer); try { logger.info("Starting application {} on port {}", serverData.getModule().getContext(), serverData.getPort()); logger.info("Browse to http://localhost:{}/{}/application.wadl", serverData.getPort(), serverData.getModule().getContext()); logger.info("Configured resource classes :-"); serverData.extractResources() .forEach(t -> logger.info(t._1() + " : " + "http://localhost:" + serverData.getPort() + "/" + serverData.getModule().getContext() + t._2())); ; httpServer.start(); start.complete(true); end.get(); } catch (IOException e) { throw ExceptionSoftener.throwSoftenedException(e); } catch (ExecutionException e) { throw ExceptionSoftener.throwSoftenedException(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw ExceptionSoftener.throwSoftenedException(e); } finally { httpServer.stop(); } }
public void assureModule(Module module) { if (!modulePort.containsKey(module.getContext())) { HashMap<String, ModuleBean> builder = HashMap.empty(); builder = builder.putAll(modulePort); builder = builder.put(module.getContext(), ModuleBean.builder().host(getHost(module)).port(getPort(module)).build()); modulePort = builder; } }
@Test public void testGetters() { assertThat(serverData.getBaseUrlPattern(), is("url")); assertThat(serverData.getResources().size(), is(1)); assertThat(serverData.getRootContext(), is(rootContext)); assertThat(serverData.getModule().getContext(), is("context")); assertThat(serverData.getPort(), is(8080)); }
javax.servlet.ServletRegistration.Dynamic servletRegistration = webappContext.addServlet(config.getName() + "-" + serverData.getModule() .getContext(), config.getServlet()); Map<String, String> initParams = config.getInitParams(); for (String key : initParams.keySet()) { .getProviders()); servletRegistration.setInitParameter("context", serverData.getModule() .getContext()); servletRegistration.setLoadOnStartup(1); servletRegistration.addMapping(serverData.getBaseUrlPattern());
public ServerApplication createApp(final Module module, final ApplicationContext rootContext) { ModuleDataExtractor extractor = new ModuleDataExtractor(module); PersistentList resources = extractor.getRestResources(rootContext); MicroserverEnvironment microserverEnvironment = rootContext.getBean(MicroserverEnvironment.class); microserverEnvironment.assureModule(module); String fullRestResource = "/" + module.getContext() + "/*"; ServerData serverData = new ServerData(microserverEnvironment.getModuleBean(module).getPort(), resources, rootContext, fullRestResource, module); List<FilterData> filterDataList = extractor.createFilteredDataList(serverData); List<ServletData> servletDataList = extractor.createServletDataList(serverData); GrizzlyApplication app = new GrizzlyApplication( new AllData(serverData, filterDataList, servletDataList, module.getListeners(serverData), module.getRequestListeners(serverData))); return app; } }
private int getPort(Module module) { return Integer.valueOf(Optional.ofNullable(properties.get(module.getContext() + ".port")).orElse(nextPort++).toString()); } }
private String getHost(Module module) { Try.CheckedSupplier<String,Exception> s = ()->InetAddress.getLocalHost().getHostName(); try{ return Optional.ofNullable(properties.get(module.getContext() + ".host")).orElse(s.get()).toString(); }catch(Throwable e){ throw new RuntimeException(e); } }
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; }
private int externalPort(ServerData next) { String ep = props.getProperty("external.port." + next.getModule().getContext()); if (ep == null) { return next.getPort(); } try { return Integer.valueOf(ep); } catch (NumberFormatException e) { return next.getPort(); } } }
public MicroserverEnvironment(Properties propertyFactory, Collection<ModuleBean> modules) { modulePort = HashMap.fromStream(modules.stream().map(m-> tuple(m.getModule().getContext(),m))); this.properties = propertyFactory; }
public void assureModule(Module module) { if (!modulePort.containsKey(module.getContext())) { HashMap<String, ModuleBean> builder = HashMap.empty(); builder = builder.putAll(modulePort); builder = builder.put(module.getContext(), ModuleBean.builder().host(getHost(module)).port(getPort(module)).build()); modulePort = builder; } }