Java Code Examples for org.apache.catalina.core.StandardContext#getParentClassLoader()

The following examples show how to use org.apache.catalina.core.StandardContext#getParentClassLoader() . 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: ArkTomcatServletWebServerFactory.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
@Override
protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
    if (host.getState() == LifecycleState.NEW) {
        super.prepareContext(host, initializers);
    } else {
        File documentRoot = getValidDocumentRoot();
        StandardContext context = new StandardContext();
        if (documentRoot != null) {
            context.setResources(new StandardRoot(context));
        }
        context.setName(getContextPath());
        context.setDisplayName(getDisplayName());
        context.setPath(getContextPath());
        File docBase = (documentRoot != null) ? documentRoot : createTempDir("tomcat-docbase");
        context.setDocBase(docBase.getAbsolutePath());
        context.addLifecycleListener(new Tomcat.FixContextListener());
        context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
        resetDefaultLocaleMapping(context);
        addLocaleMappings(context);
        context.setUseRelativeRedirects(false);
        configureTldSkipPatterns(context);
        WebappLoader loader = new WebappLoader(context.getParentClassLoader());
        loader
            .setLoaderClass("com.alipay.sofa.ark.web.embed.tomcat.ArkTomcatEmbeddedWebappClassLoader");
        loader.setDelegate(true);
        context.setLoader(loader);
        if (isRegisterDefaultServlet()) {
            addDefaultServlet(context);
        }
        if (shouldRegisterJspServlet()) {
            addJspServlet(context);
            addJasperInitializer(context);
        }
        context.addLifecycleListener(new StaticResourceConfigurer(context));
        ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
        context.setParent(host);
        configureContext(context, initializersToUse);
        host.addChild(context);
    }
}
 
Example 2
Source File: TestVirtualContext.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testAdditionalWebInfClassesPaths() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0-virtual-webapp/src/main/webapp");
    // app dir is relative to server home
    StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test",
        appDir.getAbsolutePath());
    File tempFile = File.createTempFile("virtualWebInfClasses", null);

    File additionWebInfClasses = new File(tempFile.getAbsolutePath() + ".dir");
    Assert.assertTrue(additionWebInfClasses.mkdirs());
    File targetPackageForAnnotatedClass =
        new File(additionWebInfClasses,
            MyAnnotatedServlet.class.getPackage().getName().replace('.', '/'));
    Assert.assertTrue(targetPackageForAnnotatedClass.mkdirs());
    InputStream annotatedServletClassInputStream =
        this.getClass().getResourceAsStream(
            MyAnnotatedServlet.class.getSimpleName() + ".class");
    FileOutputStream annotatedServletClassOutputStream =
        new FileOutputStream(new File(targetPackageForAnnotatedClass,
            MyAnnotatedServlet.class.getSimpleName() + ".class"));
    IOUtils.copy(annotatedServletClassInputStream, annotatedServletClassOutputStream);
    annotatedServletClassInputStream.close();
    annotatedServletClassOutputStream.close();

    VirtualWebappLoader loader = new VirtualWebappLoader(ctx.getParentClassLoader());
    loader.setVirtualClasspath("test/webapp-3.0-virtual-webapp/target/classes;" + //
        "test/webapp-3.0-virtual-library/target/classes;" + //
        additionWebInfClasses.getAbsolutePath());
    ctx.setLoader(loader);

    tomcat.start();
    // first test that without the setting on StandardContext the annotated
    // servlet is not detected
    assertPageContains("/test/annotatedServlet", MyAnnotatedServlet.MESSAGE, 404);

    tomcat.stop();

    // then test that if we configure StandardContext with the additional
    // path, the servlet is detected
    // ctx.setAdditionalVirtualWebInfClasses(additionWebInfClasses.getAbsolutePath());
    VirtualDirContext resources = new VirtualDirContext();
    resources.setExtraResourcePaths("/WEB-INF/classes=" + additionWebInfClasses);
    ctx.setResources(resources);

    tomcat.start();
    assertPageContains("/test/annotatedServlet", MyAnnotatedServlet.MESSAGE);
    tomcat.stop();
    FileUtils.deleteDirectory(additionWebInfClasses);
    tempFile.delete();
}