Java Code Examples for org.apache.catalina.core.StandardHost#setName()

The following examples show how to use org.apache.catalina.core.StandardHost#setName() . 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: TomcatMetricsTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
void runTomcat(HttpServlet servlet, Callable<Void> doWithTomcat) throws Exception {
    Tomcat server = new Tomcat();
    try {
        StandardHost host = new StandardHost();
        host.setName("localhost");
        server.setHost(host);
        server.setPort(0);
        server.start();

        this.port = server.getConnector().getLocalPort();

        Context context = server.addContext("", null);
        server.addServlet("", "servletname", servlet);
        context.addServletMappingDecoded("/", "servletname");

        doWithTomcat.call();

    } finally {
        server.stop();
        server.destroy();

    }
}
 
Example 2
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 3
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 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?
 * @return the object name of the created host
 *
 * @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 = service.getContainer();
    engine.addChild(host);

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

}
 
Example 4
Source File: MBeanFactory.java    From Tomcat7.0.67 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());

}
 
Example 5
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());

}
 
Example 6
Source File: VirtualHostClusterUtil.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public static Host addHostToEngine(String hostName) {
    String hostBaseDir = CarbonUtils.getCarbonRepository() + "/webapps/";
    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 7
Source File: TestRegistration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testMBeanDeregistration() throws Exception {
    final MBeanServer mbeanServer = Registry.getRegistry(null, null).getMBeanServer();
    // Verify there are no Catalina or Tomcat MBeans
    Set<ObjectName> onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    Assert.assertEquals("Unexpected: " + onames, 0, onames.size());
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    Assert.assertEquals("Unexpected: " + onames, 0, onames.size());

    final Tomcat tomcat = getTomcatInstance();
    final File contextDir = new File(getTemporaryDirectory(), "webappFoo");
    addDeleteOnTearDown(contextDir);
    if (!contextDir.mkdirs() && !contextDir.isDirectory()) {
        Assert.fail("Failed to create: [" + contextDir.toString() + "]");
    }
    Context ctx = tomcat.addContext(contextName, contextDir.getAbsolutePath());

    CombinedRealm combinedRealm = new CombinedRealm();
    Realm nullRealm = new NullRealm();
    combinedRealm.addRealm(nullRealm);
    ctx.setRealm(combinedRealm);

    tomcat.start();

    getUrl("http://localhost:" + getPort());

    // Verify there are no Catalina MBeans
    onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    Assert.assertEquals("Found: " + onames, 0, onames.size());

    // Verify there are the correct Tomcat MBeans
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    ArrayList<String> found = new ArrayList<>(onames.size());
    for (ObjectName on: onames) {
        found.add(on.toString());
    }

    // Create the list of expected MBean names
    String protocol = tomcat.getConnector().getProtocolHandlerClassName();
    if (protocol.indexOf("Nio2") > 0) {
        protocol = "nio2";
    } else if (protocol.indexOf("Apr") > 0) {
        protocol = "apr";
    } else {
        protocol = "nio";
    }
    String index = tomcat.getConnector().getProperty("nameIndex").toString();
    ArrayList<String> expected = new ArrayList<>(Arrays.asList(basicMBeanNames()));
    expected.addAll(Arrays.asList(hostMBeanNames("localhost")));
    expected.addAll(Arrays.asList(contextMBeanNames("localhost", contextName)));
    expected.addAll(Arrays.asList(connectorMBeanNames("auto-" + index, protocol)));
    expected.addAll(Arrays.asList(optionalMBeanNames("localhost")));
    expected.addAll(Arrays.asList(requestMBeanNames(
            "auto-" + index + "-" + getPort(), protocol)));

    // Did we find all expected MBeans?
    ArrayList<String> missing = new ArrayList<>(expected);
    missing.removeAll(found);
    Assert.assertTrue("Missing Tomcat MBeans: " + missing, missing.isEmpty());

    // Did we find any unexpected MBeans?
    List<String> additional = found;
    additional.removeAll(expected);
    Assert.assertTrue("Unexpected Tomcat MBeans: " + additional, additional.isEmpty());

    tomcat.stop();

    // There should still be some Tomcat MBeans
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    Assert.assertTrue("No Tomcat MBeans", onames.size() > 0);

    // add a new host
    StandardHost host = new StandardHost();
    host.setName("otherhost");
    tomcat.getEngine().addChild(host);

    final File contextDir2 = new File(getTemporaryDirectory(), "webappFoo2");
    addDeleteOnTearDown(contextDir2);
    if (!contextDir2.mkdirs() && !contextDir2.isDirectory()) {
        Assert.fail("Failed to create: [" + contextDir2.toString() + "]");
    }
    tomcat.addContext(host, contextName + "2", contextDir2.getAbsolutePath());

    tomcat.start();
    tomcat.stop();
    tomcat.destroy();

    // There should be no Catalina MBeans and no Tomcat MBeans
    onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    Assert.assertEquals("Remaining: " + onames, 0, onames.size());
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    Assert.assertEquals("Remaining: " + onames, 0, onames.size());
}
 
Example 8
Source File: Embedded.java    From Tomcat7.0.67 with Apache License 2.0 1 votes vote down vote up
/**
 * Create, configure, and return a Host that will process all
 * HTTP requests received from one of the associated Connectors,
 * and directed to the specified virtual host.
 * <p>
 * After you have customized the properties, listeners, and Valves
 * for this Host, you must attach it to the corresponding Engine
 * by calling:
 * <pre>
 *   engine.addChild(host);
 * </pre>
 * which will also cause the Host to be started if the Engine has
 * already been started.  If this is the default (or only) Host you
 * will be defining, you may also tell the Engine to pass all requests
 * not assigned to another virtual host to this one:
 * <pre>
 *   engine.setDefaultHost(host.getName());
 * </pre>
 *
 * @param name Canonical name of this virtual host
 * @param appBase Absolute pathname to the application base directory
 *  for this virtual host
 *
 * @exception IllegalArgumentException if an invalid parameter
 *  is specified
 */
public Host createHost(String name, String appBase) {

    if( log.isDebugEnabled() )
        log.debug("Creating host '" + name + "' with appBase '" +
                   appBase + "'");

    StandardHost host = new StandardHost();

    host.setAppBase(appBase);
    host.setName(name);

    return (host);

}
 
Example 9
Source File: Embedded.java    From tomcatsrc with Apache License 2.0 1 votes vote down vote up
/**
 * Create, configure, and return a Host that will process all
 * HTTP requests received from one of the associated Connectors,
 * and directed to the specified virtual host.
 * <p>
 * After you have customized the properties, listeners, and Valves
 * for this Host, you must attach it to the corresponding Engine
 * by calling:
 * <pre>
 *   engine.addChild(host);
 * </pre>
 * which will also cause the Host to be started if the Engine has
 * already been started.  If this is the default (or only) Host you
 * will be defining, you may also tell the Engine to pass all requests
 * not assigned to another virtual host to this one:
 * <pre>
 *   engine.setDefaultHost(host.getName());
 * </pre>
 *
 * @param name Canonical name of this virtual host
 * @param appBase Absolute pathname to the application base directory
 *  for this virtual host
 *
 * @exception IllegalArgumentException if an invalid parameter
 *  is specified
 */
public Host createHost(String name, String appBase) {

    if( log.isDebugEnabled() )
        log.debug("Creating host '" + name + "' with appBase '" +
                   appBase + "'");

    StandardHost host = new StandardHost();

    host.setAppBase(appBase);
    host.setName(name);

    return (host);

}