Search in sources :

Example 1 with Lifecycle

use of org.apache.catalina.Lifecycle in project tomcat by apache.

the class ThreadLocalLeakPreventionListener method lifecycleEvent.

/**
     * 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);
    }
}
Also used : Context(org.apache.catalina.Context) Server(org.apache.catalina.Server) Lifecycle(org.apache.catalina.Lifecycle)

Example 2 with Lifecycle

use of org.apache.catalina.Lifecycle in project tomcat by apache.

the class StandardContext method setManager.

@Override
public void setManager(Manager manager) {
    Lock writeLock = managerLock.writeLock();
    writeLock.lock();
    Manager oldManager = null;
    try {
        // Change components if necessary
        oldManager = this.manager;
        if (oldManager == manager)
            return;
        this.manager = manager;
        // Stop the old component if necessary
        if (oldManager instanceof Lifecycle) {
            try {
                ((Lifecycle) oldManager).stop();
                ((Lifecycle) oldManager).destroy();
            } catch (LifecycleException e) {
                log.error("StandardContext.setManager: stop-destroy: ", e);
            }
        }
        // Start the new component if necessary
        if (manager != null) {
            manager.setContext(this);
        }
        if (getState().isAvailable() && manager instanceof Lifecycle) {
            try {
                ((Lifecycle) manager).start();
            } catch (LifecycleException e) {
                log.error("StandardContext.setManager: start: ", e);
            }
        }
    } finally {
        writeLock.unlock();
    }
    // Report this property change to interested listeners
    support.firePropertyChange("manager", oldManager, manager);
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) Lifecycle(org.apache.catalina.Lifecycle) Manager(org.apache.catalina.Manager) InstanceManager(org.apache.tomcat.InstanceManager) StandardManager(org.apache.catalina.session.StandardManager) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 3 with Lifecycle

use of org.apache.catalina.Lifecycle in project tomcat by apache.

the class StandardPipeline method setBasic.

/**
     * <p>Set the Valve instance that has been distinguished as the basic
     * Valve for this Pipeline (if any).  Prior to setting the basic Valve,
     * the Valve's <code>setContainer()</code> will be called, if it
     * implements <code>Contained</code>, with the owning Container as an
     * argument.  The method may throw an <code>IllegalArgumentException</code>
     * if this Valve chooses not to be associated with this Container, or
     * <code>IllegalStateException</code> if it is already associated with
     * a different Container.</p>
     *
     * @param valve Valve to be distinguished as the basic Valve
     */
@Override
public void setBasic(Valve valve) {
    // Change components if necessary
    Valve oldBasic = this.basic;
    if (oldBasic == valve)
        return;
    // Stop the old component if necessary
    if (oldBasic != null) {
        if (getState().isAvailable() && (oldBasic instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldBasic).stop();
            } catch (LifecycleException e) {
                log.error("StandardPipeline.setBasic: stop", e);
            }
        }
        if (oldBasic instanceof Contained) {
            try {
                ((Contained) oldBasic).setContainer(null);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
        }
    }
    // Start the new component if necessary
    if (valve == null)
        return;
    if (valve instanceof Contained) {
        ((Contained) valve).setContainer(this.container);
    }
    if (getState().isAvailable() && valve instanceof Lifecycle) {
        try {
            ((Lifecycle) valve).start();
        } catch (LifecycleException e) {
            log.error("StandardPipeline.setBasic: start", e);
            return;
        }
    }
    // Update the pipeline
    Valve current = first;
    while (current != null) {
        if (current.getNext() == oldBasic) {
            current.setNext(valve);
            break;
        }
        current = current.getNext();
    }
    this.basic = valve;
}
Also used : Contained(org.apache.catalina.Contained) LifecycleException(org.apache.catalina.LifecycleException) Lifecycle(org.apache.catalina.Lifecycle) Valve(org.apache.catalina.Valve)

Example 4 with Lifecycle

use of org.apache.catalina.Lifecycle in project tomcat by apache.

the class StandardPipeline method startInternal.

/**
     * Start {@link Valve}s) in this pipeline and implement the requirements
     * of {@link LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
@Override
protected synchronized void startInternal() throws LifecycleException {
    // Start the Valves in our pipeline (including the basic), if any
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof Lifecycle)
            ((Lifecycle) current).start();
        current = current.getNext();
    }
    setState(LifecycleState.STARTING);
}
Also used : Lifecycle(org.apache.catalina.Lifecycle) Valve(org.apache.catalina.Valve)

Example 5 with Lifecycle

use of org.apache.catalina.Lifecycle in project tomcat by apache.

the class StandardHostSF method storeChildren.

/**
     * Store the specified Host properties and children
     * (Listener,Alias,Realm,Valve,Cluster, Context)
     *
     * @param aWriter
     *            PrintWriter to which we are storing
     * @param indent
     *            Number of spaces to indent this element
     * @param aHost
     *            Host whose properties are being stored
     *
     * @exception Exception
     *                if an exception occurs while storing
     */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aHost, StoreDescription parentDesc) throws Exception {
    if (aHost instanceof StandardHost) {
        StandardHost host = (StandardHost) aHost;
        // Store nested <Listener> elements
        LifecycleListener[] listeners = ((Lifecycle) host).findLifecycleListeners();
        storeElementArray(aWriter, indent, listeners);
        // Store nested <Alias> elements
        String[] aliases = host.findAliases();
        getStoreAppender().printTagArray(aWriter, "Alias", indent + 2, aliases);
        // Store nested <Realm> element
        Realm realm = host.getRealm();
        if (realm != null) {
            Realm parentRealm = null;
            if (host.getParent() != null) {
                parentRealm = host.getParent().getRealm();
            }
            if (realm != parentRealm) {
                storeElement(aWriter, indent, realm);
            }
        }
        // Store nested <Valve> elements
        Valve[] valves = host.getPipeline().getValves();
        if (valves != null && valves.length > 0) {
            List<Valve> hostValves = new ArrayList<>();
            for (int i = 0; i < valves.length; i++) {
                if (!(valves[i] instanceof ClusterValve))
                    hostValves.add(valves[i]);
            }
            storeElementArray(aWriter, indent, hostValves.toArray());
        }
        // store all <Cluster> elements
        Cluster cluster = host.getCluster();
        if (cluster != null) {
            Cluster parentCluster = null;
            if (host.getParent() != null) {
                parentCluster = host.getParent().getCluster();
            }
            if (cluster != parentCluster) {
                storeElement(aWriter, indent, cluster);
            }
        }
        // store all <Context> elements
        Container[] children = host.findChildren();
        storeElementArray(aWriter, indent, children);
    }
}
Also used : Lifecycle(org.apache.catalina.Lifecycle) ArrayList(java.util.ArrayList) Cluster(org.apache.catalina.Cluster) LifecycleListener(org.apache.catalina.LifecycleListener) ClusterValve(org.apache.catalina.ha.ClusterValve) Container(org.apache.catalina.Container) StandardHost(org.apache.catalina.core.StandardHost) ClusterValve(org.apache.catalina.ha.ClusterValve) Valve(org.apache.catalina.Valve) Realm(org.apache.catalina.Realm)

Aggregations

Lifecycle (org.apache.catalina.Lifecycle)25 LifecycleException (org.apache.catalina.LifecycleException)15 Realm (org.apache.catalina.Realm)10 Valve (org.apache.catalina.Valve)8 Container (org.apache.catalina.Container)7 Cluster (org.apache.catalina.Cluster)6 LifecycleListener (org.apache.catalina.LifecycleListener)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Lock (java.util.concurrent.locks.Lock)4 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)4 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)4 Contained (org.apache.catalina.Contained)4 Loader (org.apache.catalina.Loader)4 Manager (org.apache.catalina.Manager)4 WebappLoader (org.apache.catalina.loader.WebappLoader)4 StandardManager (org.apache.catalina.session.StandardManager)4 InstanceManager (org.apache.tomcat.InstanceManager)4 Notification (javax.management.Notification)3 ServletException (javax.servlet.ServletException)3