Example usage for javax.servlet ServletContextListener contextDestroyed

List of usage examples for javax.servlet ServletContextListener contextDestroyed

Introduction

In this page you can find the example usage for javax.servlet ServletContextListener contextDestroyed.

Prototype

default public void contextDestroyed(ServletContextEvent sce) 

Source Link

Document

Receives notification that the ServletContext is about to be shut down.

Usage

From source file:org.grails.plugin.batch.Launcher.java

public void destroy() {
    logDebug("destroy(): begin");
    synchronized (destroyLock) {

        if (!this.destroyed) {
            GrailsApplication grailsApplication = ApplicationHolder.getApplication();

            if (grailsApplication != null) {
                DefaultGrailsMainClass main = getMainClass(grailsApplication);
                if (main != null) {
                    main.callDestroy();/*from www  .j av a  2 s . c o  m*/
                }

                GrailsClass[] bootstraps = grailsApplication.getArtefacts(BootstrapArtefactHandler.TYPE);
                for (int i = bootstraps.length - 1; i >= 0; i--) {
                    GrailsClass bootstrap = bootstraps[i];
                    ((GrailsBootstrapClass) bootstrap).callDestroy();
                }
            }

            {
                ServletContextEvent event = new ServletContextEvent(servletContext);
                for (int i = servletContextListeners.length - 1; i >= 0; i--) {
                    ServletContextListener l = servletContextListeners[i];
                    l.contextDestroyed(event);
                }
            }

            if (shutdownHook != null) {
                if (!shutdownHook.isAlive()) {
                    Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
                    logDebug(true, "destroy(): shutdown hook removed");
                }
                this.shutdownHook = null;
            }

            servletContext = null;
        }

        this.destroyed = true;
        logDebug(true, "destroy(): end");
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.startup.StartupManager.java

/**
 * Notify the listeners that the context is being destroyed, in the reverse
 * order from how they were notified at initialization.
 *///from  www .  j  a v a  2  s  . com
@Override
public void contextDestroyed(ServletContextEvent sce) {
    List<ServletContextListener> destroyList = new ArrayList<ServletContextListener>(initializeList);
    Collections.reverse(destroyList);

    for (ServletContextListener listener : destroyList) {
        try {
            log.debug("Destroying '" + listener.getClass().getName() + "'");
            listener.contextDestroyed(sce);
        } catch (Exception e) {
            log.error("Unexpected exception from contextDestroyed() on '" + listener.getClass().getName() + "'",
                    e);
        } catch (Throwable t) {
            log.fatal("Unexpected error from contextDestroyed() on '" + listener.getClass().getName() + "'", t);
            throw t;
        }
    }
    log.info("Called 'contextDestroyed' on all listeners.");
}

From source file:com.jsmartframework.web.manager.ServletControl.java

@Override
public void destroy() {
    // Call registered WebContextListeners
    for (ServletContextListener contextListener : HANDLER.contextListeners) {
        contextListener.contextDestroyed(new ServletContextEvent(getServletContext()));
    }/*from w ww.  j  av  a2 s .  c  om*/
    super.destroy();
}

From source file:org.ireland.jnetty.webapp.WebApp.java

/**
 * ?publish ContextDestroyed Event /*from w w  w  .j a  v a  2  s.co  m*/
 */
protected void publishContextDestroyedEvent(ServletContextEvent event) {
    if (_contextListeners != null) {
        for (int i = _contextListeners.size() - 1; i >= 0; i--) {
            ServletContextListener listener = _contextListeners.get(i);

            try {
                listener.contextDestroyed(event);
            } catch (Exception e) {
                log.warn(e.toString(), e);
            }
        }
    }
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/applicationContext.xml "
                    + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);/*www . j  a  va  2s  . c  om*/
    PortletContextLoader contextLoader = (PortletContextLoader) sc
            .getAttribute(PortletApplicationContextUtils2.ROOT_PORTLET_APPLICATION_CONTEXT_LOADER_ATTRIBUTE);
    assertNotNull(contextLoader);

    //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
    //the portlet app context due to lack of PortletContext reference
    MockPortletContext pc = new MockPortletContext(sc);
    PortletApplicationContext context = PortletApplicationContextUtils2.getPortletApplicationContext(pc);

    assertTrue("Correct PortletApplicationContext exposed in PortletContext",
            context instanceof ContribXmlPortletApplicationContext);
    assertTrue(PortletContextLoader
            .getCurrentPortletApplicationContext() instanceof ContribXmlPortletApplicationContext);
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertTrue("Has father", context.containsBean("father"));
    assertTrue("Has rod", context.containsBean("rod"));
    assertTrue("Has kerry", context.containsBean("kerry"));
    assertTrue("Not destroyed", !lb.isDestroyed());
    assertFalse(context.containsBean("beans1.bean1"));
    assertFalse(context.containsBean("beans1.bean2"));
    listener.contextDestroyed(event);
    assertTrue("Destroyed", lb.isDestroyed());
    assertNull(sc.getAttribute(PortletApplicationContext.ROOT_PORTLET_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(PortletContextLoader.getCurrentPortletApplicationContext());
}

From source file:org.apache.catalina.core.StandardContext.java

/**
 * Send an application stop event to all interested listeners.
 * Return <code>true</code> if all events were sent successfully,
 * or <code>false</code> otherwise.
 *//*ww  w  . jav a  2  s. c  om*/
public boolean listenerStop() {

    if (log.isDebugEnabled())
        log.debug("Sending application stop events");

    boolean ok = true;
    Object listeners[] = getApplicationLifecycleListeners();
    if (listeners == null)
        return (ok);
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    for (int i = 0; i < listeners.length; i++) {
        int j = (listeners.length - 1) - i;
        if (listeners[j] == null)
            continue;
        if (!(listeners[j] instanceof ServletContextListener))
            continue;
        ServletContextListener listener = (ServletContextListener) listeners[j];
        try {
            fireContainerEvent("beforeContextDestroyed", listener);
            listener.contextDestroyed(event);
            fireContainerEvent("beforeContextDestroyed", listener);
        } catch (Throwable t) {
            fireContainerEvent("beforeContextDestroyed", listener);
            getServletContext()
                    .log(sm.getString("standardContext.listenerStop", listeners[j].getClass().getName()), t);
            ok = false;
        }
    }
    setApplicationEventListeners(null);
    setApplicationLifecycleListeners(null);
    return (ok);

}

From source file:org.polymap.service.geoserver.GeoServerWms.java

public void destroy() {
    log.debug("destroy(): ...");
    super.destroy();
    if (dispatcher != null) {
        ClassLoader threadLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(context.cl);

        try {//w w w  . j a  v  a2s  . c  om
            // listeners
            ServletContextEvent ev = new ServletContextEvent(context);
            for (ServletContextListener loader : loaders) {
                loader.contextDestroyed(ev);
            }
            loaders = null;

            // dispatcher
            dispatcher.destroy();
            dispatcher = null;

            context.destroy();
            context = null;
        } catch (IOException e) {
            log.warn("", e);
        } finally {
            Thread.currentThread().setContextClassLoader(threadLoader);
        }
    }
}