@Override public Connector apply(String hostname) { // Create the connector with our protocol handler. Tomcat will call ProtocolHandler.setAdapter() // on its startup with the Coyote Adapter which gives an access to Tomcat's HTTP service pipeline. final Class<?> protocolType = TomcatService.PROTOCOL_HANDLER_CLASS; final Connector connector = new Connector(protocolType.getName()); // We do not really open a port - just trying to stop the Connector from complaining. connector.setPort(0); final StandardServer server = newServer(hostname, connector, config); // Retrieve the components configured by newServer(), so we can use it in checkConfiguration(). final Service service = server.findServices()[0]; final Engine engine = TomcatUtil.engine(service, hostname); final StandardHost host = (StandardHost) engine.findChildren()[0]; final Context context = (Context) host.findChildren()[0]; // Apply custom configurators set via TomcatServiceBuilder.configurator() try { config.configurators().forEach(c -> c.accept(server)); } catch (Throwable t) { throw new TomcatServiceException("failed to configure an embedded Tomcat", t); } // Make sure the configurators did not ruin what we have configured in this method. checkConfiguration(server, service, connector, engine, host, context); assert connector.getService().getServer() != null; return connector; }
rootContext.setLoader(loader); rootContext.setReloadable(true); rootContext.setIgnoreAnnotations(true); localHost.addChild(rootContext); localHost.setDeployOnStartup(true); engine.setName("localEngine"); engine.addChild(localHost); engine.setDefaultHost(localHost.getName()); engine.setJvmRoute(jvmRoute); engine.setService(new StandardService()); container.addEngine(engine);
@Test public void verifySingleConnector() { // Relevant to Tomcat 9.0 assertThat(applicationContext).isInstanceOf(WebServerApplicationContext.class); WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer(); assertThat(webServer).isInstanceOf(TomcatWebServer.class); assertThat(((TomcatWebServer) webServer).getTomcat() .getEngine() .getService() .findConnectors()).hasSize(1); }
/** * Returns the {@link Engine} of the specified Tomcat {@link Service}. If the {@link Service} has no * {@link Engine} set, a new {@link Engine} will be created and set. * * <p>The return type of {@link Service#getContainer()} has been changed from {@link Container} to * {@link Engine} since 8.5. Calling it directly will cause {@link NoSuchMethodError}. */ static Engine engine(Service service, String hostname) { final Engine engine; try { final Method m = Service.class.getDeclaredMethod("getContainer"); engine = (Engine) m.invoke(service); } catch (Exception e) { throw new Error("failed to invoke Service.getContainer()", e); } if (engine != null) { return engine; } // Create a new engine. final Engine newEngine = new StandardEngine(); newEngine.setName("Tomcat"); newEngine.setDefaultHost(hostname); newEngine.setRealm(new NullRealm()); service.setContainer(newEngine); return newEngine; }
/** * Render a list of the currently active Contexts in our virtual host. * * @param writer Writer to render to * @param smClient StringManager for the client's locale */ protected void list(PrintWriter writer, StringManager smClient) { if (debug >= 1) { log(sm.getString("hostManagerServlet.list", engine.getName())); } writer.println(smClient.getString("hostManagerServlet.listed", engine.getName())); Container[] hosts = engine.findChildren(); for (int i = 0; i < hosts.length; i++) { Host host = (Host) hosts[i]; String name = host.getName(); String[] aliases = host.findAliases(); writer.println(smClient.getString("hostManagerServlet.listitem", name, StringUtils.join(aliases))); } }
StringManager smClient) { Server server = ((Engine)host.getParent()).getService().getServer(); writer.println(smClient.getString("managerServlet.saveFail", server)); return; try { ((StandardServer) server).storeConfig(); writer.println(smClient.getString("managerServlet.saved")); } catch (Exception e) { log("managerServlet.storeConfig", e); writer.println(smClient.getString("managerServlet.exception", e.toString())); return; contextPath = ""; Context context = (Context) host.findChild(contextPath); if (context == null) { writer.println(smClient.getString("managerServlet.noContext",
private void findDefaultHost() { Engine engine = (Engine) connector.getService().getContainer(); String defaultHost = engine.getDefaultHost(); boolean found = false; if (defaultHost != null && defaultHost.length() >0) { Container[] containers = engine.findChildren(); for (Container container : containers) { Host host = (Host) container; if (defaultHost.equalsIgnoreCase(host.getName())) { found = true; break; } String[] aliases = host.findAliases(); for (String alias : aliases) { if (defaultHost.equalsIgnoreCase(alias)) { found = true; break; } } } } if(found) { mapper.setDefaultHostName(defaultHost); } else { log.warn(sm.getString("mapperListener.unknownDefaultHost", defaultHost, connector)); } }
private void findDefaultHost() { Engine engine = service.getContainer(); String defaultHost = engine.getDefaultHost(); boolean found = false; if (defaultHost != null && defaultHost.length() > 0) { Container[] containers = engine.findChildren(); for (Container container : containers) { Host host = (Host) container; if (defaultHost.equalsIgnoreCase(host.getName())) { found = true; break; } String[] aliases = host.findAliases(); for (String alias : aliases) { if (defaultHost.equalsIgnoreCase(alias)) { found = true; break; } } } } if (found) { mapper.setDefaultHostName(defaultHost); } else { log.error(sm.getString("mapperListener.unknownDefaultHost", defaultHost, service)); } }
private Server getServer() { if (container instanceof Server) { return (Server) container; } if (container instanceof Context) { // Could do this in one go. Lots of casts so split out for clarity Engine engine = (Engine) ((Context) container).getParent().getParent(); return engine.getService().getServer(); } return null; }
@Override public void setWrapper(Wrapper wrapper) { Valve valve = createValve(); if (wrapper != null) { host = (Host) wrapper.getParent().getParent(); Engine engine = (Engine) host.getParent(); Service service = engine.getService(); connectors = service.findConnectors(); try { deployerOName = new ObjectName(host.getParent().getName() + ":type=Deployer,host=" + host.getName()); } catch (MalformedObjectNameException e) { logger.trace("", e); } host.getPipeline().addValve(valve); mbeanServer = ManagementFactory.getPlatformMBeanServer(); } else if (host != null) { host.getPipeline().removeValve(valve); } }
@Override public void startInternal() throws LifecycleException { setState(LifecycleState.STARTING); // Find any components that have already been initialized since the // MBean listener won't be notified as those components will have // already registered their MBeans findDefaultHost(); Engine engine = (Engine) connector.getService().getContainer(); addListeners(engine); Container[] conHosts = engine.findChildren(); for (Container conHost : conHosts) { Host host = (Host) conHost; if (!LifecycleState.NEW.equals(host.getState())) { // Registering the host will register the context and wrappers registerHost(host); } } }
rootContext.setLoader(loader); rootContext.setReloadable(true); localHost.addChild(rootContext); engine.setName("localEngine"); engine.addChild(localHost); engine.setDefaultHost(localHost.getName()); container.addEngine(engine);
private void expire(SingleSignOnSessionKey key) { if (engine == null) { containerLog.warn(sm.getString("singleSignOn.sessionExpire.engineNull", key)); return; Container host = engine.findChild(key.getHostName()); if (host == null) { containerLog.warn(sm.getString("singleSignOn.sessionExpire.hostNotFound", key)); return; Context context = (Context) host.findChild(key.getContextName()); if (context == null) { containerLog.warn(sm.getString("singleSignOn.sessionExpire.contextNotFound", key)); return; Manager manager = context.getManager(); if (manager == null) { containerLog.warn(sm.getString("singleSignOn.sessionExpire.managerNotFound", key));
if (!config.serviceName().equals(expectedService.getName())) { throw new TomcatServiceException( "A configurator should never change the name of the default service."); final Connector[] connectors = expectedService.findConnectors(); if (connectors == null || Arrays.stream(connectors).noneMatch(c -> c == expectedConnector)) { throw new TomcatServiceException("A configurator should never remove the default connector."); if (!config.engineName().equals(expectedEngine.getName())) { throw new TomcatServiceException( "A configurator should never change the name of the default engine."); if (expectedEngine.getRealm() != config.realm()) { throw new TomcatServiceException("A configurator should never change the default realm."); final Container[] engineChildren = expectedEngine.findChildren(); if (engineChildren == null || Arrays.stream(engineChildren).noneMatch(c -> c == expectedHost)) { throw new TomcatServiceException("A configurator should never remove the default host."); if (!config.docBase().toString().equals(expectedContext.getDocBase())) { throw new TomcatServiceException( "A configurator should never change the docBase of the default context.");
protected Map<String,Set<String>> getConnectorCiphers() { Map<String,Set<String>> result = new HashMap<>(); Engine e = (Engine) host.getParent(); Service s = e.getService(); Connector connectors[] = s.findConnectors(); for (Connector connector : connectors) { Set<String> cipherList = new HashSet<>(); if (Boolean.TRUE.equals(connector.getProperty("SSLEnabled"))) { String[] ciphersUsed = (String[]) connector.getProperty("ciphersUsed"); for (String cipherUsed : ciphersUsed) { cipherList.add(cipherUsed); } } else { cipherList.add(sm.getString("managerServlet.notSslConnector")); } result.put(connector.toString(), cipherList); } return result; } }
private void populateSessionTrackingModes() { // URL re-writing is always enabled by default defaultSessionTrackingModes = EnumSet.of(SessionTrackingMode.URL); supportedSessionTrackingModes = EnumSet.of(SessionTrackingMode.URL); if (context.getCookies()) { defaultSessionTrackingModes.add(SessionTrackingMode.COOKIE); supportedSessionTrackingModes.add(SessionTrackingMode.COOKIE); } // SSL not enabled by default as it can only used on its own // Context > Host > Engine > Service Service s = ((Engine) context.getParent().getParent()).getService(); Connector[] connectors = s.findConnectors(); // Need at least one SSL enabled connector to use the SSL session ID. for (Connector connector : connectors) { if (Boolean.TRUE.equals(connector.getAttribute("SSLEnabled"))) { supportedSessionTrackingModes.add(SessionTrackingMode.SSL); break; } } }
private Connector[] getConnectors() { Engine e = (Engine) host.getParent(); Service s = e.getService(); return s.findConnectors(); } }
/** * Access to the engine, for further customization. */ public Engine getEngine() { if(engine == null ) { getServer(); engine = new StandardEngine(); engine.setName( "Tomcat" ); engine.setDefaultHost(hostname); service.setContainer(engine); } return engine; }
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0); ObjectName name = new ObjectName("Catalina", "type", "Server"); Server server = (Server) mBeanServer.getAttribute(name, "managedResource"); Service service = server.findService("Catalina"); Engine engine = (Engine) service.getContainer(); Host host = (Host) engine.findChild(engine.getDefaultHost()); host.getAppBase(); //Got it.