static TomcatService forConfig(TomcatServiceConfig config) { final Consumer<Connector> postStopTask = connector -> { final org.apache.catalina.Server server = connector.getService().getServer(); if (server.getState() == LifecycleState.STOPPED) { try { logger.info("Destroying an embedded Tomcat: {}", toString(server)); server.destroy(); } catch (Exception e) { logger.warn("Failed to destroy an embedded Tomcat: {}", toString(server), e); } } }; return new ManagedTomcatService(null, new ManagedConnectorFactory(config), postStopTask); }
void start() throws Exception { assert hostName() != null; started = false; connector = connectorFactory.apply(hostName()); final Service service = connector.getService(); final Engine engine = TomcatUtil.engine(service, hostName()); final String engineName = engine.getName(); if (activeEngines.contains(engineName)) { throw new TomcatServiceException("duplicate engine name: " + engineName); } server = service.getServer(); if (!TOMCAT_START_STATES.contains(server.getState())) { logger.info("Starting an embedded Tomcat: {}", toString(server)); server.start(); started = true; } else { throw new TomcatServiceException("Cannot manage already running server: " + engineName); } activeEngines.add(engineName); this.engineName = engineName; }
@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; }
private Server findServer() { try { Service[] services = (Service[]) this.mbeanServer.invoke(this.name, "findServices", null, null); return (services.length > 0) ? services[0].getServer() : null; } catch (JMException e) { throw new IllegalStateException(e); } } }
/** * Constructs a new CatalinaEngine that wraps the specified catalina engine * @param engine a catalina engine */ public CatalinaEngine(org.apache.catalina.Engine engine, MBeanServer mbeanServer) { this(engine, new CatalinaServer(engine.getService().getServer(), mbeanServer)); }
private Server getServer() { Container c = context; while (c != null && !(c instanceof Engine)) { c = c.getParent(); } if (c == null) { return null; } Service s = ((Engine)c).getService(); if (s == null) { return null; } return s.getServer(); }
@Override public File getCatalinaHome() { if (service != null) { Server s = service.getServer(); if (s != null) { File base = s.getCatalinaHome(); if (base != null) { return base; } } } // Fall-back return super.getCatalinaHome(); }
@Override public File getCatalinaBase() { if (service != null) { Server s = service.getServer(); if (s != null) { File base = s.getCatalinaBase(); if (base != null) { return base; } } } // Fall-back return super.getCatalinaBase(); }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
private javax.naming.Context getGlobalNamingContext() { if (container instanceof Context) { Engine e = (Engine) ((Context) container).getParent().getParent(); return e.getService().getServer().getGlobalNamingContext(); } return null; }
private javax.naming.Context getGlobalNamingContext() { if (container instanceof Context) { Engine e = (Engine) ((Context) container).getParent().getParent(); return e.getService().getServer().getGlobalNamingContext(); } return null; }
private void reconfigureStartStopExecutor(int threads) { if (threads == 1) { // Use a fake executor if (!(startStopExecutor instanceof InlineExecutorService)) { startStopExecutor = new InlineExecutorService(); } } else { // Delegate utility execution to the Service Server server = Container.getService(this).getServer(); server.setUtilityThreads(threads); startStopExecutor = server.getUtilityExecutor(); } }
@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); }