Java Code Examples for org.apache.catalina.Context#addChild()

The following examples show how to use org.apache.catalina.Context#addChild() . 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: Launcher.java    From lucene-geo-gazetteer with Apache License 2.0 6 votes vote down vote up
public static void launchService(int port, String indexPath)
        throws IOException, LifecycleException {

    Tomcat server = new Tomcat();
    Context context = server.addContext("/", new File(".").getAbsolutePath());
    System.setProperty(INDEX_PATH_PROP, indexPath);

    Wrapper servlet = context.createWrapper();
    servlet.setName("CXFNonSpringJaxrs");
    servlet.setServletClass(CXFNonSpringJaxrsServlet.class.getName());
    servlet.addInitParameter("jaxrs.serviceClasses", SearchResource.class.getName() + " " + HealthCheckAPI.class.getName());

    servlet.setLoadOnStartup(1);
    context.addChild(servlet);
    context.addServletMapping("/api/*", "CXFNonSpringJaxrs");

    System.out.println("Starting Embedded Tomcat on port : " + port );
    server.setPort(port);
    server.start();
    server.getServer().await();
}
 
Example 2
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Static version of {@link #addServlet(String, String, String)}
 * @param ctx           Context to add Servlet to
 * @param servletName   Servlet name (used in mappings)
 * @param servletClass  The class to be used for the Servlet
 * @return The wrapper for the servlet
 */
public static Wrapper addServlet(Context ctx,
                                  String servletName,
                                  String servletClass) {
    // will do class for name and set init params
    Wrapper sw = ctx.createWrapper();
    sw.setServletClass(servletClass);
    sw.setName(servletName);
    ctx.addChild(sw);

    return sw;
}
 
Example 3
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Static version of {@link #addServlet(String, String, Servlet)}.
 * @param ctx           Context to add Servlet to
 * @param servletName   Servlet name (used in mappings)
 * @param servlet       The Servlet to add
 * @return The wrapper for the servlet
 */
public static Wrapper addServlet(Context ctx,
                                  String servletName,
                                  Servlet servlet) {
    // will do class for name and set init params
    Wrapper sw = new ExistingStandardWrapper(servlet);
    sw.setName(servletName);
    ctx.addChild(sw);

    return sw;
}
 
Example 4
Source File: ArkTomcatServletWebServerFactory.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
private void addDefaultServlet(Context context) {
    Wrapper defaultServlet = context.createWrapper();
    defaultServlet.setName("default");
    defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
    defaultServlet.addInitParameter("debug", "0");
    defaultServlet.addInitParameter("listings", "false");
    defaultServlet.setLoadOnStartup(1);
    // Otherwise the default location of a Spring DispatcherServlet cannot be set
    defaultServlet.setOverridable(true);
    context.addChild(defaultServlet);
    context.addServletMappingDecoded("/", "default");
}
 
Example 5
Source File: ArkTomcatServletWebServerFactory.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
private void addJspServlet(Context context) {
    Wrapper jspServlet = context.createWrapper();
    jspServlet.setName("jsp");
    jspServlet.setServletClass(getJsp().getClassName());
    jspServlet.addInitParameter("fork", "false");
    for (Map.Entry<String, String> entry : getJsp().getInitParameters().entrySet()) {
        jspServlet.addInitParameter(entry.getKey(), entry.getValue());
    }
    jspServlet.setLoadOnStartup(3);
    context.addChild(jspServlet);
    context.addServletMappingDecoded("*.jsp", "jsp");
    context.addServletMappingDecoded("*.jspx", "jsp");
}
 
Example 6
Source File: Tomcat.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Static version of {@link #addServlet(String, String, String)}
 * @param ctx           Context to add Servlet to
 * @param servletName   Servlet name (used in mappings)
 * @param servletClass  The class to be used for the Servlet
 * @return The wrapper for the servlet
 */
public static Wrapper addServlet(Context ctx, 
                                  String servletName, 
                                  String servletClass) {
    // will do class for name and set init params
    Wrapper sw = ctx.createWrapper();
    sw.setServletClass(servletClass);
    sw.setName(servletName);
    ctx.addChild(sw);
    
    return sw;
}
 
Example 7
Source File: Tomcat.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Static version of {@link #addServlet(String, String, Servlet)}.
 * @param ctx           Context to add Servlet to
 * @param servletName   Servlet name (used in mappings)
 * @param servlet       The Servlet to add
 * @return The wrapper for the servlet
 */
public static Wrapper addServlet(Context ctx,
                                  String servletName, 
                                  Servlet servlet) {
    // will do class for name and set init params
    Wrapper sw = new ExistingStandardWrapper(servlet);
    sw.setName(servletName);
    ctx.addChild(sw);
    
    return sw;
}
 
Example 8
Source File: Tomcat.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Static version of {@link #addServlet(String, String, String)}
 * @param ctx           Context to add Servlet to
 * @param servletName   Servlet name (used in mappings)
 * @param servletClass  The class to be used for the Servlet
 * @return The wrapper for the servlet
 */
public static Wrapper addServlet(Context ctx, 
                                  String servletName, 
                                  String servletClass) {
    // will do class for name and set init params
    Wrapper sw = ctx.createWrapper();
    sw.setServletClass(servletClass);
    sw.setName(servletName);
    ctx.addChild(sw);
    
    return sw;
}
 
Example 9
Source File: Tomcat.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Static version of {@link #addServlet(String, String, Servlet)}.
 * @param ctx           Context to add Servlet to
 * @param servletName   Servlet name (used in mappings)
 * @param servlet       The Servlet to add
 * @return The wrapper for the servlet
 */
public static Wrapper addServlet(Context ctx,
                                  String servletName, 
                                  Servlet servlet) {
    // will do class for name and set init params
    Wrapper sw = new ExistingStandardWrapper(servlet);
    sw.setName(servletName);
    ctx.addChild(sw);
    
    return sw;
}
 
Example 10
Source File: TestStandardWrapper.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBug51445AddChild() throws Exception {

    initLatch();

    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    StandardWrapper wrapper = new StandardWrapper();
    wrapper.setServletName("Bug51445");
    wrapper.setServletClass(Bug51445Servlet.class.getName());
    ctx.addChild(wrapper);
    ctx.addServletMappingDecoded("/", "Bug51445");

    tomcat.start();

    // Start the threads
    Bug51445Thread[] threads = new Bug51445Thread[5];
    for (int i = 0; i < BUG51445_THREAD_COUNT; i ++) {
        threads[i] = new Bug51445Thread(getPort());
        threads[i].start();
    }

    // Wait for threads to finish
    for (int i = 0; i < BUG51445_THREAD_COUNT; i ++) {
        threads[i].join();
    }

    Set<String> servlets = new HashSet<>();
    // Output the result
    for (int i = 0; i < BUG51445_THREAD_COUNT; i ++) {
        System.out.println(threads[i].getResult());
    }
    // Check the result
    for (int i = 0; i < BUG51445_THREAD_COUNT; i ++) {
        String[] results = threads[i].getResult().split(",");
        Assert.assertEquals(2, results.length);
        Assert.assertEquals("10", results[0]);
        Assert.assertFalse(servlets.contains(results[1]));
        servlets.add(results[1]);
    }
}
 
Example 11
Source File: TomcatServer.java    From pippo with Apache License 2.0 4 votes vote down vote up
@Override
public void start() {
    if (StringUtils.isNullOrEmpty(pippoFilterPath)) {
        pippoFilterPath = "/*";
    }

    tomcat = createTomcat();
    tomcat.setBaseDir(getSettings().getBaseFolder());

    if (getSettings().getKeystoreFile() == null) {
        enablePlainConnector(tomcat);
    } else {
        enableSSLConnector(tomcat);
    }

    File docBase = new File(System.getProperty("java.io.tmpdir"));
    Context context = tomcat.addContext(getSettings().getContextPath(), docBase.getAbsolutePath());
    context.setAllowCasualMultipartParsing(true);
    PippoServlet pippoServlet = new PippoServlet();
    pippoServlet.setApplication(getApplication());

    Wrapper wrapper = context.createWrapper();
    String name = "pippoServlet";

    wrapper.setName(name);
    wrapper.setLoadOnStartup(1);
    wrapper.setServlet(pippoServlet);
    context.addChild(wrapper);
    context.addServletMapping(pippoFilterPath, name);

    // inject application as context attribute
    context.getServletContext().setAttribute(PIPPO_APPLICATION, getApplication());

    // add initializers
    context.addApplicationListener(PippoServletContextListener.class.getName());

    // add listeners
    listeners.forEach(listener -> context.addApplicationListener(listener.getName()));

    String version = tomcat.getClass().getPackage().getImplementationVersion();
    log.info("Starting Tomcat Server {} on port {}", version, getSettings().getPort());

    try {
        tomcat.start();
    } catch (LifecycleException e) {
        log.error("Unable to launch Tomcat", e);
        throw new PippoRuntimeException(e);
    }

    if (!getApplication().getPippoSettings().isTest()) {
        tomcat.getServer().await();
    }
}
 
Example 12
Source File: TomcatWsRegistry.java    From tomee with Apache License 2.0 4 votes vote down vote up
private void addServlet(final Container host, final Context context, final String mapping, final HttpListener httpListener, final String path,
                        final List<String> addresses, final boolean fakeDeployment, final String moduleId) {
    // build the servlet
    final Wrapper wrapper = context.createWrapper();
    wrapper.setName("webservice" + path.substring(1));
    wrapper.setServletClass(WsServlet.class.getName());

    // add servlet to context
    context.addChild(wrapper);
    context.addServletMappingDecoded(mapping, wrapper.getName());

    final String webServicecontainerID = wrapper.getName() + WsServlet.WEBSERVICE_CONTAINER + httpListener.hashCode();
    wrapper.addInitParameter(WsServlet.WEBSERVICE_CONTAINER, webServicecontainerID);

    setWsContainer(context, wrapper, httpListener);

    webserviceContexts.put(new Key(path, moduleId), context);

    // register wsdl locations for service-ref resolution
    for (final Connector connector : connectors) {
        final StringBuilder fullContextpath;
        if (!WEBSERVICE_OLDCONTEXT_ACTIVE && !fakeDeployment) {
            String contextPath = context.getPath();
            if (contextPath == null ||  !contextPath.isEmpty()) {
                if (contextPath != null && !contextPath.startsWith("/")) {
                    contextPath = "/" + contextPath;
                } else if (contextPath == null) {
                    contextPath = "/";
                }
            }

            fullContextpath = new StringBuilder(contextPath);
            if (!WEBSERVICE_SUB_CONTEXT.equals("/")) {
                fullContextpath.append(WEBSERVICE_SUB_CONTEXT);
            }
            fullContextpath.append(path);
        } else {
            fullContextpath = new StringBuilder(context.getPath()).append(path);
        }

        try {
            final URI address = new URI(connector.getScheme(), null, host.getName(), connector.getPort(), fullContextpath.toString(), null, null);
            addresses.add(address.toString());
        } catch (final URISyntaxException ignored) {
            // no-op
        }
    }
}
 
Example 13
Source File: TomcatHessianRegistry.java    From tomee with Apache License 2.0 4 votes vote down vote up
@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);
}