org.apache.catalina.core.StandardHost Java Examples

The following examples show how to use org.apache.catalina.core.StandardHost. 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: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static boolean shouldNotDeploy(final StandardContext standardContext) {
    if (StandardHost.class.isInstance(standardContext.getParent())) {
        final StandardHost host = StandardHost.class.cast(standardContext.getParent());
        if (host.getAutoDeploy() && standardContext.getDocBase() != null && 
                standardContext.getDocBase() != null &&
                new File(host.getAppBaseFile(), standardContext.getDocBase()).isDirectory() && (
                new File(host.getAppBaseFile(), standardContext.getDocBase() + ".ear").exists() ||
                new File(host.getAppBaseFile(), standardContext.getDocBase() + ".rar").exists())
        ) {

            LOGGER.info(String.format("Not deploying exploded directory %s as Java EE artifact exists which will be deployed.",
                    new File(host.getAppBaseFile(), standardContext.getPath()).getAbsolutePath()));

            return true;
        }
    }
    return false;
}
 
Example #2
Source File: TestHostConfigAutomaticDeployment.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void testBrokenAppWithAntiLocking(boolean unpackWARs)
        throws Exception {

    Tomcat tomcat = getTomcatInstance();
    StandardHost host = (StandardHost) tomcat.getHost();

    host.setUnpackWARs(unpackWARs);

    File war = createWar(WAR_BROKEN_SOURCE, false);
    createXmlInConfigBaseForExternal(war, true);

    File dir = new File(getAppBaseFile(host), APP_NAME.getBaseName());

    tomcat.start();

    // Simulate deploy on start-up
    tomcat.getHost().backgroundProcess();

    Assert.assertTrue(war.isFile());
    if (unpackWARs) {
        Assert.assertTrue(dir.isDirectory());
    }
}
 
Example #3
Source File: TestHostConfigAutomaticDeployment.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testSetContextClassName() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        StandardHost standardHost = (StandardHost) host;
        standardHost.setContextClass(TesterContext.class.getName());
    }

    // Copy the WAR file
    File war = new File(host.getAppBaseFile(),
            APP_NAME.getBaseName() + ".war");
    Files.copy(WAR_XML_SOURCE.toPath(), war.toPath());

    // Deploy the copied war
    tomcat.start();
    host.backgroundProcess();

    // Check the Context class
    Context ctxt = (Context) host.findChild(APP_NAME.getName());

    Assert.assertTrue(ctxt instanceof TesterContext);
}
 
Example #4
Source File: TestHostConfigAutomaticDeployment.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetContextClassName() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        StandardHost standardHost = (StandardHost) host;
        standardHost.setContextClass(TesterContext.class.getName());
    }

    // Copy the WAR file
    File war = new File(getAppBaseFile(host),
            APP_NAME.getBaseName() + ".war");
    copy(WAR_XML_SOURCE, war);

    // Deploy the copied war
    tomcat.start();
    host.backgroundProcess();

    // Check the Context class
    Context ctxt = (Context) host.findChild(APP_NAME.getName());

    Assert.assertTrue(ctxt instanceof TesterContext);
}
 
Example #5
Source File: TestHostConfigAutomaticDeployment.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void testBrokenAppWithAntiLocking(boolean unpackWARs)
        throws Exception {

    Tomcat tomcat = getTomcatInstance();
    StandardHost host = (StandardHost) tomcat.getHost();

    host.setUnpackWARs(unpackWARs);

    File war = createWar(WAR_BROKEN_SOURCE, false);
    createXmlInConfigBaseForExternal(war, true);

    File dir = new File(host.getAppBaseFile(), APP_NAME.getBaseName());

    tomcat.start();

    // Simulate deploy on start-up
    tomcat.getHost().backgroundProcess();

    Assert.assertTrue(war.isFile());
    if (unpackWARs) {
        Assert.assertTrue(dir.isDirectory());
    }
}
 
Example #6
Source File: TestHostConfigAutomaticDeployment.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void doTestUnpackWAR(boolean unpackWARs, boolean unpackWAR,
        boolean external, boolean resultDir) throws Exception {

    Tomcat tomcat = getTomcatInstance();
    StandardHost host = (StandardHost) tomcat.getHost();

    host.setUnpackWARs(unpackWARs);

    tomcat.start();

    File war;
    if (unpackWAR) {
        war = createWar(WAR_XML_UNPACKWAR_TRUE_SOURCE, !external);
    } else {
        war = createWar(WAR_XML_UNPACKWAR_FALSE_SOURCE, !external);
    }
    if (external) {
        createXmlInConfigBaseForExternal(war);
    }

    host.backgroundProcess();

    File dir = new File(host.getAppBase(), APP_NAME.getBaseName());
    Assert.assertEquals(
            Boolean.valueOf(resultDir), Boolean.valueOf(dir.isDirectory()));
}
 
Example #7
Source File: TestAbstractArchiveResource.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testNestedJarGetURL() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File docBase = new File("test/webresources/war-url-connection.war");
    Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
    skipTldsForResourceJars(ctx);

    ((StandardHost) tomcat.getHost()).setUnpackWARs(false);

    tomcat.start();

    WebResource webResource =
            ctx.getResources().getClassLoaderResource("/META-INF/resources/index.html");

    StringBuilder expectedURL = new StringBuilder("jar:war:");
    expectedURL.append(docBase.getAbsoluteFile().toURI().toURL().toString());
    expectedURL.append("*/WEB-INF/lib/test.jar!/META-INF/resources/index.html");

    Assert.assertEquals(expectedURL.toString(), webResource.getURL().toString());
}
 
Example #8
Source File: TestAbstractArchiveResource.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testJarGetURL() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File docBase = new File("test/webapp");
    Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
    skipTldsForResourceJars(ctx);

    ((StandardHost) tomcat.getHost()).setUnpackWARs(false);

    tomcat.start();

    WebResource webResource =
            ctx.getResources().getClassLoaderResource("/META-INF/tags/echo.tag");

    StringBuilder expectedURL = new StringBuilder("jar:");
    expectedURL.append(docBase.getAbsoluteFile().toURI().toURL().toString());
    expectedURL.append("WEB-INF/lib/test-lib.jar!/META-INF/tags/echo.tag");

    Assert.assertEquals(expectedURL.toString(), webResource.getURL().toString());
}
 
Example #9
Source File: TestTomcat.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBrokenContextPerAddContext() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass("InvalidContextClassName");
    }

    // No file system docBase required
    try {
        tomcat.addContext(null, "", null);
        fail();
    } catch (IllegalArgumentException e) {
        // OK
    }
}
 
Example #10
Source File: TestTomcat.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCustomContextPerAddWebappWithHost() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class
                .getName());
    }

    File appFile = new File("test/deployment/context.war");
    org.apache.catalina.Context context = tomcat.addWebapp(host, "/test",
            appFile.getAbsolutePath());

    assertEquals(ReplicatedContext.class.getName(), context.getClass()
            .getName());
}
 
Example #11
Source File: TestReplicatedContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }

    File root = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());

    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMapping("/access", "test");

    tomcat.start();

    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");

    Assert.assertEquals("OK", result.toString());

}
 
Example #12
Source File: TestTomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testGetBrokenContextPerAddContext() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass("InvalidContextClassName");
    }

    // No file system docBase required
    try {
        tomcat.addContext(null, "", null);
        Assert.fail();
    } catch (IllegalArgumentException e) {
        // OK
    }
}
 
Example #13
Source File: TestTomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testGetCustomContextPerAddWebappWithHost() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class
                .getName());
    }

    File appFile = new File("test/deployment/context.war");
    Context context = tomcat.addWebapp(host, "/test",
            appFile.getAbsolutePath());

    Assert.assertEquals(ReplicatedContext.class.getName(), context.getClass()
            .getName());
}
 
Example #14
Source File: TestTomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testGetBrokenContextPerAddWepapp() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass("InvalidContextClassName");
    }

    try {
        File appFile = new File("test/deployment/context.war");
        tomcat.addWebapp(null, "/test", appFile.getAbsolutePath());
        Assert.fail();
    } catch (IllegalArgumentException e) {
        // OK
    }
}
 
Example #15
Source File: TestReplicatedContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }

    File root = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());

    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMapping("/access", "test");

    tomcat.start();

    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");

    Assert.assertEquals("OK", result.toString());

}
 
Example #16
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Create the configured {@link Context} for the given <code>host</code>.
 * The default constructor of the class that was configured with
 * {@link StandardHost#setContextClass(String)} will be used
 *
 * @param host
 *            host for which the {@link Context} should be created, or
 *            <code>null</code> if default host should be used
 * @param url
 *            path of the webapp which should get the {@link Context}
 * @return newly created {@link Context}
 */
private Context createContext(Host host, String url) {
    String contextClass = StandardContext.class.getName();
    if (host == null) {
        host = this.getHost();
    }
    if (host instanceof StandardHost) {
        contextClass = ((StandardHost) host).getContextClass();
    }
    try {
        return (Context) Class.forName(contextClass).getConstructor()
                .newInstance();
    } catch (InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException
            | NoSuchMethodException | SecurityException
            | ClassNotFoundException e) {
        throw new IllegalArgumentException(
                "Can't instantiate context-class " + contextClass
                        + " for host " + host + " and url "
                        + url, e);
    }
}
 
Example #17
Source File: TestTomcat.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBrokenContextPerAddWepapp() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass("InvalidContextClassName");
    }

    try {
        File appFile = new File("test/deployment/context.war");
        tomcat.addWebapp(null, "/test", appFile.getAbsolutePath());
        fail();
    } catch (IllegalArgumentException e) {
        // OK
    }
}
 
Example #18
Source File: TestTomcat.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBrokenContextPerAddContext() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass("InvalidContextClassName");
    }

    // No file system docBase required
    try {
        tomcat.addContext(null, "", null);
        fail();
    } catch (IllegalArgumentException e) {
        // OK
    }
}
 
Example #19
Source File: HostUtil.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
/**
 * add host to engine.
 * 
 * @param hostName
 *            name of the host
 * @return will return the added host of Engine
 */
public static Host addHostToEngine(String hostName) {
	String hostBaseDir = CarbonUtils.getCarbonRepository() + "/"
			+ UrlMapperConstants.HostProperties.WEB_APPS + "/";
	CarbonTomcatService carbonTomcatService = DataHolder.getInstance().getCarbonTomcatService();
	// adding virtual host to tomcat engine
	Engine engine = carbonTomcatService.getTomcat().getEngine();
	StandardHost host = new StandardHost();
	host.setAppBase(hostBaseDir);
	host.setName(hostName);
	host.setUnpackWARs(false);
	host.addValve(new CarbonContextCreatorValve());
	host.addValve(new CompositeValve());
	engine.addChild(host);
	log.info("host added to the tomcat: " + host);
	return host;
}
 
Example #20
Source File: StoreContextAppender.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Print Context Values. <ul><li> Special handling to default workDir.
 * </li><li> Don't save path at external context.xml </li><li> Don't
 * generate docBase for host.appBase webapps <LI></ul>
 *
 * @see org.apache.catalina.storeconfig.StoreAppender#isPrintValue(java.lang.Object,
 *      java.lang.Object, java.lang.String,
 *      org.apache.catalina.storeconfig.StoreDescription)
 */
@Override
public boolean isPrintValue(Object bean, Object bean2, String attrName,
        StoreDescription desc) {
    boolean isPrint = super.isPrintValue(bean, bean2, attrName, desc);
    if (isPrint) {
        StandardContext context = ((StandardContext) bean);
        if ("workDir".equals(attrName)) {
            String defaultWorkDir = getDefaultWorkDir(context);
            isPrint = !defaultWorkDir.equals(context.getWorkDir());
        } else if ("path".equals(attrName)) {
            isPrint = desc.isStoreSeparate()
                        && desc.isExternalAllowed()
                        && context.getConfigFile() == null;
        } else if ("docBase".equals(attrName)) {
            Container host = context.getParent();
            if (host instanceof StandardHost) {
                File appBase = getAppBase(((StandardHost) host));
                File docBase = getDocBase(context,appBase);
                isPrint = !appBase.equals(docBase.getParentFile());
            }
        }
    }
    return isPrint;
}
 
Example #21
Source File: StoreContextAppender.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Make default Work Dir.
 *
 * @param context The context
 * @return The default working directory for the context.
 */
protected String getDefaultWorkDir(StandardContext context) {
    String defaultWorkDir = null;
    String contextWorkDir = context.getName();
    if (contextWorkDir.length() == 0)
        contextWorkDir = "_";
    if (contextWorkDir.startsWith("/"))
        contextWorkDir = contextWorkDir.substring(1);

    Container host = context.getParent();
    if (host instanceof StandardHost) {
        String hostWorkDir = ((StandardHost) host).getWorkDir();
        if (hostWorkDir != null) {
            defaultWorkDir = hostWorkDir + File.separator + contextWorkDir;
        } else {
            String engineName = context.getParent().getParent().getName();
            String hostName = context.getParent().getName();
            defaultWorkDir = "work" + File.separator + engineName
                    + File.separator + hostName + File.separator
                    + contextWorkDir;
        }
    }
    return defaultWorkDir;
}
 
Example #22
Source File: TestReplicatedContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }

    File root = new File("test/webapp");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());

    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMappingDecoded("/access", "test");

    tomcat.start();

    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");

    Assert.assertEquals("OK", result.toString());

}
 
Example #23
Source File: TestTomcat.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCustomContextPerAddWebappWithNullHost() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class
                .getName());
    }

    File appFile = new File("test/deployment/context.war");
    Context context = tomcat.addWebapp(null, "/test",
            appFile.getAbsolutePath());

    assertEquals(ReplicatedContext.class.getName(), context.getClass()
            .getName());
}
 
Example #24
Source File: TestTomcat.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBrokenContextPerAddWepapp() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass("InvalidContextClassName");
    }

    try {
        File appFile = new File("test/deployment/context.war");
        tomcat.addWebapp(null, "/test", appFile.getAbsolutePath());
        fail();
    } catch (IllegalArgumentException e) {
        // OK
    }
}
 
Example #25
Source File: TestTomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testGetCustomContextPerAddWebappWithNullHost() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class
                .getName());
    }

    File appFile = new File("test/deployment/context.war");
    Context context = tomcat.addWebapp(null, "/test",
            appFile.getAbsolutePath());

    Assert.assertEquals(ReplicatedContext.class.getName(), context.getClass()
            .getName());
}
 
Example #26
Source File: TestTomcat.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCustomContextPerAddContextWithHost() {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class
                .getName());
    }

    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext(host, "", null);
    assertEquals(ReplicatedContext.class.getName(), ctx.getClass()
            .getName());
}
 
Example #27
Source File: GlobalListenerSupport.java    From tomee with Apache License 2.0 5 votes vote down vote up
private static boolean hasChild(final StandardHost host, final String name) {
    for (final Container child : host.findChildren()) {
        // the TomEERemoteWebapp path = "/" + name
        if (name.equals(child.getName())
            || (StandardContext.class.isInstance(child) && ("/" + name).equals(StandardContext.class.cast(child).getPath()))) {
            return true;
        }
    }
    return false;
}
 
Example #28
Source File: ManagerServlet.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Find potential memory leaks caused by web application reload.
 */
protected void findleaks(boolean statusLine, PrintWriter writer,
        StringManager smClient) {
    
    if (!(host instanceof StandardHost)) {
        writer.println(smClient.getString("managerServlet.findleaksFail"));
        return;
    }
    
    String[] results =
        ((StandardHost) host).findReloadedContextMemoryLeaks();
    
    if (results.length > 0) {
        if (statusLine) {
            writer.println(
                    smClient.getString("managerServlet.findleaksList"));
        }
        for (String result : results) {
            if ("".equals(result)) {
                result = "/";
            }
            writer.println(result);
        }
    } else if (statusLine) {
        writer.println(smClient.getString("managerServlet.findleaksNone"));
    }
}
 
Example #29
Source File: JFishTomcat.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public Context addWebapp(Host host, String contextPath, String docBase, ContextConfig config) {
if(hack()){
       if (host instanceof StandardHost) {
           ((StandardHost) host).setContextClass(contextClass.getName());
       }
}
Context ctx = super.addWebapp(host, contextPath, docBase, config);
return ctx;

  }
 
Example #30
Source File: MBeanFactory.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new StandardHost.
 *
 * @param parent MBean Name of the associated parent component
 * @param name Unique name of this Host
 * @param appBase Application base directory name
 * @param autoDeploy Should we auto deploy?
 * @param deployOnStartup Deploy on server startup?
 * @param deployXML Should we deploy Context XML config files property?
 * @param unpackWARs Should we unpack WARs when auto deploying?
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createStandardHost(String parent, String name,
                                 String appBase,
                                 boolean autoDeploy,
                                 boolean deployOnStartup,
                                 boolean deployXML,                                       
                                 boolean unpackWARs)
    throws Exception {

    // Create a new StandardHost instance
    StandardHost host = new StandardHost();
    host.setName(name);
    host.setAppBase(appBase);
    host.setAutoDeploy(autoDeploy);
    host.setDeployOnStartup(deployOnStartup);
    host.setDeployXML(deployXML);
    host.setUnpackWARs(unpackWARs);

    // add HostConfig for active reloading
    HostConfig hostConfig = new HostConfig();
    host.addLifecycleListener(hostConfig);

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Service service = getService(pname);
    Engine engine = (Engine) service.getContainer();
    engine.addChild(host);

    // Return the corresponding MBean name
    return (host.getObjectName().toString());

}