org.apache.catalina.Server Java Examples

The following examples show how to use org.apache.catalina.Server. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StoreConfigLifecycleListener.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Register StoreRegistry after Start the complete Server.
 *
 * @see org.apache.catalina.LifecycleListener#lifecycleEvent(org.apache.catalina.LifecycleEvent)
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {
    if (Lifecycle.AFTER_START_EVENT.equals(event.getType())) {
        if (event.getSource() instanceof Server) {
            createMBean((Server) event.getSource());
        } else {
            log.warn(sm.getString("storeConfigListener.notServer"));
        }
    } else if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
        if (oname != null) {
            registry.unregisterComponent(oname);
            oname = null;
        }
    }
 }
 
Example #2
Source File: MBeanFactory.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new StandardService and StandardEngine.
 *
 * @param domain       Domain name for the container instance
 * @param defaultHost  Name of the default host to be used in the Engine
 * @param baseDir      Base directory value for Engine 
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createStandardServiceEngine(String domain,
        String defaultHost, String baseDir) throws Exception{

    if (!(container instanceof Server)) {
        throw new Exception("Container not Server");
    }
    
    StandardEngine engine = new StandardEngine();
    engine.setDomain(domain);
    engine.setName(domain);
    engine.setDefaultHost(defaultHost);
    engine.setBaseDir(baseDir);

    Service service = new StandardService();
    service.setContainer(engine);
    service.setName(domain);
    
    ((Server) container).addService(service);
    
    return engine.getObjectName().toString();
}
 
Example #3
Source File: RealmBase.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Return the Server object that is the ultimate parent for the container
 * with which this Realm is associated. If the server cannot be found (eg
 * because the container hierarchy is not complete), <code>null</code> is
 * returned.
 */
protected Server getServer() {
    Container c = container;
    if (c instanceof Context) {
        c = c.getParent();
    }
    if (c instanceof Host) {
        c = c.getParent();
    }
    if (c instanceof Engine) {
        Service s = ((Engine)c).getService();
        if (s != null) {
            return s.getServer();
        }
    }
    return null;
}
 
Example #4
Source File: RealmBase.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Return the Server object that is the ultimate parent for the container
 * with which this Realm is associated. If the server cannot be found (eg
 * because the container hierarchy is not complete), <code>null</code> is
 * returned.
 */
protected Server getServer() {
    Container c = container;
    if (c instanceof Context) {
        c = c.getParent();
    }
    if (c instanceof Host) {
        c = c.getParent();
    }
    if (c instanceof Engine) {
        Service s = ((Engine)c).getService();
        if (s != null) {
            return s.getServer();
        }
    }
    return null;
}
 
Example #5
Source File: StoreConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Write the configuration information for this entire <code>Server</code>
 * out to the server.xml configuration file.
 *
 * @param aServer Server instance
 * @return <code>true</code> if the store operation was successful
 */
@Override
public synchronized boolean store(Server aServer) {
    StoreFileMover mover = new StoreFileMover(System
            .getProperty("catalina.base"), getServerFilename(),
            getRegistry().getEncoding());
    // Open an output writer for the new configuration file
    try {
        try (PrintWriter writer = mover.getWriter()) {
            store(writer, -2, aServer);
        }
        mover.move();
        return true;
    } catch (Exception e) {
        log.error(sm.getString("config.storeServerError"), e);
    }
    return false;
}
 
Example #6
Source File: Meecrowave.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private static Server createServer(final String serverXml) {
    final Catalina catalina = new Catalina() {
        // skip few init we don't need *here*
        @Override
        protected void initDirs() {
            // no-op
        }

        @Override
        protected void initStreams() {
            // no-op
        }

        @Override
        protected void initNaming() {
            // no-op
        }
    };
    catalina.setConfigFile(serverXml);
    catalina.load();
    return catalina.getServer();
}
 
Example #7
Source File: MBeanFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new StandardService and StandardEngine.
 *
 * @param domain       Domain name for the container instance
 * @param defaultHost  Name of the default host to be used in the Engine
 * @param baseDir      Base directory value for Engine 
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createStandardServiceEngine(String domain,
        String defaultHost, String baseDir) throws Exception{

    if (!(container instanceof Server)) {
        throw new Exception("Container not Server");
    }
    
    StandardEngine engine = new StandardEngine();
    engine.setDomain(domain);
    engine.setName(domain);
    engine.setDefaultHost(defaultHost);
    engine.setBaseDir(baseDir);

    Service service = new StandardService();
    service.setContainer(engine);
    service.setName(domain);
    
    ((Server) container).addService(service);
    
    return engine.getObjectName().toString();
}
 
Example #8
Source File: RealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Return the Server object that is the ultimate parent for the container
 * with which this Realm is associated. If the server cannot be found (eg
 * because the container hierarchy is not complete), <code>null</code> is
 * returned.
 * @return the Server associated with the realm
 */
protected Server getServer() {
    Container c = container;
    if (c instanceof Context) {
        c = c.getParent();
    }
    if (c instanceof Host) {
        c = c.getParent();
    }
    if (c instanceof Engine) {
        Service s = ((Engine)c).getService();
        if (s != null) {
            return s.getServer();
        }
    }
    return null;
}
 
Example #9
Source File: ContextConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #10
Source File: Container.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static Server createServer(final String serverXml) {
    final Catalina catalina = new Catalina() {
        // skip few init we don't need *here*
        @Override
        protected void initDirs() {
            // no-op
        }

        @Override
        protected void initStreams() {
            // no-op
        }

        @Override
        protected void initNaming() {
            // no-op
        }
    };
    catalina.setConfigFile(serverXml);
    catalina.load();
    return catalina.getServer();
}
 
Example #11
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Creates a new StandardService and StandardEngine.
 *
 * @param domain       Domain name for the container instance
 * @param defaultHost  Name of the default host to be used in the Engine
 * @param baseDir      Base directory value for Engine
 * @return the object name of the created service
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createStandardServiceEngine(String domain,
        String defaultHost, String baseDir) throws Exception{

    if (!(container instanceof Server)) {
        throw new Exception("Container not Server");
    }

    StandardEngine engine = new StandardEngine();
    engine.setDomain(domain);
    engine.setName(domain);
    engine.setDefaultHost(defaultHost);

    Service service = new StandardService();
    service.setContainer(engine);
    service.setName(domain);

    ((Server) container).addService(service);

    return engine.getObjectName().toString();
}
 
Example #12
Source File: ContextConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
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();
}
 
Example #13
Source File: ContextConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Process a "destroy" event for this Context.
 */
protected synchronized void destroy() {
    // Called from StandardContext.destroy()
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("contextConfig.destroy"));
    }

    // Skip clearing the work directory if Tomcat is being shutdown
    Server s = getServer();
    if (s != null && !s.getState().isAvailable()) {
        return;
    }

    // Changed to getWorkPath per Bugzilla 35819.
    if (context instanceof StandardContext) {
        String workDir = ((StandardContext) context).getWorkPath();
        if (workDir != null) {
            ExpandWar.delete(new File(workDir));
        }
    }
}
 
Example #14
Source File: ContextConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Process a "destroy" event for this Context.
 */
protected synchronized void destroy() {
    // Called from StandardContext.destroy()
    if (log.isDebugEnabled())
        log.debug(sm.getString("contextConfig.destroy"));

    // Skip clearing the work directory if Tomcat is being shutdown
    Server s = getServer();
    if (s != null && !s.getState().isAvailable()) {
        return;
    }

    // Changed to getWorkPath per Bugzilla 35819.
    if (context instanceof StandardContext) {
        String workDir = ((StandardContext) context).getWorkPath();
        if (workDir != null) {
            ExpandWar.delete(new File(workDir));
        }
    }
}
 
Example #15
Source File: ServingLayer.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * Blocks and waits until the server shuts down.
 */
public void await() {
  Server server;
  synchronized (this) {
    server = tomcat.getServer();
  }
  server.await(); // Can't do this with lock held
}
 
Example #16
Source File: Cli.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void close() {
    if (closed) {
        return;
    }
    this.closed = true;
    if (instance != null) {
        final Server server = instance.getTomcat().getServer();
        if (StandardServer.class.isInstance(server)) {
            StandardServer.class.cast(server).stopAwait();
        }
    }
}
 
Example #17
Source File: NamingResources.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #18
Source File: ThreadLocalLeakPreventionListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Listens for {@link LifecycleEvent} for the start of the {@link Server} to
 * initialize itself and then for after_stop events of each {@link Context}.
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {
    try {
        Lifecycle lifecycle = event.getLifecycle();
        if (Lifecycle.AFTER_START_EVENT.equals(event.getType()) &&
                lifecycle instanceof Server) {
            // when the server starts, we register ourself as listener for
            // all context
            // as well as container event listener so that we know when new
            // Context are deployed
            Server server = (Server) lifecycle;
            registerListenersForServer(server);
        }

        if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType()) &&
                lifecycle instanceof Server) {
            // Server is shutting down, so thread pools will be shut down so
            // there is no need to clean the threads
            serverStopping = true;
        }

        if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType()) &&
                lifecycle instanceof Context) {
            stopIdleThreads((Context) lifecycle);
        }
    } catch (Exception e) {
        String msg =
            sm.getString(
                "threadLocalLeakPreventionListener.lifecycleEvent.error",
                event);
        log.error(msg, e);
    }
}
 
Example #19
Source File: Catalina.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Stop an existing server instance.
 */
public void stop() {

    try {
        // Remove the ShutdownHook first so that server.stop()
        // doesn't get invoked twice
        if (useShutdownHook) {
            Runtime.getRuntime().removeShutdownHook(shutdownHook);

            // If JULI is being used, re-enable JULI's shutdown to ensure
            // log messages are not lost
            LogManager logManager = LogManager.getLogManager();
            if (logManager instanceof ClassLoaderLogManager) {
                ((ClassLoaderLogManager) logManager).setUseShutdownHook(
                        true);
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // This will fail on JDK 1.2. Ignoring, as Tomcat can run
        // fine without the shutdown hook.
    }

    // Shut down the server
    try {
        Server s = getServer();
        LifecycleState state = s.getState();
        if (LifecycleState.STOPPING_PREP.compareTo(state) <= 0
                && LifecycleState.DESTROYED.compareTo(state) >= 0) {
            // Nothing to do. stop() was already called
        } else {
            s.stop();
            s.destroy();
        }
    } catch (LifecycleException e) {
        log.error("Catalina.stop", e);
    }

}
 
Example #20
Source File: MBeanFactory.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Remove an existing Service.
 *
 * @param name MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeService(String name) throws Exception {

    if (!(container instanceof Server)) {
        throw new Exception();
    }
    
    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(name);
    Service service = getService(oname); 
    ((Server) container).removeService(service);
}
 
Example #21
Source File: TomcatSetup.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(final Tomcat tomcat) {
    final Server server = tomcat.getServer();
    server.addLifecycleListener(event -> {
        if (Server.class.isInstance(event.getData()) && Lifecycle.AFTER_DESTROY_EVENT.equals(event.getType())
                && Boolean.getBoolean("talend.component.exit-on-destroy")) {
            System.exit(0);
        }
    });
    // if we want it to be really configurable we should add it in ComponentServerConfiguration
    // and set this instance in the standard context to be able to configure it from cdi side
    final boolean dev = Boolean.getBoolean("talend.component.server.tomcat.valve.error.debug");
    if (!dev) {
        Stream
                .of(server.findServices())
                .map(Service::getContainer)
                .flatMap(e -> Stream.of(e.findChildren()))
                .filter(StandardHost.class::isInstance)
                .map(StandardHost.class::cast)
                .forEach(host -> host.addLifecycleListener(event -> {
                    if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
                        StandardHost.class
                                .cast(host)
                                .setErrorReportValveClass(MinimalErrorReportValve.class.getName());
                    }
                }));
    }
}
 
Example #22
Source File: Catalina.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Stop an existing server instance.
 */
public void stop() {

    try {
        // Remove the ShutdownHook first so that server.stop()
        // doesn't get invoked twice
        if (useShutdownHook) {
            Runtime.getRuntime().removeShutdownHook(shutdownHook);

            // If JULI is being used, re-enable JULI's shutdown to ensure
            // log messages are not lost
            LogManager logManager = LogManager.getLogManager();
            if (logManager instanceof ClassLoaderLogManager) {
                ((ClassLoaderLogManager) logManager).setUseShutdownHook(
                        true);
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // This will fail on JDK 1.2. Ignoring, as Tomcat can run
        // fine without the shutdown hook.
    }

    // Shut down the server
    try {
        Server s = getServer();
        LifecycleState state = s.getState();
        if (LifecycleState.STOPPING_PREP.compareTo(state) <= 0
                && LifecycleState.DESTROYED.compareTo(state) >= 0) {
            // Nothing to do. stop() was already called
        } else {
            s.stop();
            s.destroy();
        }
    } catch (LifecycleException e) {
        log.error("Catalina.stop", e);
    }

}
 
Example #23
Source File: TomcatSecurityService.java    From tomee with Apache License 2.0 5 votes vote down vote up
public TomcatSecurityService() {
    final Server server = TomcatHelper.getServer();
    for (final Service service : server.findServices()) {
        if (service.getContainer() instanceof Engine) {
            final Engine engine = (Engine) service.getContainer();
            if (engine.getRealm() != null) {
                defaultRealm = engine.getRealm();
                break;
            }
        }
    }
}
 
Example #24
Source File: Log4j2ShutdownHooksExecutor.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void lifecycleEvent(final LifecycleEvent event) {
    if (Server.class.isInstance(event.getSource()) && Lifecycle.AFTER_DESTROY_EVENT.equals(event.getType())) {
        final Collection<Runnable> copy = new ArrayList<>(CaptureLog4j2ShutdownHooks.HOOKS);
        CaptureLog4j2ShutdownHooks.HOOKS.removeAll(copy);
        for (final Runnable runnable : copy) {
            runnable.run();
        }
    }
}
 
Example #25
Source File: ThreadLocalLeakPreventionListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void registerListenersForServer(Server server) {
    for (Service service : server.findServices()) {
        Engine engine = (Engine) service.getContainer();
        engine.addContainerListener(this);
        registerListenersForEngine(engine);
    }

}
 
Example #26
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void registerListenersForServer(Server server) {
    for (Service service : server.findServices()) {
        Engine engine = (Engine) service.getContainer();
        engine.addContainerListener(this);
        registerListenersForEngine(engine);
    }

}
 
Example #27
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Listens for {@link LifecycleEvent} for the start of the {@link Server} to
 * initialize itself and then for after_stop events of each {@link Context}.
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {
    try {
        Lifecycle lifecycle = event.getLifecycle();
        if (Lifecycle.AFTER_START_EVENT.equals(event.getType()) &&
                lifecycle instanceof Server) {
            // when the server starts, we register ourself as listener for
            // all context
            // as well as container event listener so that we know when new
            // Context are deployed
            Server server = (Server) lifecycle;
            registerListenersForServer(server);
        }

        if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType()) &&
                lifecycle instanceof Server) {
            // Server is shutting down, so thread pools will be shut down so
            // there is no need to clean the threads
            serverStopping = true;
        }

        if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType()) &&
                lifecycle instanceof Context) {
            stopIdleThreads((Context) lifecycle);
        }
    } catch (Exception e) {
        String msg =
            sm.getString(
                "threadLocalLeakPreventionListener.lifecycleEvent.error",
                event);
        log.error(msg, e);
    }
}
 
Example #28
Source File: NamingResources.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #29
Source File: MBeanFactory.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Remove an existing Service.
 *
 * @param name MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeService(String name) throws Exception {

    if (!(container instanceof Server)) {
        throw new Exception();
    }
    
    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(name);
    Service service = getService(oname); 
    ((Server) container).removeService(service);
}
 
Example #30
Source File: Runner.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
private static void configureServer(Server server) {
  //server.addLifecycleListener(new SecurityListener());
  //server.addLifecycleListener(new AprLifecycleListener());
  LifecycleListener jasperListener = new JasperListener();
  server.addLifecycleListener(jasperListener);
  jasperListener.lifecycleEvent(new LifecycleEvent(server, Lifecycle.BEFORE_INIT_EVENT, null));
  server.addLifecycleListener(new JreMemoryLeakPreventionListener());
  //server.addLifecycleListener(new GlobalResourcesLifecycleListener());
  server.addLifecycleListener(new ThreadLocalLeakPreventionListener());
}