Java Code Examples for org.apache.catalina.Context#start()

The following examples show how to use org.apache.catalina.Context#start() . 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: HostConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void reload(DeployedApplication app, File fileToRemove, String newDocBase) {
    if(log.isInfoEnabled())
        log.info(sm.getString("hostConfig.reload", app.name));
    Context context = (Context) host.findChild(app.name);
    if (context.getState().isAvailable()) {
        if (fileToRemove != null && newDocBase != null) {
            context.addLifecycleListener(
                    new ExpandedDirectoryRemovalListener(fileToRemove, newDocBase));
        }
        // Reload catches and logs exceptions
        context.reload();
    } else {
        // If the context was not started (for example an error
        // in web.xml) we'll still get to try to start
        if (fileToRemove != null && newDocBase != null) {
            ExpandWar.delete(fileToRemove);
            context.setDocBase(newDocBase);
        }
        try {
            context.start();
        } catch (Exception e) {
            log.error(sm.getString("hostConfig.context.restart", app.name), e);
        }
    }
}
 
Example 2
Source File: HostConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void reload(DeployedApplication app, File fileToRemove, String newDocBase) {
    if(log.isInfoEnabled())
        log.info(sm.getString("hostConfig.reload", app.name));
    Context context = (Context) host.findChild(app.name);
    if (context.getState().isAvailable()) {
        if (fileToRemove != null && newDocBase != null) {
            context.addLifecycleListener(
                    new ExpandedDirectoryRemovalListener(fileToRemove, newDocBase));
        }
        // Reload catches and logs exceptions
        context.reload();
    } else {
        // If the context was not started (for example an error
        // in web.xml) we'll still get to try to start
        if (fileToRemove != null && newDocBase != null) {
            ExpandWar.delete(fileToRemove);
            context.setDocBase(newDocBase);
        }
        try {
            context.start();
        } catch (Exception e) {
            log.warn(sm.getString
                     ("hostConfig.context.restart", app.name), e);
        }
    }
}
 
Example 3
Source File: ManagerServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Start the web application at the specified context path.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to be started
 * @param smClient i18n support for current client's locale
 */
protected void start(PrintWriter writer, ContextName cn,
        StringManager smClient) {

    if (debug >= 1)
        log("start: Starting web application '" + cn + "'");

    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String displayPath = cn.getDisplayName();

    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext",
                    Escape.htmlElementContent(displayPath)));
            return;
        }
        context.start();
        if (context.getState().isAvailable())
            writer.println(smClient.getString("managerServlet.started",
                    displayPath));
        else
            writer.println(smClient.getString("managerServlet.startFailed",
                    displayPath));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        getServletContext().log(sm.getString("managerServlet.startFailed",
                displayPath), t);
        writer.println(smClient.getString("managerServlet.startFailed",
                displayPath));
        writer.println(smClient.getString("managerServlet.exception",
                t.toString()));
    }

}
 
Example 4
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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");
    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());
}
 
Example 5
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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");
    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());
}
 
Example 6
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
        fail();
    } catch (LifecycleException ex) {
        // As expected
    }
    assertEquals(LifecycleState.FAILED, context.getState());

    // The second attempt
    listener.setFail(false);
    context.start();
    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");
    assertEquals("Target", result.toString());
}
 
Example 7
Source File: ManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Start the web application at the specified context path.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to be started
 */
protected void start(PrintWriter writer, ContextName cn,
        StringManager smClient) {

    if (debug >= 1)
        log("start: Starting web application '" + cn + "'");

    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String displayPath = cn.getDisplayName();

    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext", 
                    RequestUtil.filter(displayPath)));
            return;
        }
        context.start();
        if (context.getState().isAvailable())
            writer.println(smClient.getString("managerServlet.started",
                    displayPath));
        else
            writer.println(smClient.getString("managerServlet.startFailed",
                    displayPath));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        getServletContext().log(sm.getString("managerServlet.startFailed",
                displayPath), t);
        writer.println(smClient.getString("managerServlet.startFailed",
                displayPath));
        writer.println(smClient.getString("managerServlet.exception",
                t.toString()));
    }

}
 
Example 8
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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();
        fail();
    } catch (LifecycleException ex) {
        // As expected
    }
    assertEquals(LifecycleState.FAILED, context.getState());

    // The second attempt
    loader.setFail(false);
    context.start();
    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");
    assertEquals("Target", result.toString());
}
 
Example 9
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@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();
        fail();
    } catch (LifecycleException ex) {
        // As expected
    }
    assertEquals(LifecycleState.FAILED, context.getState());

    // The second attempt
    loader.setFail(false);
    context.start();
    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");
    assertEquals("Target", result.toString());
}
 
Example 10
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@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();
        fail();
    } catch (LifecycleException ex) {
        // As expected
    }
    assertEquals(LifecycleState.FAILED, context.getState());

    // The second attempt
    listener.setFail(false);
    context.start();
    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");
    assertEquals("Target", result.toString());
}
 
Example 11
Source File: ManagerServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Start the web application at the specified context path.
 *
 * @param writer Writer to render to
 * @param cn Name of the application to be started
 */
protected void start(PrintWriter writer, ContextName cn,
        StringManager smClient) {

    if (debug >= 1)
        log("start: Starting web application '" + cn + "'");

    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String displayPath = cn.getDisplayName();

    try {
        Context context = (Context) host.findChild(cn.getName());
        if (context == null) {
            writer.println(smClient.getString("managerServlet.noContext", 
                    RequestUtil.filter(displayPath)));
            return;
        }
        context.start();
        if (context.getState().isAvailable())
            writer.println(smClient.getString("managerServlet.started",
                    displayPath));
        else
            writer.println(smClient.getString("managerServlet.startFailed",
                    displayPath));
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        getServletContext().log(sm.getString("managerServlet.startFailed",
                displayPath), t);
        writer.println(smClient.getString("managerServlet.startFailed",
                displayPath));
        writer.println(smClient.getString("managerServlet.exception",
                t.toString()));
    }

}
 
Example 12
Source File: TestELInterpreterFactory.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug54239() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    tomcat.start();

    ServletContext context = ctx.getServletContext();

    ELInterpreter interpreter =
            ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof DefaultELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    context.setAttribute(ELInterpreter.class.getName(),
            SimpleELInterpreter.class.getName());
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    SimpleELInterpreter simpleInterpreter = new SimpleELInterpreter();
    context.setAttribute(ELInterpreter.class.getName(), simpleInterpreter);
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
    Assert.assertTrue(interpreter == simpleInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    ctx.stop();
    ctx.addApplicationListener(Bug54239Listener.class.getName());
    ctx.start();

    interpreter = ELInterpreterFactory.getELInterpreter(ctx.getServletContext());
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
}
 
Example 13
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug46243() throws Exception {
    // This tests that if a Filter init() fails then the web application
    // is not put into service. (BZ 46243)
    // This also tests that 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();

    File docBase = new File(tomcat.getHost().getAppBase(), "ROOT");
    if (!docBase.mkdirs() && !docBase.isDirectory()) {
        fail("Unable to create docBase");
    }

    Context root = tomcat.addContext("", "ROOT");
    configureTest46243Context(root, true);
    tomcat.start();

    // Configure the client
    Bug46243Client client =
            new Bug46243Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { REQUEST });

    client.connect();
    client.processRequest();
    assertTrue(client.isResponse404());

    // Context failed to start. This checks that automatic transition
    // from FAILED to STOPPED state was successful.
    assertEquals(LifecycleState.STOPPED, root.getState());

    // Prepare context for the second attempt
    // Configuration was cleared on stop() thanks to
    // StandardContext.resetContext(), so we need to configure it again
    // from scratch.
    configureTest46243Context(root, false);
    root.start();
    // The same request is processed successfully
    client.connect();
    client.processRequest();
    assertTrue(client.isResponse200());
    assertEquals(Bug46243Filter.class.getName()
            + HelloWorldServlet.RESPONSE_TEXT, client.getResponseBody());
}
 
Example 14
Source File: TestELInterpreterFactory.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug54239() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    tomcat.start();

    ServletContext context = ctx.getServletContext();

    ELInterpreter interpreter =
            ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof DefaultELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    context.setAttribute(ELInterpreter.class.getName(),
            SimpleELInterpreter.class.getName());
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    SimpleELInterpreter simpleInterpreter = new SimpleELInterpreter();
    context.setAttribute(ELInterpreter.class.getName(), simpleInterpreter);
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
    Assert.assertTrue(interpreter == simpleInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    ctx.stop();
    ctx.addApplicationListener(Bug54239Listener.class.getName());
    ctx.start();

    interpreter = ELInterpreterFactory.getELInterpreter(ctx.getServletContext());
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
}
 
Example 15
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug46243() throws Exception {
    // This tests that if a Filter init() fails then the web application
    // is not put into service. (BZ 46243)
    // This also tests that 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();

    File docBase = new File(tomcat.getHost().getAppBase(), "ROOT");
    if (!docBase.mkdirs() && !docBase.isDirectory()) {
        fail("Unable to create docBase");
    }

    Context root = tomcat.addContext("", "ROOT");
    configureTest46243Context(root, true);
    tomcat.start();

    // Configure the client
    Bug46243Client client =
            new Bug46243Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { REQUEST });

    client.connect();
    client.processRequest();
    assertTrue(client.isResponse404());

    // Context failed to start. This checks that automatic transition
    // from FAILED to STOPPED state was successful.
    assertEquals(LifecycleState.STOPPED, root.getState());

    // Prepare context for the second attempt
    // Configuration was cleared on stop() thanks to
    // StandardContext.resetContext(), so we need to configure it again
    // from scratch.
    configureTest46243Context(root, false);
    root.start();
    // The same request is processed successfully
    client.connect();
    client.processRequest();
    assertTrue(client.isResponse200());
    assertEquals(Bug46243Filter.class.getName()
            + HelloWorldServlet.RESPONSE_TEXT, client.getResponseBody());
}
 
Example 16
Source File: TestELInterpreterFactory.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBug54239() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    tomcat.start();

    ServletContext context = ctx.getServletContext();

    ELInterpreter interpreter =
            ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof DefaultELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    context.setAttribute(ELInterpreter.class.getName(),
            SimpleELInterpreter.class.getName());
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    SimpleELInterpreter simpleInterpreter = new SimpleELInterpreter();
    context.setAttribute(ELInterpreter.class.getName(), simpleInterpreter);
    interpreter = ELInterpreterFactory.getELInterpreter(context);
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
    Assert.assertTrue(interpreter == simpleInterpreter);

    context.removeAttribute(ELInterpreter.class.getName());

    ctx.stop();
    ctx.addApplicationListener(Bug54239Listener.class.getName());
    ctx.start();

    interpreter = ELInterpreterFactory.getELInterpreter(ctx.getServletContext());
    Assert.assertNotNull(interpreter);
    Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
}
 
Example 17
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBug46243() throws Exception {
    // This tests that if a Filter init() fails then the web application
    // is not put into service. (BZ 46243)
    // This also tests that 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();

    File docBase = new File(tomcat.getHost().getAppBaseFile(), "ROOT");
    if (!docBase.mkdirs() && !docBase.isDirectory()) {
        Assert.fail("Unable to create docBase");
    }

    Context root = tomcat.addContext("", "ROOT");
    configureTest46243Context(root, true);
    tomcat.start();

    // Configure the client
    Bug46243Client client =
            new Bug46243Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { REQUEST });

    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse404());

    // Context failed to start. This checks that automatic transition
    // from FAILED to STOPPED state was successful.
    Assert.assertEquals(LifecycleState.STOPPED, root.getState());

    // Prepare context for the second attempt
    // Configuration was cleared on stop() thanks to
    // StandardContext.resetContext(), so we need to configure it again
    // from scratch.
    configureTest46243Context(root, false);
    root.start();
    // The same request is processed successfully
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    Assert.assertEquals(Bug46243Filter.class.getName()
            + HelloWorldServlet.RESPONSE_TEXT, client.getResponseBody());
}