@Override public void setup() { Connector connector = new Connector(Http11NioProtocol.class.getName()); connector.setPort(0); File baseDir = createTempDir("tomcat"); String baseDirPath = baseDir.getAbsolutePath(); this.tomcatServer = new Tomcat(); this.tomcatServer.setBaseDir(baseDirPath); this.tomcatServer.setPort(0); this.tomcatServer.getService().addConnector(connector); this.tomcatServer.setConnector(connector); }
private String getPortsDescription(boolean localPort) { StringBuilder ports = new StringBuilder(); for (Connector connector : this.tomcat.getService().findConnectors()) { if (ports.length() != 0) { ports.append(' '); } int port = localPort ? connector.getLocalPort() : connector.getPort(); ports.append(port).append(" (").append(connector.getScheme()).append(')'); } return ports.toString(); }
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.");
private void removeServiceConnectors() { for (Service service : this.tomcat.getServer().findServices()) { Connector[] connectors = service.findConnectors().clone(); this.serviceConnectors.put(service, connectors); for (Connector connector : connectors) { service.removeConnector(connector); } } }
public ObjectName getParentName() throws MalformedObjectNameException { if (getService()==null) { return null; } String name = getService().getName(); ObjectName serviceName=new ObjectName(domain + ":type=Service,serviceName="+name); return serviceName; }
/** * Remove an existing Host. * * @param name MBean Name of the component to remove * * @exception Exception if a component cannot be removed */ public void removeHost(String name) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(name); String hostName = oname.getKeyProperty("host"); Service service = getService(oname); Engine engine = service.getContainer(); Host host = (Host) engine.findChild(hostName); // Remove this component from its parent component if(host!=null) { engine.removeChild(host); } }
@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); } }
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.
ObjectName name = null; try { name = new ObjectName("Catalina", "type", "Server"); } catch (MalformedObjectNameException e1) { e1.printStackTrace(); for (Connector connector : service.findConnectors()) { System.out.println(connector); String uriEncoding = connector.getURIEncoding(); System.out.println("URIEncoding : " + uriEncoding); boolean use = connector.getUseBodyEncodingForURI();
@Override public WebServer getWebServer(ServletContextInitializer... initializers) { Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); tomcat.setBaseDir(baseDir.getAbsolutePath()); Connector connector = new Connector(this.protocol); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); configureEngine(tomcat.getEngine()); for (Connector additionalConnector : this.additionalTomcatConnectors) { tomcat.getService().addConnector(additionalConnector); } prepareContext(tomcat.getHost(), initializers); return getTomcatWebServer(tomcat); }
Engine engine = (Engine) context.getParent().getParent(); Service service = engine.getService(); Connector[] connectors = service.findConnectors(); if (connectors != null) { for (Connector connector : connectors) { ProtocolHandler handler = connector.getProtocolHandler(); Executor executor = null; if (handler != null) {
public String encodeURL(String relativePath, String scheme) { StringBuffer urlEncoded = new StringBuffer(); //Context Context context = (Context)sipManager.getContainer(); //Host Host host = (Host)context.getParent(); //Service Service service = ((Engine)host.getParent()).getService(); String hostname = host.getName(); //retrieving the port corresponding to the specified scheme //TODO ask EG what if the scheme is not supported on the server ? int port = -1; Connector[] connectors = service.findConnectors(); int i = 0; while (i < connectors.length && port < 0) { if(scheme != null && connectors[i].getProtocol().toLowerCase().contains(scheme.toLowerCase())) { port = connectors[i].getPort(); } i++; } urlEncoded = urlEncoded.append(scheme); urlEncoded = urlEncoded.append("://"); urlEncoded = urlEncoded.append(hostname); urlEncoded = urlEncoded.append(":"); urlEncoded = urlEncoded.append(port); urlEncoded = urlEncoded.append(((Context)sipManager.getContainer()).getPath()); urlEncoded = urlEncoded.append(encodeURL(relativePath)); return urlEncoded.toString(); }
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; }
@Test public void bind_to_specific_address() { Properties p = new Properties(); p.setProperty("sonar.web.port", "9000"); p.setProperty("sonar.web.host", "1.2.3.4"); TomcatConnectors.configure(tomcat, new Props(p)); verify(tomcat.getService()) .addConnector(argThat(c -> c.getScheme().equals("http") && c.getPort() == 9000 && ((InetAddress) c.getProperty("address")).getHostAddress().equals("1.2.3.4"))); }
/** * Access to the engine, for further customization. * @return The engine */ public Engine getEngine() { Service service = getServer().findServices()[0]; if (service.getContainer() != null) { return service.getContainer(); } Engine engine = new StandardEngine(); engine.setName( "Tomcat" ); engine.setDefaultHost(hostname); engine.setRealm(createDefaultRealm()); service.setContainer(engine); return engine; }
@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; }
@Test public void test() throws Exception { MBeanServer mbeanServer = mock(MBeanServer.class); ObjectName name = ObjectName.getInstance("Catalina:type=Server"); Server expected = mock(Server.class); Service service = mock(Service.class); when(mbeanServer.invoke(same(name), eq("findServices"), (Object[]) isNull(), (String[]) isNull())).thenReturn(new Service[] { service }); when(service.getServer()).thenReturn(expected); ServerProvider provider = new JMXServerProvider(mbeanServer, name); Server result = provider.getServer(); assertSame(expected, result); }
@Override public void startInternal() { 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); } } }
/** * Returns an application server URL constructed using the specified request. * * @param request the HTTP servlet request * @return the application server URL constructed */ public static Optional<String> constructApplicationServerURL(Request request) { if (request == null) { return Optional.empty(); } String sslConnectorScheme = "https"; StringBuilder appServerURL = new StringBuilder(sslConnectorScheme + "://"); String requestHost = request.getHost().getName(); Optional<Connector> sslConnector = Arrays.stream(((Engine) request.getHost().getParent()).getService().findConnectors()) .filter(connector -> connector.getScheme().equals(sslConnectorScheme)) .findFirst(); if (sslConnector.isPresent()) { return Optional.of(appServerURL.append(requestHost).append(":") .append(sslConnector.get().getPort()).toString()); } else { return Optional.empty(); } }
/** * 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; }