Search in sources :

Example 56 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class LiveReloadInstaller method install.

public static void install(String path, final int port, final String folder) {
    final Server server = TomcatHelper.getServer();
    if (server == null) {
        throw new IllegalStateException("tomcat not yet starting");
    }
    // checking which one is localhost could be better but should be fine
    final Service service = server.findServices()[0];
    final Engine engine = Engine.class.cast(service.getContainer());
    final Container host = engine.findChild(engine.getDefaultHost());
    if (LifecycleState.STARTED != host.getState()) {
        throw new IllegalStateException("host not started, call LiveReloadInstaller.install() later.");
    }
    // add connector
    final Connector connector = new Connector();
    connector.setPort(port);
    connector.setAttribute("connectionTimeout", "30000");
    service.addConnector(connector);
    // and the endpoint and start the watcher
    final Closeable watch = Instances.get().getWatcher().watch(folder);
    final LiveReloadWebapp liveReloadWebapp = new LiveReloadWebapp(path);
    liveReloadWebapp.addApplicationLifecycleListener(new ServletContextListener() {

        @Override
        public void contextInitialized(final ServletContextEvent servletContextEvent) {
            servletContextEvent.getServletContext().log("Started livereload server on port " + port);
        }

        @Override
        public void contextDestroyed(final ServletContextEvent servletContextEvent) {
            try {
                watch.close();
            } catch (final IOException e) {
            // no-op: not that important, we shutdown anyway
            }
        }
    });
    host.addChild(liveReloadWebapp);
}
Also used : Connector(org.apache.catalina.connector.Connector) Container(org.apache.catalina.Container) Server(org.apache.catalina.Server) ServletContextListener(javax.servlet.ServletContextListener) Closeable(java.io.Closeable) Service(org.apache.catalina.Service) IOException(java.io.IOException) Engine(org.apache.catalina.Engine) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 57 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class TomcatWsRegistry method addWsContainer.

// String webContext, String path, HttpListener httpListener, String virtualHost, String realmName, String transportGuarantee, String authMethod, ClassLoader classLoader
@Override
public List<String> addWsContainer(final HttpListener httpListener, final ClassLoader classLoader, final String context, String virtualHost, String path, final String realmName, final String transportGuarantee, final String authMethod, final String moduleId) throws Exception {
    if (path == null) {
        throw new NullPointerException("contextRoot is null");
    }
    if (httpListener == null) {
        throw new NullPointerException("httpListener is null");
    }
    // assure context root with a leading slash
    if (!path.startsWith("/")) {
        path = "/" + path;
    }
    // find the existing host (we do not auto-create hosts)
    if (virtualHost == null) {
        virtualHost = engine.getDefaultHost();
    }
    final Container host = engine.findChild(virtualHost);
    if (host == null) {
        throw new IllegalArgumentException("Invalid virtual host '" + virtualHost + "'.  Do you have a matchiing Host entry in the server.xml?");
    }
    final List<String> addresses = new ArrayList<>();
    // - old way (/*)
    if (WEBSERVICE_OLDCONTEXT_ACTIVE) {
        deployInFakeWebapp(path, classLoader, authMethod, transportGuarantee, realmName, host, httpListener, addresses, context);
    }
    // - new way (/<webappcontext>/webservices/<name>) if webcontext is specified
    if (context != null) {
        final Context webAppContext = findContext(context, moduleId, host);
        if (webAppContext != null) {
            // sub context = '/' means the service address is provided by webservices
            if (WEBSERVICE_SUB_CONTEXT.equals("/") && path.startsWith("/")) {
                addServlet(host, webAppContext, path, httpListener, path, addresses, false, moduleId);
            } else if (WEBSERVICE_SUB_CONTEXT.equals("/") && !path.startsWith("/")) {
                addServlet(host, webAppContext, '/' + path, httpListener, path, addresses, false, moduleId);
            } else {
                addServlet(host, webAppContext, WEBSERVICE_SUB_CONTEXT + path, httpListener, path, addresses, false, moduleId);
            }
        } else if (!WEBSERVICE_OLDCONTEXT_ACTIVE) {
            // deploying in a jar
            deployInFakeWebapp(path, classLoader, authMethod, transportGuarantee, realmName, host, httpListener, addresses, context);
        }
    }
    return addresses;
}
Also used : Context(org.apache.catalina.Context) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) Container(org.apache.catalina.Container) ArrayList(java.util.ArrayList)

Example 58 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class WebappDeployer method check.

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);
                }
            }
        }
    }
}
Also used : Container(org.apache.catalina.Container) StandardServer(org.apache.catalina.core.StandardServer) StandardHost(org.apache.catalina.core.StandardHost) Service(org.apache.catalina.Service) Engine(org.apache.catalina.Engine)

Example 59 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class TomcatHessianRegistry method deploy.

@Override
public String deploy(final ClassLoader loader, final HessianServer listener, final String hostname, final String app, final String authMethod, final String transportGuarantee, final String realmName, final String name) throws URISyntaxException {
    Container host = engine.findChild(hostname);
    if (host == null) {
        host = engine.findChild(engine.getDefaultHost());
        if (host == null) {
            throw new IllegalArgumentException("Invalid virtual host '" + engine.getDefaultHost() + "'.  Do you have a matchiing Host entry in the server.xml?");
        }
    }
    final String contextRoot = contextName(app);
    Context context = Context.class.cast(host.findChild(contextRoot));
    if (context == null) {
        Pair<Context, Integer> fakeContext = fakeContexts.get(contextRoot);
        if (fakeContext != null) {
            context = fakeContext.getLeft();
            fakeContext.setValue(fakeContext.getValue() + 1);
        } else {
            context = Context.class.cast(host.findChild(contextRoot));
            if (context == null) {
                fakeContext = fakeContexts.get(contextRoot);
                if (fakeContext == null) {
                    context = createNewContext(loader, authMethod, transportGuarantee, realmName, app);
                    fakeContext = new MutablePair<>(context, 1);
                    fakeContexts.put(contextRoot, fakeContext);
                } else {
                    context = fakeContext.getLeft();
                    fakeContext.setValue(fakeContext.getValue() + 1);
                }
            }
        }
    }
    final String servletMapping = generateServletPath(name);
    Wrapper wrapper = Wrapper.class.cast(context.findChild(servletMapping));
    if (wrapper != null) {
        throw new IllegalArgumentException("Servlet " + servletMapping + " in web application context " + context.getName() + " already exists");
    }
    wrapper = context.createWrapper();
    wrapper.setName(HESSIAN.replace("/", "") + "_" + name);
    wrapper.setServlet(new OpenEJBHessianServlet(listener));
    context.addChild(wrapper);
    context.addServletMappingDecoded(servletMapping, wrapper.getName());
    if ("BASIC".equals(authMethod) && StandardContext.class.isInstance(context)) {
        final StandardContext standardContext = StandardContext.class.cast(context);
        boolean found = false;
        for (final Valve v : standardContext.getPipeline().getValves()) {
            if (LimitedBasicValve.class.isInstance(v) || BasicAuthenticator.class.isInstance(v)) {
                found = true;
                break;
            }
        }
        if (!found) {
            standardContext.addValve(new LimitedBasicValve());
        }
    }
    final List<String> addresses = new ArrayList<>();
    for (final Connector connector : connectors) {
        for (final String mapping : wrapper.findMappings()) {
            final URI address = new URI(connector.getScheme(), null, host.getName(), connector.getPort(), contextRoot + mapping, null, null);
            addresses.add(address.toString());
        }
    }
    return HttpUtil.selectSingleAddress(addresses);
}
Also used : Context(org.apache.catalina.Context) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) Wrapper(org.apache.catalina.Wrapper) Connector(org.apache.catalina.connector.Connector) ArrayList(java.util.ArrayList) URI(java.net.URI) Container(org.apache.catalina.Container) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) OpenEJBValve(org.apache.tomee.catalina.OpenEJBValve) Valve(org.apache.catalina.Valve)

Example 60 with Container

use of org.apache.catalina.Container in project tomee by apache.

the class TomcatHessianRegistry method undeploy.

@Override
public void undeploy(final String hostname, final String app, final String name) {
    Container host = engine.findChild(hostname);
    if (host == null) {
        host = engine.findChild(engine.getDefaultHost());
        if (host == null) {
            throw new IllegalArgumentException("Invalid virtual host '" + engine.getDefaultHost() + "'.  Do you have a matchiing Host entry in the server.xml?");
        }
    }
    final String contextRoot = contextName(app);
    final Pair<Context, Integer> fakeContext = fakeContexts.get(contextRoot);
    if (fakeContext != null) {
        fakeContext.setValue(fakeContext.getValue() - 1);
        if (fakeContext.getValue() == 0) {
            fakeContexts.remove(contextRoot);
            host.removeChild(fakeContext.getKey());
        }
    }
}
Also used : Context(org.apache.catalina.Context) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) Container(org.apache.catalina.Container)

Aggregations

Container (org.apache.catalina.Container)163 Context (org.apache.catalina.Context)28 IOException (java.io.IOException)24 Host (org.apache.catalina.Host)22 StandardContext (org.apache.catalina.core.StandardContext)21 Engine (org.apache.catalina.Engine)18 LifecycleException (org.apache.catalina.LifecycleException)17 File (java.io.File)15 ObjectName (javax.management.ObjectName)15 Wrapper (org.apache.catalina.Wrapper)13 StandardHost (org.apache.catalina.core.StandardHost)13 ArrayList (java.util.ArrayList)12 ServletException (javax.servlet.ServletException)11 MalformedURLException (java.net.MalformedURLException)10 Valve (org.apache.catalina.Valve)10 StandardWrapper (org.apache.catalina.core.StandardWrapper)10 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)10 NamingException (javax.naming.NamingException)9 Lifecycle (org.apache.catalina.Lifecycle)9 Realm (org.apache.catalina.Realm)9