Search in sources :

Example 21 with LifecycleException

use of org.apache.catalina.LifecycleException in project tomcat70 by apache.

the class ContainerBase method stopInternal.

/**
 * Stop this component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void stopInternal() throws LifecycleException {
    // Stop our thread
    threadStop();
    setState(LifecycleState.STOPPING);
    // Stop the Valves in our pipeline (including the basic), if any
    if (pipeline instanceof Lifecycle && ((Lifecycle) pipeline).getState().isAvailable()) {
        ((Lifecycle) pipeline).stop();
    }
    // Stop our child containers, if any
    Container[] children = findChildren();
    List<Future<Void>> results = new ArrayList<Future<Void>>();
    for (int i = 0; i < children.length; i++) {
        results.add(startStopExecutor.submit(new StopChild(children[i])));
    }
    boolean fail = false;
    for (Future<Void> result : results) {
        try {
            result.get();
        } catch (Exception e) {
            log.error(sm.getString("containerBase.threadedStopFailed"), e);
            fail = true;
        }
    }
    if (fail) {
        throw new LifecycleException(sm.getString("containerBase.threadedStopFailed"));
    }
    // Stop our subordinate components, if any
    DirContext resources = getResourcesInternal();
    if ((resources != null) && (resources instanceof Lifecycle)) {
        ((Lifecycle) resources).stop();
    }
    Realm realm = getRealmInternal();
    if ((realm != null) && (realm instanceof Lifecycle)) {
        ((Lifecycle) realm).stop();
    }
    Cluster cluster = getClusterInternal();
    if ((cluster != null) && (cluster instanceof Lifecycle)) {
        ((Lifecycle) cluster).stop();
    }
    Manager manager = getManagerInternal();
    if ((manager != null) && (manager instanceof Lifecycle) && ((Lifecycle) manager).getState().isAvailable()) {
        ((Lifecycle) manager).stop();
    }
    Loader loader = getLoaderInternal();
    if ((loader != null) && (loader instanceof Lifecycle)) {
        ((Lifecycle) loader).stop();
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) Lifecycle(org.apache.catalina.Lifecycle) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Cluster(org.apache.catalina.Cluster) Loader(org.apache.catalina.Loader) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) ServletException(javax.servlet.ServletException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) Container(org.apache.catalina.Container) Future(java.util.concurrent.Future) Realm(org.apache.catalina.Realm)

Example 22 with LifecycleException

use of org.apache.catalina.LifecycleException in project tomcat70 by apache.

the class ContainerBase method setLoader.

/**
 * Set the Loader with which this Container is associated.
 *
 * @param loader The newly associated loader
 */
@Override
public void setLoader(Loader loader) {
    Lock writeLock = loaderLock.writeLock();
    writeLock.lock();
    Loader oldLoader = null;
    try {
        // Change components if necessary
        oldLoader = this.loader;
        if (oldLoader == loader)
            return;
        this.loader = loader;
        // Stop the old component if necessary
        if (getState().isAvailable() && (oldLoader != null) && (oldLoader instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldLoader).stop();
            } catch (LifecycleException e) {
                log.error("ContainerBase.setLoader: stop: ", e);
            }
        }
        // Start the new component if necessary
        if (loader != null)
            loader.setContainer(this);
        if (getState().isAvailable() && (loader != null) && (loader instanceof Lifecycle)) {
            try {
                ((Lifecycle) loader).start();
            } catch (LifecycleException e) {
                log.error("ContainerBase.setLoader: start: ", e);
            }
        }
    } finally {
        writeLock.unlock();
    }
    // Report this property change to interested listeners
    support.firePropertyChange("loader", oldLoader, loader);
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) Lifecycle(org.apache.catalina.Lifecycle) Loader(org.apache.catalina.Loader) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 23 with LifecycleException

use of org.apache.catalina.LifecycleException in project tomcat70 by apache.

the class ContainerBase method setCluster.

/**
 * Set the Cluster with which this Container is associated.
 *
 * @param cluster The newly associated Cluster
 */
@Override
public void setCluster(Cluster cluster) {
    Cluster oldCluster = null;
    Lock writeLock = clusterLock.writeLock();
    writeLock.lock();
    try {
        // Change components if necessary
        oldCluster = this.cluster;
        if (oldCluster == cluster)
            return;
        this.cluster = cluster;
        // Stop the old component if necessary
        if (getState().isAvailable() && (oldCluster != null) && (oldCluster instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldCluster).stop();
            } catch (LifecycleException e) {
                log.error("ContainerBase.setCluster: stop: ", e);
            }
        }
        // Start the new component if necessary
        if (cluster != null)
            cluster.setContainer(this);
        if (getState().isAvailable() && (cluster != null) && (cluster instanceof Lifecycle)) {
            try {
                ((Lifecycle) cluster).start();
            } catch (LifecycleException e) {
                log.error("ContainerBase.setCluster: start: ", e);
            }
        }
    } finally {
        writeLock.unlock();
    }
    // Report this property change to interested listeners
    support.firePropertyChange("cluster", oldCluster, cluster);
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) Lifecycle(org.apache.catalina.Lifecycle) Cluster(org.apache.catalina.Cluster) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 24 with LifecycleException

use of org.apache.catalina.LifecycleException in project tomcat70 by apache.

the class TestStandardContext method testWebappListenerConfigureFail.

@Test
public void testWebappListenerConfigureFail() throws Exception {
    // Test that if LifecycleListener on webapp fails during
    // configure_start event and if the cause of the failure is gone,
    // the context can be started without a need to redeploy it.
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    tomcat.start();
    // To not start Context automatically, as we have to configure it first
    ((ContainerBase) tomcat.getHost()).setStartChildren(false);
    FailingLifecycleListener listener = new FailingLifecycleListener();
    File root = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp("", root.getAbsolutePath());
    context.addLifecycleListener(listener);
    try {
        context.start();
        Assert.fail();
    } catch (LifecycleException ex) {
    // As expected
    }
    Assert.assertEquals(LifecycleState.FAILED, context.getState());
    // The second attempt
    listener.setFail(false);
    context.start();
    Assert.assertEquals(LifecycleState.STARTED, context.getState());
    // Using a test from testBug49922() to check that the webapp is running
    ByteChunk result = getUrl("http://localhost:" + getPort() + "/bug49922/target");
    Assert.assertEquals("Target", result.toString());
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Tomcat(org.apache.catalina.startup.Tomcat) LifecycleException(org.apache.catalina.LifecycleException) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 25 with LifecycleException

use of org.apache.catalina.LifecycleException in project tomcat70 by apache.

the class TestStandardContext method testWebappLoaderStartFail.

@Test
public void testWebappLoaderStartFail() throws Exception {
    // Test that if WebappLoader start() fails and if the cause of
    // the failure is gone, the context can be started without
    // a need to redeploy it.
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    tomcat.start();
    // To not start Context automatically, as we have to configure it first
    ((ContainerBase) tomcat.getHost()).setStartChildren(false);
    FailingWebappLoader loader = new FailingWebappLoader();
    File root = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp("", root.getAbsolutePath());
    context.setLoader(loader);
    try {
        context.start();
        Assert.fail();
    } catch (LifecycleException ex) {
    // As expected
    }
    Assert.assertEquals(LifecycleState.FAILED, context.getState());
    // The second attempt
    loader.setFail(false);
    context.start();
    Assert.assertEquals(LifecycleState.STARTED, context.getState());
    // Using a test from testBug49922() to check that the webapp is running
    ByteChunk result = getUrl("http://localhost:" + getPort() + "/bug49922/target");
    Assert.assertEquals("Target", result.toString());
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Tomcat(org.apache.catalina.startup.Tomcat) LifecycleException(org.apache.catalina.LifecycleException) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

LifecycleException (org.apache.catalina.LifecycleException)128 Lifecycle (org.apache.catalina.Lifecycle)36 IOException (java.io.IOException)29 Container (org.apache.catalina.Container)19 NamingException (javax.naming.NamingException)18 File (java.io.File)17 Realm (org.apache.catalina.Realm)16 MalformedURLException (java.net.MalformedURLException)15 ServletException (javax.servlet.ServletException)12 ArrayList (java.util.ArrayList)9 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)9 Manager (org.apache.catalina.Manager)9 Valve (org.apache.catalina.Valve)9 Tomcat (org.apache.catalina.startup.Tomcat)9 Lock (java.util.concurrent.locks.Lock)8 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)8 Cluster (org.apache.catalina.Cluster)8 Loader (org.apache.catalina.Loader)8 Server (org.apache.catalina.Server)8 Contained (org.apache.catalina.Contained)7