Search in sources :

Example 41 with ServletContextEvent

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

the class WebAppContextTest method testServletContextListener.

@Test
public void testServletContextListener() throws Exception {
    Server server = new Server();
    HotSwapHandler swap = new HotSwapHandler();
    server.setHandler(swap);
    server.start();
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(System.getProperty("java.io.tmpdir"));
    final List<String> history = new ArrayList<>();
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I0");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D0");
        }
    });
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I1");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D1");
            throw new RuntimeException("Listener1 destroy broken");
        }
    });
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I2");
            throw new RuntimeException("Listener2 init broken");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D2");
        }
    });
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I3");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D3");
        }
    });
    try {
        swap.setHandler(context);
        context.start();
    } catch (Exception e) {
        history.add(e.getMessage());
    } finally {
        try {
            swap.setHandler(null);
        } catch (Exception e) {
            while (e.getCause() instanceof Exception) e = (Exception) e.getCause();
            history.add(e.getMessage());
        } finally {
        }
    }
    Assert.assertThat(history, Matchers.contains("I0", "I1", "I2", "Listener2 init broken", "D1", "D0", "Listener1 destroy broken"));
    server.stop();
}
Also used : Server(org.eclipse.jetty.server.Server) HotSwapHandler(org.eclipse.jetty.server.handler.HotSwapHandler) ServletContextListener(javax.servlet.ServletContextListener) ArrayList(java.util.ArrayList) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletContextEvent(javax.servlet.ServletContextEvent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 42 with ServletContextEvent

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

the class ContextHandler method startContext.

/* ------------------------------------------------------------ */
/**
     * Extensible startContext. this method is called from {@link ContextHandler#doStart()} instead of a call to super.doStart(). This allows derived classes to
     * insert additional handling (Eg configuration) before the call to super.doStart by this method will start contained handlers.
     * @throws Exception if unable to start the context
     *
     * @see org.eclipse.jetty.server.handler.ContextHandler.Context
     */
protected void startContext() throws Exception {
    String managedAttributes = _initParams.get(MANAGED_ATTRIBUTES);
    if (managedAttributes != null)
        addEventListener(new ManagedAttributeListener(this, StringUtil.csvSplit(managedAttributes)));
    super.doStart();
    // Call context listeners
    _destroySerletContextListeners.clear();
    if (!_servletContextListeners.isEmpty()) {
        ServletContextEvent event = new ServletContextEvent(_scontext);
        for (ServletContextListener listener : _servletContextListeners) {
            callContextInitialized(listener, event);
            _destroySerletContextListeners.add(listener);
        }
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 43 with ServletContextEvent

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

the class ServletContextHandlerTest method testReplaceHandler.

@Test
public void testReplaceHandler() throws Exception {
    ServletContextHandler servletContextHandler = new ServletContextHandler();
    ServletHolder sh = new ServletHolder(new TestServlet());
    servletContextHandler.addServlet(sh, "/foo");
    final AtomicBoolean contextInit = new AtomicBoolean(false);
    final AtomicBoolean contextDestroy = new AtomicBoolean(false);
    servletContextHandler.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            if (sce.getServletContext() != null)
                contextInit.set(true);
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            if (sce.getServletContext() != null)
                contextDestroy.set(true);
        }
    });
    ServletHandler shandler = servletContextHandler.getServletHandler();
    ResourceHandler rh = new ResourceHandler();
    servletContextHandler.insertHandler(rh);
    assertEquals(shandler, servletContextHandler.getServletHandler());
    assertEquals(rh, servletContextHandler.getHandler());
    assertEquals(rh.getHandler(), shandler);
    _server.setHandler(servletContextHandler);
    _server.start();
    assertTrue(contextInit.get());
    _server.stop();
    assertTrue(contextDestroy.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletContextListener(javax.servlet.ServletContextListener) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 44 with ServletContextEvent

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

the class CheBootstrapTest method environment_variables_prefixed_with_che_underscore_convert_double_underscores_into_one_underscore_in_variable_name.

@Test
public void environment_variables_prefixed_with_che_underscore_convert_double_underscores_into_one_underscore_in_variable_name() throws Exception {
    Properties cheProperties = new Properties();
    cheProperties.put("che.some.other.name_with_underscores", "che_value");
    cheProperties.put("che.some.name", "NULL");
    writePropertiesFile(che, "che.properties", cheProperties);
    Properties userProperties = new Properties();
    userProperties.put("che.some.other.name_with_underscores", "user_value");
    writePropertiesFile(userCongDir, "user.properties", userProperties);
    systemPropertiesHelper.property("che.some.other.name_with_underscores", "che_dot_system_property_value");
    ModuleScanner.modules.add(binder -> binder.bind(TestConfOverrideWithUnderscoresComponent.class));
    cheBootstrap.contextInitialized(new ServletContextEvent(servletContext));
    Injector injector = retrieveComponentFromServletContext(Injector.class);
    TestConfOverrideWithUnderscoresComponent testComponent = injector.getInstance(TestConfOverrideWithUnderscoresComponent.class);
    assertEquals(testComponent.otherString, System.getenv("CHE_SOME_OTHER_NAME__WITH__UNDERSCORES"));
}
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)

Example 45 with ServletContextEvent

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

the class CheBootstrapTest method system_properties_prefixed_with_che_dot_override_user_specified_and_che_properties.

@Test
public void system_properties_prefixed_with_che_dot_override_user_specified_and_che_properties() throws Exception {
    Properties cheProperties = new Properties();
    cheProperties.put("che.some.name", "che_value");
    cheProperties.put("che.some.other.name", "NULL");
    writePropertiesFile(che, "che.properties", cheProperties);
    Properties userProperties = new Properties();
    userProperties.put("che.some.name", "user_value");
    writePropertiesFile(userCongDir, "user.properties", userProperties);
    systemPropertiesHelper.property("che.some.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.string, "che_dot_system_property_value");
}
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)74 Test (org.junit.Test)43 MockServletContext (org.springframework.mock.web.test.MockServletContext)21 ServletContextListener (javax.servlet.ServletContextListener)19 ServletContext (javax.servlet.ServletContext)18 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