Search in sources :

Example 11 with LifecycleListener

use of org.apache.catalina.LifecycleListener in project tomcat by apache.

the class HostConfig method deployDirectory.

/**
     * Deploy exploded webapp.
     * @param cn The context name
     * @param dir The path to the root folder of the weapp
     */
protected void deployDirectory(ContextName cn, File dir) {
    long startTime = 0;
    // Deploy the application in this directory
    if (log.isInfoEnabled()) {
        startTime = System.currentTimeMillis();
        log.info(sm.getString("hostConfig.deployDir", dir.getAbsolutePath()));
    }
    Context context = null;
    File xml = new File(dir, Constants.ApplicationContextXml);
    File xmlCopy = new File(host.getConfigBaseFile(), cn.getBaseName() + ".xml");
    DeployedApplication deployedApp;
    boolean copyThisXml = copyXML;
    try {
        if (deployXML && xml.exists()) {
            synchronized (digesterLock) {
                try {
                    context = (Context) digester.parse(xml);
                } catch (Exception e) {
                    log.error(sm.getString("hostConfig.deployDescriptor.error", xml), e);
                    context = new FailedContext();
                } finally {
                    digester.reset();
                    if (context == null) {
                        context = new FailedContext();
                    }
                }
            }
            if (copyThisXml == false && context instanceof StandardContext) {
                // Host is using default value. Context may override it.
                copyThisXml = ((StandardContext) context).getCopyXML();
            }
            if (copyThisXml) {
                Files.copy(xml.toPath(), xmlCopy.toPath());
                context.setConfigFile(xmlCopy.toURI().toURL());
            } else {
                context.setConfigFile(xml.toURI().toURL());
            }
        } else if (!deployXML && xml.exists()) {
            // Block deployment as META-INF/context.xml may contain security
            // configuration necessary for a secure deployment.
            log.error(sm.getString("hostConfig.deployDescriptor.blocked", cn.getPath(), xml, xmlCopy));
            context = new FailedContext();
        } else {
            context = (Context) Class.forName(contextClass).newInstance();
        }
        Class<?> clazz = Class.forName(host.getConfigClass());
        LifecycleListener listener = (LifecycleListener) clazz.newInstance();
        context.addLifecycleListener(listener);
        context.setName(cn.getName());
        context.setPath(cn.getPath());
        context.setWebappVersion(cn.getVersion());
        context.setDocBase(cn.getBaseName());
        host.addChild(context);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log.error(sm.getString("hostConfig.deployDir.error", dir.getAbsolutePath()), t);
    } finally {
        deployedApp = new DeployedApplication(cn.getName(), xml.exists() && deployXML && copyThisXml);
        // Fake re-deploy resource to detect if a WAR is added at a later
        // point
        deployedApp.redeployResources.put(dir.getAbsolutePath() + ".war", Long.valueOf(0));
        deployedApp.redeployResources.put(dir.getAbsolutePath(), Long.valueOf(dir.lastModified()));
        if (deployXML && xml.exists()) {
            if (copyThisXml) {
                deployedApp.redeployResources.put(xmlCopy.getAbsolutePath(), Long.valueOf(xmlCopy.lastModified()));
            } else {
                deployedApp.redeployResources.put(xml.getAbsolutePath(), Long.valueOf(xml.lastModified()));
                // Fake re-deploy resource to detect if a context.xml file is
                // added at a later point
                deployedApp.redeployResources.put(xmlCopy.getAbsolutePath(), Long.valueOf(0));
            }
        } else {
            // Fake re-deploy resource to detect if a context.xml file is
            // added at a later point
            deployedApp.redeployResources.put(xmlCopy.getAbsolutePath(), Long.valueOf(0));
            if (!xml.exists()) {
                deployedApp.redeployResources.put(xml.getAbsolutePath(), Long.valueOf(0));
            }
        }
        addWatchedResources(deployedApp, dir.getAbsolutePath(), context);
        // Add the global redeploy resources (which are never deleted) at
        // the end so they don't interfere with the deletion process
        addGlobalRedeployResources(deployedApp);
    }
    deployed.put(cn.getName(), deployedApp);
    if (log.isInfoEnabled()) {
        log.info(sm.getString("hostConfig.deployDir.finished", dir.getAbsolutePath(), Long.valueOf(System.currentTimeMillis() - startTime)));
    }
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext) LifecycleListener(org.apache.catalina.LifecycleListener) JarFile(java.util.jar.JarFile) File(java.io.File) IOException(java.io.IOException)

Example 12 with LifecycleListener

use of org.apache.catalina.LifecycleListener in project spring-boot by spring-projects.

the class TomcatServletWebServerFactory method configureContext.

/**
	 * Configure the Tomcat {@link Context}.
	 * @param context the Tomcat context
	 * @param initializers initializers to apply
	 */
protected void configureContext(Context context, ServletContextInitializer[] initializers) {
    TomcatStarter starter = new TomcatStarter(initializers);
    if (context instanceof TomcatEmbeddedContext) {
        // Should be true
        ((TomcatEmbeddedContext) context).setStarter(starter);
    }
    context.addServletContainerInitializer(starter, NO_CLASSES);
    for (LifecycleListener lifecycleListener : this.contextLifecycleListeners) {
        context.addLifecycleListener(lifecycleListener);
    }
    for (Valve valve : this.contextValves) {
        context.getPipeline().addValve(valve);
    }
    for (ErrorPage errorPage : getErrorPages()) {
        new TomcatErrorPage(errorPage).addToContext(context);
    }
    for (MimeMappings.Mapping mapping : getMimeMappings()) {
        context.addMimeMapping(mapping.getExtension(), mapping.getMimeType());
    }
    configureSession(context);
    for (TomcatContextCustomizer customizer : this.tomcatContextCustomizers) {
        customizer.customize(context);
    }
}
Also used : ErrorPage(org.springframework.boot.web.server.ErrorPage) Valve(org.apache.catalina.Valve) LifecycleListener(org.apache.catalina.LifecycleListener) MimeMappings(org.springframework.boot.web.server.MimeMappings)

Example 13 with LifecycleListener

use of org.apache.catalina.LifecycleListener in project spring-boot by spring-projects.

the class TomcatServletWebServerFactoryTests method tomcatListeners.

@Test
public void tomcatListeners() throws Exception {
    TomcatServletWebServerFactory factory = getFactory();
    LifecycleListener[] listeners = new LifecycleListener[4];
    for (int i = 0; i < listeners.length; i++) {
        listeners[i] = mock(LifecycleListener.class);
    }
    factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1]));
    factory.addContextLifecycleListeners(listeners[2], listeners[3]);
    this.webServer = factory.getWebServer();
    InOrder ordered = inOrder((Object[]) listeners);
    for (LifecycleListener listener : listeners) {
        ordered.verify(listener).lifecycleEvent(any(LifecycleEvent.class));
    }
}
Also used : InOrder(org.mockito.InOrder) LifecycleEvent(org.apache.catalina.LifecycleEvent) LifecycleListener(org.apache.catalina.LifecycleListener) Test(org.junit.Test)

Example 14 with LifecycleListener

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

the class TomcatWebAppBuilder method init.

/**
     * {@inheritDoc}
     */
@Override
public void init(final StandardContext standardContext) {
    if (isIgnored(standardContext)) {
        return;
    }
    // just adding a carriage return to get logs more readable
    logger.info("------------------------- " + Contexts.getHostname(standardContext).replace("_", hosts.getDefaultHost()) + " -> " + finalName(standardContext.getPath()));
    if (FORCE_RELOADABLE) {
        final ContextInfo ctxInfo = getContextInfo(standardContext);
        if (ctxInfo == null || (ctxInfo.appInfo != null && ctxInfo.appInfo.webAppAlone)) {
            // don't do it for ears
            standardContext.setReloadable(true);
        }
    }
    if (SKIP_TLD) {
        if (standardContext.getJarScanner() != null && standardContext.getJarScanner().getJarScanFilter() != null) {
            final JarScanFilter jarScanFilter = standardContext.getJarScanner().getJarScanFilter();
            if (StandardJarScanFilter.class.isInstance(jarScanFilter)) {
                StandardJarScanFilter.class.cast(jarScanFilter).setDefaultTldScan(false);
            }
        }
    }
    final String name = standardContext.getName();
    initJ2EEInfo(standardContext);
    File warFile = Contexts.warPath(standardContext);
    if (!warFile.exists()) {
        return;
    }
    if (!warFile.isDirectory()) {
        try {
            warFile = DeploymentLoader.unpack(warFile);
        } catch (final OpenEJBException e) {
            logger.error("can't unpack '" + warFile.getAbsolutePath() + "'");
        }
    }
    standardContext.setCrossContext(SystemInstance.get().getOptions().get(OPENEJB_CROSSCONTEXT_PROPERTY, false));
    standardContext.setNamingResources(new OpenEJBNamingResource(standardContext.getNamingResources()));
    String sessionManager = SystemInstance.get().getOptions().get(OPENEJB_SESSION_MANAGER_PROPERTY + "." + name, (String) null);
    if (sessionManager == null) {
        sessionManager = SystemInstance.get().getOptions().get(OPENEJB_SESSION_MANAGER_PROPERTY, (String) null);
    }
    if (sessionManager != null) {
        if (sessionManagerClass == null) {
            try {
                // the manager should be in standardclassloader
                sessionManagerClass = ParentClassLoaderFinder.Helper.get().loadClass(sessionManager);
            } catch (final ClassNotFoundException e) {
                logger.error("can't find '" + sessionManager + "', StandardManager will be used", e);
                sessionManagerClass = StandardManager.class;
            }
        }
        try {
            final Manager mgr = (Manager) sessionManagerClass.newInstance();
            standardContext.setManager(mgr);
        } catch (final Exception e) {
            logger.error("can't instantiate '" + sessionManager + "', StandardManager will be used", e);
        }
    }
    final LifecycleListener[] listeners = standardContext.findLifecycleListeners();
    for (final LifecycleListener l : listeners) {
        if (l instanceof ContextConfig) {
            standardContext.removeLifecycleListener(l);
        }
    }
    standardContext.addLifecycleListener(new OpenEJBContextConfig(new StandardContextInfo(standardContext)));
    // force manually the namingContextListener to merge jndi in an easier way
    final NamingContextListener ncl = new NamingContextListener();
    try {
        ncl.setName((String) getNamingContextName.invoke(standardContext));
    } catch (final Exception e) {
        ncl.setName(getId(standardContext));
    }
    ncl.setExceptionOnFailedWrite(standardContext.getJndiExceptionOnFailedWrite());
    standardContext.setNamingContextListener(ncl);
    standardContext.addLifecycleListener(ncl);
    standardContext.addLifecycleListener(new TomcatJavaJndiBinder());
    // listen some events
    standardContext.addContainerListener(new TomEEContainerListener());
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) StandardManager(org.apache.catalina.session.StandardManager) JarScanFilter(org.apache.tomcat.JarScanFilter) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter) OpenEJBContextConfig(org.apache.catalina.startup.OpenEJBContextConfig) LifecycleListener(org.apache.catalina.LifecycleListener) Manager(org.apache.catalina.Manager) InstanceManager(org.apache.tomcat.InstanceManager) StandardManager(org.apache.catalina.session.StandardManager) DeploymentExceptionManager(org.apache.openejb.assembler.classic.DeploymentExceptionManager) TransactionManager(javax.transaction.TransactionManager) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter) LifecycleException(org.apache.catalina.LifecycleException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ContextConfig(org.apache.catalina.startup.ContextConfig) OpenEJBContextConfig(org.apache.catalina.startup.OpenEJBContextConfig) NamingContextListener(org.apache.catalina.core.NamingContextListener) File(java.io.File) JarFile(java.util.jar.JarFile)

Example 15 with LifecycleListener

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

the class GlobalListenerSupport method forceFirstLifecycleListener.

/**
     * Update context lifecycle listeners.
     *
     * @param context tomcat context.
     */
private void forceFirstLifecycleListener(final StandardContext context) {
    final LifecycleListener[] listeners = context.findLifecycleListeners();
    // if we are already first return
    if (listeners.length > 0 && listeners[0] == this) {
        return;
    }
    // remove all of the current listeners
    for (final LifecycleListener listener : listeners) {
        context.removeLifecycleListener(listener);
    }
    // add this class (as first)
    context.addLifecycleListener(this);
    // add back all listeners
    for (final LifecycleListener listener : listeners) {
        if (listener != this) {
            context.addLifecycleListener(listener);
        }
    }
}
Also used : LifecycleListener(org.apache.catalina.LifecycleListener)

Aggregations

LifecycleListener (org.apache.catalina.LifecycleListener)22 Container (org.apache.catalina.Container)6 Valve (org.apache.catalina.Valve)6 File (java.io.File)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Lifecycle (org.apache.catalina.Lifecycle)5 JarFile (java.util.jar.JarFile)4 Context (org.apache.catalina.Context)4 StandardContext (org.apache.catalina.core.StandardContext)4 Realm (org.apache.catalina.Realm)3 ClusterValve (org.apache.catalina.ha.ClusterValve)3 FileInputStream (java.io.FileInputStream)2 Cluster (org.apache.catalina.Cluster)2 LifecycleException (org.apache.catalina.LifecycleException)2 Manager (org.apache.catalina.Manager)2 Connector (org.apache.catalina.connector.Connector)2 StandardHost (org.apache.catalina.core.StandardHost)2 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)2 BufferedOutputStream (java.io.BufferedOutputStream)1