Java Code Examples for org.apache.catalina.Host#findChild()

The following examples show how to use org.apache.catalina.Host#findChild() . 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: 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 2
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 3
Source File: TestHostConfigAutomaticDeployment.java    From tomcatsrc 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 4
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Copy the specified WAR file to the Host's appBase and then call
 * {@link #addWebapp(String, String)} with the newly copied WAR. The WAR
 * will <b>NOT</b> be removed from the Host's appBase when the Tomcat
 * instance stops. Note that {@link ExpandWar} provides utility methods that
 * may be used to delete the WAR and/or expanded directory if required.
 *
 * @param contextPath   The context mapping to use, "" for root context.
 * @param source        The location from which the WAR should be copied
 *
 * @return The deployed Context
 *
 * @throws IOException If an I/O error occurs while copying the WAR file
 *                     from the specified URL to the appBase
 */
public Context addWebapp(String contextPath, URL source) throws IOException {

    ContextName cn = new ContextName(contextPath, null);

    // Make sure a conflicting web application has not already been deployed
    Host h = getHost();
    if (h.findChild(cn.getName()) != null) {
        throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictChild",
                source, contextPath, cn.getName()));
    }

    // Make sure appBase does not contain a conflicting web application
    File targetWar = new File(h.getAppBaseFile(), cn.getBaseName() + ".war");
    File targetDir = new File(h.getAppBaseFile(), cn.getBaseName());

    if (targetWar.exists()) {
        throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile",
                source, contextPath, targetWar.getAbsolutePath()));
    }
    if (targetDir.exists()) {
        throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile",
                source, contextPath, targetDir.getAbsolutePath()));
    }

    // Should be good to copy the WAR now
    URLConnection uConn = source.openConnection();

    try (InputStream is = uConn.getInputStream();
            OutputStream os = new FileOutputStream(targetWar)) {
        IOTools.flow(is, os);
    }

    return addWebapp(contextPath, targetWar.getAbsolutePath());
}
 
Example 5
Source File: VirtualHostClusterUtil.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public static void removeVirtualHost(String hostName) {
    Engine engine = DataHolder.getInstance().getCarbonTomcatService().getTomcat().getEngine();
    Host host = (Host) engine.findChild(hostName);
    Context context = (Context) host.findChild("/");
    try {
        if (host.getState().isAvailable()) {
            if (context != null && context.getAvailable()) {
                context.setRealm(null);
                context.stop();
                context.destroy();
                log.info("Unloaded webapp from the host: " + host
                        + " as the context of: " + context);
            }
            host.removeChild(context);
            host.setRealm(null);
            host.stop();
            host.destroy();
            engine.removeChild(host);
        }
    }catch (LifecycleException e) {
        log.error("error while removing host from tomcat", e);
    }
    URLMappingHolder.getInstance().removeUrlMappingMap(
            host.getName());
    log.info("Unloaded host from the engine: " + host);

}
 
Example 6
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Remove an existing Context.
 *
 * @param contextName MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeContext(String contextName) throws Exception {

    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(contextName);
    String domain = oname.getDomain();
    StandardService service = (StandardService) getService(oname);

    Engine engine = service.getContainer();
    String name = oname.getKeyProperty("name");
    name = name.substring(2);
    int i = name.indexOf('/');
    String hostName = name.substring(0,i);
    String path = name.substring(i);
    ObjectName deployer = new ObjectName(domain+":type=Deployer,host="+
                                         hostName);
    String pathStr = getPathStr(path);
    if(mserver.isRegistered(deployer)) {
        mserver.invoke(deployer,"addServiced",
                       new Object[]{pathStr},
                       new String[] {"java.lang.String"});
        mserver.invoke(deployer,"unmanageApp",
                       new Object[] {pathStr},
                       new String[] {"java.lang.String"});
        mserver.invoke(deployer,"removeServiced",
                       new Object[] {pathStr},
                       new String[] {"java.lang.String"});
    } else {
        log.warn("Deployer not found for "+hostName);
        Host host = (Host) engine.findChild(hostName);
        Context context = (Context) host.findChild(pathStr);
        // Remove this component from its parent component
        host.removeChild(context);
        if(context instanceof StandardContext)
        try {
            ((StandardContext)context).destroy();
        } catch (Exception e) {
            log.warn("Error during context [" + context.getName() + "] destroy ", e);
       }

    }

}
 
Example 7
Source File: MBeanFactory.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Remove an existing Context.
 *
 * @param contextName MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeContext(String contextName) throws Exception {

    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(contextName);
    String domain = oname.getDomain();
    StandardService service = (StandardService) getService(oname);

    Engine engine = (Engine) service.getContainer();
    String name = oname.getKeyProperty("name");
    name = name.substring(2);
    int i = name.indexOf('/');
    String hostName = name.substring(0,i);
    String path = name.substring(i);
    ObjectName deployer = new ObjectName(domain+":type=Deployer,host="+
                                         hostName);
    String pathStr = getPathStr(path);
    if(mserver.isRegistered(deployer)) {
        mserver.invoke(deployer,"addServiced",
                       new Object[]{pathStr},
                       new String[] {"java.lang.String"});
        mserver.invoke(deployer,"unmanageApp",
                       new Object[] {pathStr},
                       new String[] {"java.lang.String"});
        mserver.invoke(deployer,"removeServiced",
                       new Object[] {pathStr},
                       new String[] {"java.lang.String"});
    } else {
        log.warn("Deployer not found for "+hostName);
        Host host = (Host) engine.findChild(hostName);
        Context context = (Context) host.findChild(pathStr);
        // Remove this component from its parent component
        host.removeChild(context);
        if(context instanceof StandardContext)
        try {
            ((StandardContext)context).destroy();
        } catch (Exception e) {
            log.warn("Error during context [" + context.getName() + "] destroy ", e);
       }
   
    }

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

    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(contextName);
    String domain = oname.getDomain();
    StandardService service = (StandardService) getService(oname);

    Engine engine = (Engine) service.getContainer();
    String name = oname.getKeyProperty("name");
    name = name.substring(2);
    int i = name.indexOf('/');
    String hostName = name.substring(0,i);
    String path = name.substring(i);
    ObjectName deployer = new ObjectName(domain+":type=Deployer,host="+
                                         hostName);
    String pathStr = getPathStr(path);
    if(mserver.isRegistered(deployer)) {
        mserver.invoke(deployer,"addServiced",
                       new Object[]{pathStr},
                       new String[] {"java.lang.String"});
        mserver.invoke(deployer,"unmanageApp",
                       new Object[] {pathStr},
                       new String[] {"java.lang.String"});
        mserver.invoke(deployer,"removeServiced",
                       new Object[] {pathStr},
                       new String[] {"java.lang.String"});
    } else {
        log.warn("Deployer not found for "+hostName);
        Host host = (Host) engine.findChild(hostName);
        Context context = (Context) host.findChild(pathStr);
        // Remove this component from its parent component
        host.removeChild(context);
        if(context instanceof StandardContext)
        try {
            ((StandardContext)context).destroy();
        } catch (Exception e) {
            log.warn("Error during context [" + context.getName() + "] destroy ", e);
       }
   
    }

}