Search in sources :

Example 1 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project tomcat by apache.

the class StandardContext method listenerStop.

/**
     * Send an application stop event to all interested listeners.
     * @return <code>true</code> if all events were sent successfully,
     * or <code>false</code> otherwise.
     */
public boolean listenerStop() {
    if (log.isDebugEnabled())
        log.debug("Sending application stop events");
    boolean ok = true;
    Object[] listeners = getApplicationLifecycleListeners();
    if (listeners != null && listeners.length > 0) {
        ServletContextEvent event = new ServletContextEvent(getServletContext());
        ServletContextEvent tldEvent = null;
        if (noPluggabilityServletContext != null) {
            tldEvent = new ServletContextEvent(noPluggabilityServletContext);
        }
        for (int i = 0; i < listeners.length; i++) {
            int j = (listeners.length - 1) - i;
            if (listeners[j] == null)
                continue;
            if (listeners[j] instanceof ServletContextListener) {
                ServletContextListener listener = (ServletContextListener) listeners[j];
                try {
                    fireContainerEvent("beforeContextDestroyed", listener);
                    if (noPluggabilityListeners.contains(listener)) {
                        listener.contextDestroyed(tldEvent);
                    } else {
                        listener.contextDestroyed(event);
                    }
                    fireContainerEvent("afterContextDestroyed", listener);
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    fireContainerEvent("afterContextDestroyed", listener);
                    getLogger().error(sm.getString("standardContext.listenerStop", listeners[j].getClass().getName()), t);
                    ok = false;
                }
            }
            try {
                if (getInstanceManager() != null) {
                    getInstanceManager().destroyInstance(listeners[j]);
                }
            } catch (Throwable t) {
                t = ExceptionUtils.unwrapInvocationTargetException(t);
                ExceptionUtils.handleThrowable(t);
                getLogger().error(sm.getString("standardContext.listenerStop", listeners[j].getClass().getName()), t);
                ok = false;
            }
        }
    }
    // Annotation processing
    listeners = getApplicationEventListeners();
    if (listeners != null) {
        for (int i = 0; i < listeners.length; i++) {
            int j = (listeners.length - 1) - i;
            if (listeners[j] == null)
                continue;
            try {
                if (getInstanceManager() != null) {
                    getInstanceManager().destroyInstance(listeners[j]);
                }
            } catch (Throwable t) {
                t = ExceptionUtils.unwrapInvocationTargetException(t);
                ExceptionUtils.handleThrowable(t);
                getLogger().error(sm.getString("standardContext.listenerStop", listeners[j].getClass().getName()), t);
                ok = false;
            }
        }
    }
    setApplicationEventListeners(null);
    setApplicationLifecycleListeners(null);
    noPluggabilityServletContext = null;
    noPluggabilityListeners.clear();
    return ok;
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) ServletContextEvent(javax.servlet.ServletContextEvent) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 2 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project tomcat by apache.

the class StandardContext method listenerStart.

/**
     * Configure the set of instantiated application event listeners
     * for this Context.
     * @return <code>true</code> if all listeners wre
     * initialized successfully, or <code>false</code> otherwise.
     */
public boolean listenerStart() {
    if (log.isDebugEnabled())
        log.debug("Configuring application event listeners");
    // Instantiate the required listeners
    String[] listeners = findApplicationListeners();
    Object[] results = new Object[listeners.length];
    boolean ok = true;
    for (int i = 0; i < results.length; i++) {
        if (getLogger().isDebugEnabled())
            getLogger().debug(" Configuring event listener class '" + listeners[i] + "'");
        try {
            String listener = listeners[i];
            results[i] = getInstanceManager().newInstance(listener);
        } catch (Throwable t) {
            t = ExceptionUtils.unwrapInvocationTargetException(t);
            ExceptionUtils.handleThrowable(t);
            getLogger().error(sm.getString("standardContext.applicationListener", listeners[i]), t);
            ok = false;
        }
    }
    if (!ok) {
        getLogger().error(sm.getString("standardContext.applicationSkipped"));
        return false;
    }
    // Sort listeners in two arrays
    ArrayList<Object> eventListeners = new ArrayList<>();
    ArrayList<Object> lifecycleListeners = new ArrayList<>();
    for (int i = 0; i < results.length; i++) {
        if ((results[i] instanceof ServletContextAttributeListener) || (results[i] instanceof ServletRequestAttributeListener) || (results[i] instanceof ServletRequestListener) || (results[i] instanceof HttpSessionIdListener) || (results[i] instanceof HttpSessionAttributeListener)) {
            eventListeners.add(results[i]);
        }
        if ((results[i] instanceof ServletContextListener) || (results[i] instanceof HttpSessionListener)) {
            lifecycleListeners.add(results[i]);
        }
    }
    // list.
    for (Object eventListener : getApplicationEventListeners()) {
        eventListeners.add(eventListener);
    }
    setApplicationEventListeners(eventListeners.toArray());
    for (Object lifecycleListener : getApplicationLifecycleListeners()) {
        lifecycleListeners.add(lifecycleListener);
        if (lifecycleListener instanceof ServletContextListener) {
            noPluggabilityListeners.add(lifecycleListener);
        }
    }
    setApplicationLifecycleListeners(lifecycleListeners.toArray());
    if (getLogger().isDebugEnabled())
        getLogger().debug("Sending application start events");
    // Ensure context is not null
    getServletContext();
    context.setNewServletContextListenerAllowed(false);
    Object[] instances = getApplicationLifecycleListeners();
    if (instances == null || instances.length == 0) {
        return ok;
    }
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    ServletContextEvent tldEvent = null;
    if (noPluggabilityListeners.size() > 0) {
        noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
        tldEvent = new ServletContextEvent(noPluggabilityServletContext);
    }
    for (int i = 0; i < instances.length; i++) {
        if (!(instances[i] instanceof ServletContextListener))
            continue;
        ServletContextListener listener = (ServletContextListener) instances[i];
        try {
            fireContainerEvent("beforeContextInitialized", listener);
            if (noPluggabilityListeners.contains(listener)) {
                listener.contextInitialized(tldEvent);
            } else {
                listener.contextInitialized(event);
            }
            fireContainerEvent("afterContextInitialized", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            fireContainerEvent("afterContextInitialized", listener);
            getLogger().error(sm.getString("standardContext.listenerStart", instances[i].getClass().getName()), t);
            ok = false;
        }
    }
    return (ok);
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletContextListener(javax.servlet.ServletContextListener) ServletRequestListener(javax.servlet.ServletRequestListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 3 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project che by eclipse.

the class CheBootstrapTest method readsConfigurationPropertiesFromSystemProperties.

@Test
public void readsConfigurationPropertiesFromSystemProperties() throws Exception {
    setSystemProperties(createTestProperties());
    ModuleScanner.modules.add(binder -> binder.bind(TestSystemPropertiesComponent.class));
    cheBootstrap.contextInitialized(new ServletContextEvent(servletContext));
    Injector injector = retrieveComponentFromServletContext(Injector.class);
    TestSystemPropertiesComponent testComponent = injector.getInstance(TestSystemPropertiesComponent.class);
    assertEquals(testComponent.parameter_pair, Pair.of("a", "b"));
    assertEquals(testComponent.parameter_pair2, Pair.of("a", (String) null));
    assertEquals(testComponent.parameter_pair3, Pair.of("a", ""));
    assertEquals(testComponent.parameter_pair_array, new Pair[] { Pair.of("a", "b"), Pair.of("c", "d") });
    assertEquals(testComponent.nullable, null);
    assertEquals(testComponent.parameter_uri, new URI("file:/a/b/c"));
    assertEquals(testComponent.parameter_url, new URL("http://localhost"));
    assertEquals(testComponent.parameter_file, new File("/a/b/c"));
    assertEquals(testComponent.parameter_strings, new String[] { "a", "b", "c" });
    assertEquals(testComponent.parameter_int, 123);
    assertEquals(testComponent.parameter_long, 123);
    assertEquals(testComponent.parameter_bool, true);
    assertEquals(testComponent.someDir, new File(System.getProperty("java.io.tmpdir"), "/some_dir"));
    assertEquals(testComponent.suffixedPath, System.getenv("PATH") + pathSeparator + "some_path");
}
Also used : Injector(com.google.inject.Injector) URI(java.net.URI) File(java.io.File) ServletContextEvent(javax.servlet.ServletContextEvent) URL(java.net.URL) Test(org.testng.annotations.Test)

Example 4 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project che by eclipse.

the class CheBootstrapTest method tearDown.

@AfterMethod
public void tearDown() throws Exception {
    try {
        cheBootstrap.contextDestroyed(new ServletContextEvent(servletContext));
    } catch (Throwable ignored) {
    }
    systemPropertiesHelper.restoreFromBackup();
    IoUtil.deleteRecursive(che);
    IoUtil.deleteRecursive(userCongDir);
    File aliases = new File(che.getParent(), PROPERTIES_ALIASES_CONFIG_FILE);
    if (aliases.exists()) {
        aliases.delete();
    }
    ModuleScanner.modules.clear();
}
Also used : File(java.io.File) ServletContextEvent(javax.servlet.ServletContextEvent) AfterMethod(org.testng.annotations.AfterMethod)

Example 5 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project che by eclipse.

the class CheBootstrapTest method environment_variables_prefixed_with_che_underscore_override_che_dot_prefixed_system_and_user_specified_and_che_properties.

@Test
public void environment_variables_prefixed_with_che_underscore_override_che_dot_prefixed_system_and_user_specified_and_che_properties() throws Exception {
    Properties cheProperties = new Properties();
    cheProperties.put("che.some.other.name", "che_value");
    cheProperties.put("che.some.name", "NULL");
    writePropertiesFile(che, "che.properties", cheProperties);
    Properties userProperties = new Properties();
    userProperties.put("che.some.other.name", "user_value");
    writePropertiesFile(userCongDir, "user.properties", userProperties);
    systemPropertiesHelper.property("che.some.other.name", "che_dot_system_property_value");
    ModuleScanner.modules.add(binder -> binder.bind(TestConfOverrideComponent.class));
    cheBootstrap.contextInitialized(new ServletContextEvent(servletContext));
    Injector injector = retrieveComponentFromServletContext(Injector.class);
    TestConfOverrideComponent testComponent = injector.getInstance(TestConfOverrideComponent.class);
    assertEquals(testComponent.otherString, System.getenv("CHE_SOME_OTHER_NAME"));
}
Also used : Injector(com.google.inject.Injector) Properties(java.util.Properties) SystemPropertiesHelper.overrideSystemProperties(org.eclipse.che.commons.test.SystemPropertiesHelper.overrideSystemProperties) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.testng.annotations.Test)

Aggregations

ServletContextEvent (javax.servlet.ServletContextEvent)70 Test (org.junit.Test)42 MockServletContext (org.springframework.mock.web.test.MockServletContext)21 ServletContext (javax.servlet.ServletContext)18 ServletContextListener (javax.servlet.ServletContextListener)17 Injector (com.google.inject.Injector)10 ContextLoaderListener (org.springframework.web.context.ContextLoaderListener)10 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)8 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)8 Test (org.testng.annotations.Test)8 BrokerService (org.apache.activemq.broker.BrokerService)7 IOException (java.io.IOException)6 Properties (java.util.Properties)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 ArrayList (java.util.ArrayList)5 SystemPropertiesHelper.overrideSystemProperties (org.eclipse.che.commons.test.SystemPropertiesHelper.overrideSystemProperties)5 File (java.io.File)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3