Search in sources :

Example 11 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project Synthese_2BIN by TheYoungSensei.

the class Main method main.

public static void main(String[] args) throws Exception {
    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);
    StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File("./web").getAbsolutePath());
    WebResourceRoot resources = new StandardRoot(ctx);
    ctx.setResources(resources);
    tomcat.start();
    tomcat.getServer().await();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) StandardContext(org.apache.catalina.core.StandardContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) File(java.io.File) WebResourceRoot(org.apache.catalina.WebResourceRoot)

Example 12 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project tomee by apache.

the class ReloadingLoaderTest method initContext.

@Before
public void initContext() throws LifecycleException {
    final OpenEjbConfiguration configuration = new OpenEjbConfiguration();
    configuration.facilities = new FacilitiesInfo();
    final CoreContainerSystem containerSystem = new CoreContainerSystem(new IvmJndiFactory());
    SystemInstance.get().setComponent(OpenEjbConfiguration.class, configuration);
    SystemInstance.get().setComponent(ContainerSystem.class, containerSystem);
    SystemInstance.get().setComponent(WebAppEnricher.class, new WebAppEnricher() {

        @Override
        public URL[] enrichment(final ClassLoader webappClassLaoder) {
            return new URL[0];
        }
    });
    parentInstance = new AtomicReference<>(ParentClassLoaderFinder.Helper.get());
    loader = new TomEEWebappClassLoader(parentInstance.get()) {

        @Override
        public ClassLoader getInternalParent() {
            return parentInstance.get();
        }

        @Override
        protected void clearReferences() {
        // no-op: this test should be reworked to support it but in real life a loader is not stopped/started
        }
    };
    loader.init();
    final StandardRoot resources = new StandardRoot();
    loader.setResources(resources);
    resources.setContext(new StandardContext() {

        @Override
        public String getDocBase() {
            final File file = new File("target/foo");
            file.mkdirs();
            return file.getAbsolutePath();
        }

        @Override
        public String getMBeanKeyProperties() {
            return "foo";
        }

        {
        }
    });
    resources.start();
    loader.start();
    info = new AppInfo();
    info.appId = "test";
    context = new AppContext(info.appId, SystemInstance.get(), loader, new IvmContext(), new IvmContext(), true);
    containerSystem.addAppContext(context);
    final WebContext webDeployment = new WebContext(context);
    webDeployment.setId(context.getId());
    webDeployment.setClassLoader(loader);
    containerSystem.addWebContext(webDeployment);
}
Also used : IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) IvmJndiFactory(org.apache.openejb.core.ivm.naming.IvmJndiFactory) TomEEWebappClassLoader(org.apache.tomee.catalina.TomEEWebappClassLoader) CoreContainerSystem(org.apache.openejb.core.CoreContainerSystem) WebAppEnricher(org.apache.openejb.classloader.WebAppEnricher) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) AppInfo(org.apache.openejb.assembler.classic.AppInfo) FacilitiesInfo(org.apache.openejb.assembler.classic.FacilitiesInfo) StandardContext(org.apache.catalina.core.StandardContext) TomEEWebappClassLoader(org.apache.tomee.catalina.TomEEWebappClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Before(org.junit.Before)

Example 13 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project tomee by apache.

the class ReloadingLoaderTest method tomcatClassLoaderParentShouldntBeNulAfterAStopStartOtherwiseReloadIsBroken.

@Test
public void tomcatClassLoaderParentShouldntBeNulAfterAStopStartOtherwiseReloadIsBroken() throws Exception {
    final CxfRSService server = new CxfRSService();
    try {
        server.init(new Properties());
        server.start();
        server.afterApplicationCreated(new AssemblerAfterApplicationCreated(info, context, Collections.<BeanContext>emptyList()));
        {
            final ClassLoader beforeLoader = SystemInstance.get().getComponent(ContainerSystem.class).getWebContext("test").getClassLoader();
            assertSame(loader, beforeLoader);
            assertNotNull(beforeLoader);
            assertNotNull(Reflections.get(beforeLoader, "parent"));
        }
        loader.internalStop();
        server.undeploy(new AssemblerBeforeApplicationDestroyed(info, context));
        {
            final URLClassLoader afterLoader = URLClassLoader.class.cast(SystemInstance.get().getComponent(ContainerSystem.class).getWebContext("test").getClassLoader());
            assertSame(loader, afterLoader);
            assertNotNull(afterLoader);
            assertEquals(0, afterLoader.getURLs().length);
            assertEquals(LifecycleState.STOPPED, loader.getState());
        }
        final StandardRoot resources = new StandardRoot();
        loader.setResources(resources);
        resources.setContext(new StandardContext() {

            @Override
            public String getDocBase() {
                final File file = new File("target/foo");
                file.mkdirs();
                return file.getAbsolutePath();
            }

            @Override
            public String getMBeanKeyProperties() {
                return "foo";
            }

            {
            }
        });
        resources.start();
        loader.start();
        // TomcatWebAppBuilder ill catch start event from StandardContext and force a classloader
        // Reflections.set(loader, "parent", ParentClassLoaderFinder.Helper.get());
        parentInstance.set(ParentClassLoaderFinder.Helper.get());
        server.afterApplicationCreated(new AssemblerAfterApplicationCreated(info, context, Collections.<BeanContext>emptyList()));
        {
            final ClassLoader afterLoader = SystemInstance.get().getComponent(ContainerSystem.class).getWebContext("test").getClassLoader();
            assertSame(loader, afterLoader);
            assertNotNull(afterLoader);
            assertNotNull(Reflections.get(afterLoader, "parent"));
        }
        server.undeploy(new AssemblerBeforeApplicationDestroyed(info, context));
    } finally {
        server.stop();
    }
}
Also used : CoreContainerSystem(org.apache.openejb.core.CoreContainerSystem) ContainerSystem(org.apache.openejb.spi.ContainerSystem) StandardRoot(org.apache.catalina.webresources.StandardRoot) AssemblerBeforeApplicationDestroyed(org.apache.openejb.assembler.classic.event.AssemblerBeforeApplicationDestroyed) Properties(java.util.Properties) BeanContext(org.apache.openejb.BeanContext) URLClassLoader(java.net.URLClassLoader) StandardContext(org.apache.catalina.core.StandardContext) AssemblerAfterApplicationCreated(org.apache.openejb.assembler.classic.event.AssemblerAfterApplicationCreated) TomEEWebappClassLoader(org.apache.tomee.catalina.TomEEWebappClassLoader) URLClassLoader(java.net.URLClassLoader) CxfRSService(org.apache.openejb.server.cxf.rs.CxfRSService) File(java.io.File) Test(org.junit.Test)

Example 14 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project tomee by apache.

the class StandardContextCustomizer method customize.

public void customize(@Observes final LifecycleEvent event) {
    final Object data = event.getSource();
    if (!StandardContext.class.isInstance(data)) {
        return;
    }
    final StandardContext context = StandardContext.class.cast(data);
    final String contextRoot = module.getContextRoot();
    final String path = context.getPath();
    final boolean rightPath = (path.isEmpty() && contextRoot.equals(path)) || (contextRoot.startsWith("/") ? contextRoot : '/' + contextRoot).equals(path);
    if (!rightPath) {
        return;
    }
    switch(event.getType()) {
        case Lifecycle.BEFORE_START_EVENT:
            final StandardRoot resources = new StandardRoot(context);
            resources.setCachingAllowed(config.areWebResourcesCached());
            context.setResources(resources);
            if (!module.getProperties().containsKey("fakeJarLocation")) {
                context.setDocBase(module.getJarLocation());
            }
            // move last fake folder, tomcat is broken without it so we can't remove it
            final List allResources = List.class.cast(Reflections.get(resources, "allResources"));
            final Object mainResources = allResources.remove(1);
            allResources.add(mainResources);
            for (final URL url : module.getScannableUrls()) {
                final File file = URLs.toFile(url);
                final String absolutePath = file.getAbsolutePath();
                if (file.isDirectory()) {
                    resources.createWebResourceSet(WebResourceRoot.ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", absolutePath, "", "/");
                    if (new File(file, "META-INF/resources").exists()) {
                        resources.createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", absolutePath, "", "/META-INF/resources");
                    }
                } else {
                    if (absolutePath.endsWith(".jar") || Boolean.getBoolean("tomee.embedded.resources.add-war-as-jar")) {
                        resources.createWebResourceSet(WebResourceRoot.ResourceSetType.CLASSES_JAR, "/WEB-INF/lib", absolutePath, null, "/");
                        resources.createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", url, "/META-INF/resources");
                    }
                // else endsWith .war => ignore
                }
            }
            if (config.getCustomWebResources() != null) {
                for (final String web : config.getCustomWebResources()) {
                    final File file = new File(web);
                    if (file.isDirectory()) {
                        try {
                            resources.createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", file.toURI().toURL(), "/");
                        } catch (final MalformedURLException e) {
                            throw new IllegalArgumentException(e);
                        }
                    } else {
                        Logger.getLogger(StandardContextCustomizer.class.getName()).warning("'" + web + "' is not a directory, ignoring");
                    }
                }
            }
            if (config.getLoginConfig() != null) {
                context.setLoginConfig(config.getLoginConfig().build());
            }
            for (final SecurityConstaintBuilder sc : config.getSecurityConstraints()) {
                context.addConstraint(sc.build());
            }
            if (config.getWebXml() != null) {
                context.getServletContext().setAttribute(Globals.ALT_DD_ATTR, config.getWebXml());
            }
            if (loader != null) {
                context.setLoader(new ProvidedLoader(loader));
            }
            break;
        case Lifecycle.CONFIGURE_START_EVENT:
            SystemInstance.get().getComponent(TomcatWebAppBuilder.class).setFinderOnContextConfig(context, module.appModule());
            break;
        default:
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) TomcatWebAppBuilder(org.apache.tomee.catalina.TomcatWebAppBuilder) StandardRoot(org.apache.catalina.webresources.StandardRoot) URL(java.net.URL) StandardContext(org.apache.catalina.core.StandardContext) List(java.util.List) File(java.io.File) SecurityConstaintBuilder(org.apache.tomee.embedded.SecurityConstaintBuilder)

Example 15 with StandardRoot

use of org.apache.catalina.webresources.StandardRoot in project cxf by apache.

the class StatsServer method resourcesFrom.

private static WebResourceRoot resourcesFrom(final Context context, final String path) {
    final File additionResources = new File(path);
    final WebResourceRoot resources = new StandardRoot(context);
    resources.addPreResources(new DirResourceSet(resources, "/", additionResources.getAbsolutePath(), "/"));
    return resources;
}
Also used : DirResourceSet(org.apache.catalina.webresources.DirResourceSet) StandardRoot(org.apache.catalina.webresources.StandardRoot) File(java.io.File) WebResourceRoot(org.apache.catalina.WebResourceRoot)

Aggregations

StandardRoot (org.apache.catalina.webresources.StandardRoot)23 File (java.io.File)18 StandardContext (org.apache.catalina.core.StandardContext)14 Tomcat (org.apache.catalina.startup.Tomcat)13 Test (org.junit.Test)11 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)7 Context (org.apache.catalina.Context)6 WebResourceRoot (org.apache.catalina.WebResourceRoot)4 StandardJarScanner (org.apache.tomcat.util.scan.StandardJarScanner)4 URLClassLoader (java.net.URLClassLoader)3 ServletContext (jakarta.servlet.ServletContext)2 MalformedURLException (java.net.MalformedURLException)2 HashSet (java.util.HashSet)2 WebappLoader (org.apache.catalina.loader.WebappLoader)2 DirResourceSet (org.apache.catalina.webresources.DirResourceSet)2 CoreContainerSystem (org.apache.openejb.core.CoreContainerSystem)2 JarScanner (org.apache.tomcat.JarScanner)2 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)2 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)2 TomEEWebappClassLoader (org.apache.tomee.catalina.TomEEWebappClassLoader)2