Java Code Examples for org.apache.catalina.Engine#findChildren()

The following examples show how to use org.apache.catalina.Engine#findChildren() . 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: MapperListener.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void startInternal() throws LifecycleException {

    setState(LifecycleState.STARTING);

    // Find any components that have already been initialized since the
    // MBean listener won't be notified as those components will have
    // already registered their MBeans
    findDefaultHost();

    Engine engine = (Engine) connector.getService().getContainer();
    addListeners(engine);

    Container[] conHosts = engine.findChildren();
    for (Container conHost : conHosts) {
        Host host = (Host) conHost;
        if (!LifecycleState.NEW.equals(host.getState())) {
            // Registering the host will register the context and wrappers
            registerHost(host);
        }
    }
}
 
Example 2
Source File: MapperListener.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void startInternal() throws LifecycleException {

    setState(LifecycleState.STARTING);

    // Find any components that have already been initialized since the
    // MBean listener won't be notified as those components will have
    // already registered their MBeans
    findDefaultHost();

    Engine engine = (Engine) connector.getService().getContainer();
    addListeners(engine);

    Container[] conHosts = engine.findChildren();
    for (Container conHost : conHosts) {
        Host host = (Host) conHost;
        if (!LifecycleState.NEW.equals(host.getState())) {
            // Registering the host will register the context and wrappers
            registerHost(host);
        }
    }
}
 
Example 3
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Sets the current host - all future webapps will
 * be added to this host. When tomcat starts, the
 * host will be the default host.
 *
 * @param host The current host
 */
public void setHost(Host host) {
    Engine engine = getEngine();
    boolean found = false;
    for (Container engineHost : engine.findChildren()) {
        if (engineHost == host) {
            found = true;
        }
    }
    if (!found) {
        engine.addChild(host);
    }
}
 
Example 4
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public Host getHost() {
    Engine engine = getEngine();
    if (engine.findChildren().length > 0) {
        return (Host) engine.findChildren()[0];
    }

    Host host = new StandardHost();
    host.setName(hostname);
    getEngine().addChild(host);
    return host;
}
 
Example 5
Source File: MapperListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void startInternal() throws LifecycleException {

    setState(LifecycleState.STARTING);
    /**
     * StandardEngine
     */
    Engine engine = service.getContainer();
    if (engine == null) {
        return;
    }
    /***
     * 为映射注入对应Host容器。
     */
    findDefaultHost();

    /**
     * 增加对应的监听。
     */
    addListeners(engine);

    Container[] conHosts = engine.findChildren();
    for (Container conHost : conHosts) {
        Host host = (Host) conHost;
        if (!LifecycleState.NEW.equals(host.getState())) {
            // Registering the host will register the context and wrappers
            /**
             * 注册Host的时候将会注册Context和多个Wrapper。
             * {@link MapperListener#registerHost(org.apache.catalina.Host)}
             */
            registerHost(host);
        }
    }
}
 
Example 6
Source File: MapperListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void findDefaultHost() {

        Engine engine = service.getContainer();
        String defaultHost = engine.getDefaultHost();

        boolean found = false;

        if (defaultHost != null && defaultHost.length() >0) {
            Container[] containers = engine.findChildren();

            for (Container container : containers) {
                Host host = (Host) container;
                if (defaultHost.equalsIgnoreCase(host.getName())) {
                    found = true;
                    break;
                }

                String[] aliases = host.findAliases();
                for (String alias : aliases) {
                    if (defaultHost.equalsIgnoreCase(alias)) {
                        found = true;
                        break;
                    }
                }
            }
        }

        if(found) {
            mapper.setDefaultHostName(defaultHost);
        } else {
            log.warn(sm.getString("mapperListener.unknownDefaultHost",
                    defaultHost, service));
        }
    }
 
Example 7
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void registerListenersForEngine(Engine engine) {
    for (Container hostContainer : engine.findChildren()) {
        Host host = (Host) hostContainer;
        host.addContainerListener(this);
        registerListenersForHost(host);
    }
}
 
Example 8
Source File: MapperListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void findDefaultHost() {

        Engine engine = (Engine) connector.getService().getContainer();
        String defaultHost = engine.getDefaultHost();

        boolean found = false;

        if (defaultHost != null && defaultHost.length() >0) {
            Container[] containers = engine.findChildren();

            for (Container container : containers) {
                Host host = (Host) container;
                if (defaultHost.equalsIgnoreCase(host.getName())) {
                    found = true;
                    break;
                }

                String[] aliases = host.findAliases();
                for (String alias : aliases) {
                    if (defaultHost.equalsIgnoreCase(alias)) {
                        found = true;
                        break;
                    }
                }
            }
        }

        if(found) {
            mapper.setDefaultHostName(defaultHost);
        } else {
            log.warn(sm.getString("mapperListener.unknownDefaultHost",
                    defaultHost, connector));
        }
    }
 
Example 9
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void registerListenersForEngine(Engine engine) {
    for (Container hostContainer : engine.findChildren()) {
        Host host = (Host) hostContainer;
        host.addContainerListener(this);
        registerListenersForHost(host);
    }
}
 
Example 10
Source File: MapperListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void findDefaultHost() {

        Engine engine = (Engine) connector.getService().getContainer();
        String defaultHost = engine.getDefaultHost();

        boolean found = false;

        if (defaultHost != null && defaultHost.length() >0) {
            Container[] containers = engine.findChildren();

            for (Container container : containers) {
                Host host = (Host) container;
                if (defaultHost.equalsIgnoreCase(host.getName())) {
                    found = true;
                    break;
                }

                String[] aliases = host.findAliases();
                for (String alias : aliases) {
                    if (defaultHost.equalsIgnoreCase(alias)) {
                        found = true;
                        break;
                    }
                }
            }
        }

        if(found) {
            mapper.setDefaultHostName(defaultHost);
        } else {
            log.warn(sm.getString("mapperListener.unknownDefaultHost",
                    defaultHost, connector));
        }
    }
 
Example 11
Source File: ThreadLocalLeakPreventionListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void registerListenersForEngine(Engine engine) {
    for (Container hostContainer : engine.findChildren()) {
        Host host = (Host) hostContainer;
        host.addContainerListener(this);
        registerListenersForHost(host);
    }
}
 
Example 12
Source File: TomcatLoader.java    From tomee with Apache License 2.0 5 votes vote down vote up
/**
 * Process running web applications for ejb deployments.
 *
 * @param tomcatWebAppBuilder tomcat web app builder instance
 * @param standardServer      tomcat server instance
 */
private void processRunningApplications(final TomcatWebAppBuilder tomcatWebAppBuilder, final StandardServer standardServer) {
    for (final org.apache.catalina.Service service : standardServer.findServices()) {
        if (service.getContainer() instanceof Engine) {
            final Engine engine = (Engine) service.getContainer();
            for (final Container engineChild : engine.findChildren()) {
                if (engineChild instanceof Host) {
                    final Host host = (Host) engineChild;
                    for (final Container hostChild : host.findChildren()) {
                        if (hostChild instanceof StandardContext) {
                            final StandardContext standardContext = (StandardContext) hostChild;
                            final int state = TomcatHelper.getContextState(standardContext);
                            if (state == 0) {
                                // context only initialized
                                tomcatWebAppBuilder.init(standardContext);
                            } else if (state == 1) {
                                // context already started
                                standardContext.addParameter("openejb.start.late", "true");
                                final ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
                                Thread.currentThread().setContextClassLoader(standardContext.getLoader().getClassLoader());
                                try {
                                    tomcatWebAppBuilder.init(standardContext);
                                    tomcatWebAppBuilder.beforeStart(standardContext);
                                    tomcatWebAppBuilder.start(standardContext);
                                    tomcatWebAppBuilder.afterStart(standardContext);
                                } finally {
                                    Thread.currentThread().setContextClassLoader(oldCL);
                                }
                                standardContext.removeParameter("openejb.start.late");
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example 13
Source File: WebappDeployer.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void check() {
	final StandardServer server = TomcatHelper.getServer();
	for (final Service service : server.findServices()) {
		if (service.getContainer() instanceof Engine) {
			final Engine engine = (Engine) service.getContainer();
			for (final Container engineChild : engine.findChildren()) {
				if (engineChild instanceof StandardHost) {
					final StandardHost host = (StandardHost) engineChild;
					webappBuilder.checkHost(host);
				}
			}
           }
	}
}
 
Example 14
Source File: ManagedConnectorFactory.java    From armeria with Apache License 2.0 4 votes vote down vote up
private void checkConfiguration(StandardServer server,
                                Service expectedService, Connector expectedConnector,
                                Engine expectedEngine, StandardHost expectedHost, Context expectedContext) {

    // Check if Catalina base and home directories have not been changed.
    final File expectedBaseDir = config.baseDir().toFile();
    if (!Objects.equals(server.getCatalinaBase(), expectedBaseDir) ||
        !Objects.equals(server.getCatalinaHome(), expectedBaseDir)) {
        throw new TomcatServiceException("A configurator should never change the Catalina base and home.");
    }

    // Check if the server's port has not been changed.
    if (server.getPort() != EMBEDDED_TOMCAT_PORT) {
        throw new TomcatServiceException("A configurator should never change the port of the server.");
    }

    // Check if the default service has not been removed and a new service has not been added.
    final Service[] services = server.findServices();
    if (services == null || services.length != 1 || services[0] != expectedService) {
        throw new TomcatServiceException(
                "A configurator should never remove the default service or add a new service.");
    }

    // Check if the name of the default service has not been changed.
    if (!config.serviceName().equals(expectedService.getName())) {
        throw new TomcatServiceException(
                "A configurator should never change the name of the default service.");
    }

    // Check if the default connector has not been removed
    final Connector[] connectors = expectedService.findConnectors();
    if (connectors == null || Arrays.stream(connectors).noneMatch(c -> c == expectedConnector)) {
        throw new TomcatServiceException("A configurator should never remove the default connector.");
    }

    // Check if the engine has not been changed.
    final Container actualEngine = TomcatUtil.engine(expectedService, expectedHost.getName());
    if (actualEngine != expectedEngine) {
        throw new TomcatServiceException(
                "A configurator should never change the engine of the default service.");
    }

    // Check if the engine's name has not been changed.
    if (!config.engineName().equals(expectedEngine.getName())) {
        throw new TomcatServiceException(
                "A configurator should never change the name of the default engine.");
    }

    // Check if the default realm has not been changed.
    if (expectedEngine.getRealm() != config.realm()) {
        throw new TomcatServiceException("A configurator should never change the default realm.");
    }

    // Check if the default host has not been removed.
    final Container[] engineChildren = expectedEngine.findChildren();
    if (engineChildren == null || Arrays.stream(engineChildren).noneMatch(c -> c == expectedHost)) {
        throw new TomcatServiceException("A configurator should never remove the default host.");
    }

    // Check if the default context has not been removed.
    final Container[] contextChildren = expectedHost.findChildren();
    if (contextChildren == null || Arrays.stream(contextChildren).noneMatch(c -> c == expectedContext)) {
        throw new TomcatServiceException("A configurator should never remove the default context.");
    }

    // Check if the docBase of the default context has not been changed.
    if (!config.docBase().toString().equals(expectedContext.getDocBase())) {
        throw new TomcatServiceException(
                "A configurator should never change the docBase of the default context.");
    }
}
 
Example 15
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new web application builder
 * instance.
 */
public TomcatWebAppBuilder() {
    SystemInstance.get().setComponent(WebAppBuilder.class, this);
    SystemInstance.get().setComponent(TomcatWebAppBuilder.class, this);
    initJEEInfo = "true".equalsIgnoreCase(SystemInstance.get().getProperty(TomEESystemConfig.TOMEE_INIT_J2EE_INFO, "true"));

    // TODO: re-write this bit, so this becomes part of the listener, and we register this with the mbean server.

    final StandardServer standardServer = TomcatHelper.getServer();
    globalListenerSupport = new GlobalListenerSupport(standardServer, this);

    //Getting host config listeners
    hosts = new Hosts();
    SystemInstance.get().setComponent(Hosts.class, hosts);
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    for (final Service service : standardServer.findServices()) {
        if (service.getContainer() instanceof Engine) {
            final Engine engine = service.getContainer();

            // add the global router if relevant
            final URL globalRouterConf = RouterValve.serverRouterConfigurationURL();
            if (globalRouterConf != null) {
                final RouterValve routerValve = new RouterValve();
                routerValve.setConfigurationPath(globalRouterConf);
                engine.getPipeline().addValve(routerValve);
            }

            parentClassLoader = engine.getParentClassLoader();
            if (parentClassLoader == ClassLoader.getSystemClassLoader() && parentClassLoader != tccl) {
                parentClassLoader = tccl;
                engine.setParentClassLoader(tccl);
            } // else assume tomcat was setup to force a classloader and then respect it

            manageCluster(engine.getCluster());
            hosts.setDefault(engine.getDefaultHost());
            addTomEERealm(engine);

            for (final Container engineChild : engine.findChildren()) {
                if (engineChild instanceof StandardHost) {
                    final StandardHost host = (StandardHost) engineChild;
                    manageCluster(host.getCluster());
                    addTomEERealm(host);
                    host.getPipeline().addValve(new OpenEJBSecurityListener.RequestCapturer());
                    hosts.add(host);
                    for (final LifecycleListener listener : host.findLifecycleListeners()) {
                        if (listener instanceof HostConfig) {
                            final HostConfig hostConfig = (HostConfig) listener;
                            deployers.put(host.getName(), hostConfig);
                        }
                    }
                }
            }
        }
    }

    SystemInstance.get().addObserver(new ClusterObserver(clusters));

    final OpenEjbConfigurationFactory component = SystemInstance.get().getComponent(OpenEjbConfigurationFactory.class);
    ConfigurationFactory configurationFactory = ConfigurationFactory.class.isInstance(component) ?
            ConfigurationFactory.class.cast(component) : SystemInstance.get().getComponent(ConfigurationFactory.class);
    if (configurationFactory == null) {
        configurationFactory = new ConfigurationFactory();
    }
    this.configurationFactory = configurationFactory;
    deploymentLoader = new DeploymentLoader();

    servletContextHandler = new ServletContextHandler();
    setComponentsUsedByCDI();

    try { // before tomcat was using ServiceLoader or manually instantiation, now it uses SL for itself so we can be in conflict
        WebSockets.setConfigurator();
    } catch (final Throwable th) {
        // no-op: can be another API impl, normally we are ok, this is really just a safe belt
    }

    noHostCheck = !Boolean.parseBoolean(SystemInstance.get().getProperty("tomee.host.check", "true"));
}