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

The following examples show how to use org.apache.catalina.core.StandardContext#getLoader() . 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: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void beforeStop(final StandardContext standardContext) {
    final ClassLoader classLoader = standardContext.getLoader().getClassLoader();

    // if it is not our custom loader clean up now otherwise wait afterStop
    if (!(standardContext.getLoader() instanceof LazyStopLoader)) {
        jsfClasses.remove(classLoader);
    }

    // ensure we can stop it lazily - before all was in the classloader, now we align webresourceroot on the classloader config
    // we wrap it only here to not modify at all runtime
    if (!LazyStopStandardRoot.class.isInstance(standardContext.getResources())
            && TomEEWebappClassLoader.class.isInstance(classLoader) && !TomEEWebappClassLoader.class.cast(classLoader).isForceStopPhase()) {
        final LazyStopStandardRoot standardRoot = new LazyStopStandardRoot(standardContext.getResources());
        Reflections.set(standardContext, "resources", standardRoot);
        TomEEWebappClassLoader.class.cast(classLoader).setWebResourceRoot(standardRoot); // cause Assembler relies on the classloader only
    }
}
 
Example 2
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void destroy(final StandardContext standardContext) {
    final Loader standardContextLoader = standardContext.getLoader();
    if (LazyStopLoader.class.isInstance(standardContextLoader)) {
        final Loader delegate = LazyStopLoader.class.cast(standardContextLoader).getDelegateLoader();
        if (TomEEWebappLoader.class.isInstance(delegate)) {
            final TomEEWebappLoader webappLoader = TomEEWebappLoader.class.cast(delegate);
            final ClassLoader loader = webappLoader.internalLoader();
            webappLoader.clearLoader();
            if (TomEEWebappClassLoader.class.isInstance(loader)) {
                TomEEWebappClassLoader.class.cast(loader).internalDestroy();
            }
        }
    }

    final WebResourceRoot root = standardContext.getResources();
    if (LazyStopStandardRoot.class.isInstance(root)) {
        try {
            LazyStopStandardRoot.class.cast(root).internalDestroy();
        } catch (final LifecycleException e) {
            throw new IllegalStateException(e);
        }
    }
}
 
Example 3
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void initContextLoader(final StandardContext standardContext) {
    final Loader standardContextLoader = standardContext.getLoader();
    if (standardContextLoader != null
            && (
            (!TomEEWebappLoader.class.equals(standardContextLoader.getClass())
                && !WebappLoader.class.equals(standardContextLoader.getClass()))
                    || (WebappLoader.class.equals(standardContextLoader.getClass())
                            && !WebappLoader.class.cast(standardContextLoader).getLoaderClass().startsWith("org.apache.tom")))
            ) {
        // custom loader, we don't know it
        // and since we don't have a full delegate pattern for our lazy stop loader
        // simply skip lazy stop loader - normally sides effect will be an early shutdown for ears and some particular features
        // only affecting the app if the classes were not laoded at all
        return;
    }

    if (standardContextLoader != null && TomEEWebappLoader.class.isInstance(standardContextLoader)) {
        standardContextLoader.setContext(standardContext);
        return; // no need to replace the loader
    }

    // we just want to wrap it to lazy stop it (afterstop)
    // to avoid classnotfound in @PreDestoy or destroyApplication()
    final TomEEWebappLoader loader = new TomEEWebappLoader();
    loader.setDelegate(standardContext.getDelegate());
    loader.setLoaderClass(TomEEWebappClassLoader.class.getName());

    final Loader lazyStopLoader = new LazyStopLoader(loader);
    standardContext.setLoader(lazyStopLoader);
}
 
Example 4
Source File: StandardContextSF.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Store the specified context element children.
 *
 * @param aWriter Current output writer
 * @param indent Indentation level
 * @param aContext Context to store
 * @param parentDesc The element description
 * @throws Exception Configuration storing error
 */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aContext,
        StoreDescription parentDesc) throws Exception {
    if (aContext instanceof StandardContext) {
        StandardContext context = (StandardContext) aContext;
        // Store nested <Listener> elements
        LifecycleListener listeners[] = context.findLifecycleListeners();
        ArrayList<LifecycleListener> listenersArray = new ArrayList<>();
        for (LifecycleListener listener : listeners) {
            if (!(listener instanceof ThreadLocalLeakPreventionListener)) {
                listenersArray.add(listener);
            }
        }
        storeElementArray(aWriter, indent, listenersArray.toArray());

        // Store nested <Valve> elements
        Valve valves[] = context.getPipeline().getValves();
        storeElementArray(aWriter, indent, valves);

        // Store nested <Loader> elements
        Loader loader = context.getLoader();
        storeElement(aWriter, indent, loader);

        // Store nested <Manager> elements
        if (context.getCluster() == null || !context.getDistributable()) {
            Manager manager = context.getManager();
            storeElement(aWriter, indent, manager);
        }

        // Store nested <Realm> element
        Realm realm = context.getRealm();
        if (realm != null) {
            Realm parentRealm = null;
            // @TODO is this case possible?
            if (context.getParent() != null) {
                parentRealm = context.getParent().getRealm();
            }
            if (realm != parentRealm) {
                storeElement(aWriter, indent, realm);
            }
        }
        // Store nested resources
        WebResourceRoot resources = context.getResources();
        storeElement(aWriter, indent, resources);

        // Store nested <WrapperListener> elements
        String wLifecycles[] = context.findWrapperLifecycles();
        getStoreAppender().printTagArray(aWriter, "WrapperListener",
                indent + 2, wLifecycles);
        // Store nested <WrapperLifecycle> elements
        String wListeners[] = context.findWrapperListeners();
        getStoreAppender().printTagArray(aWriter, "WrapperLifecycle",
                indent + 2, wListeners);

        // Store nested <Parameter> elements
        ApplicationParameter[] appParams = context
                .findApplicationParameters();
        storeElementArray(aWriter, indent, appParams);

        // Store nested naming resources elements (EJB,Resource,...)
        NamingResourcesImpl nresources = context.getNamingResources();
        storeElement(aWriter, indent, nresources);

        // Store nested watched resources <WatchedResource>
        String[] wresources = context.findWatchedResources();
        wresources = filterWatchedResources(context, wresources);
        getStoreAppender().printTagArray(aWriter, "WatchedResource",
                indent + 2, wresources);

        // Store nested <JarScanner> elements
        JarScanner jarScanner = context.getJarScanner();
        storeElement(aWriter, indent, jarScanner);

        // Store nested <CookieProcessor> elements
        CookieProcessor cookieProcessor = context.getCookieProcessor();
        storeElement(aWriter, indent, cookieProcessor);
    }
}
 
Example 5
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 4 votes vote down vote up
private static DeploymentLoader.ExternalConfiguration configuredClasspath(final StandardContext standardContext) {
    Loader loader = standardContext.getLoader();
    if (loader != null && LazyStopLoader.class.isInstance(loader)) {
        loader = LazyStopLoader.class.cast(loader).getDelegateLoader();
    }
    if (loader != null) {
        final ClassLoader cl = standardContext.getLoader().getClassLoader();
        if (cl == null) {
            return null;
        }

        final Collection<String> cp = new LinkedList<>();

        final WebResourceRoot webResources = standardContext.getResources();
        if (webResources != null) { // to enhance
            for (final WebResourceSet[] sets : asList(webResources.getPreResources(), webResources.getPostResources(), webResources.getJarResources())) {
                for (final WebResourceSet wr : sets) {
                    final URL base = wr.getBaseUrl();
                    if (base != null) {
                        final File baseFile = URLs.toFile(base);
                        if (baseFile.isDirectory()) {
                            final String[] libs = wr.list("/WEB-INF/lib/");
                            if (libs != null) {
                                for (final String resource : libs) {
                                    cp.add(new File(baseFile, resource).getAbsolutePath());
                                }
                            }

                            final WebResource classes = wr.getResource("/WEB-INF/classes/");
                            if (classes != null) {
                                final String path = classes.getCanonicalPath();
                                if (path != null) {
                                    cp.add(path);
                                }
                            }
                        } else if (baseFile.exists() && baseFile.getName().endsWith(".jar") && wr.getResource("/WEB-INF/classes/").exists()) {
                            try {
                                cp.add(baseFile.getCanonicalPath());
                            } catch (final IOException e) {
                                throw new IllegalStateException(e);
                            }
                        }
                    }
                }
            }
        }

        if (!cp.isEmpty()) {
            return new DeploymentLoader.ExternalConfiguration(cp.toArray(new String[cp.size()]), null /*for now doesnt make sense, todo: configure*/);
        }
    }
    return null;
}