Search in sources :

Example 6 with ServletContextEvent

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

the class CheBootstrapTest method propertiesFromUserSpecifiedLocationOverrideCheProperties.

@Test
public void propertiesFromUserSpecifiedLocationOverrideCheProperties() throws Exception {
    systemPropertiesHelper.property(CHE_LOCAL_CONF_DIR, userCongDir.getAbsolutePath());
    Properties cheProperties = new Properties();
    cheProperties.put("che.some.name", "che_value");
    writePropertiesFile(che, "che.properties", cheProperties);
    Properties userProperties = new Properties();
    userProperties.put("che.some.name", "user_value");
    writePropertiesFile(userCongDir, "user.properties", userProperties);
    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, "user_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)

Example 7 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project metrics by dropwizard.

the class InstrumentedFilterContextListenerTest method injectsTheMetricRegistryIntoTheServletContext.

@Test
public void injectsTheMetricRegistryIntoTheServletContext() throws Exception {
    final ServletContext context = mock(ServletContext.class);
    final ServletContextEvent event = mock(ServletContextEvent.class);
    when(event.getServletContext()).thenReturn(context);
    listener.contextInitialized(event);
    verify(context).setAttribute("com.codahale.metrics.servlet.InstrumentedFilter.registry", registry);
}
Also used : ServletContext(javax.servlet.ServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 8 with ServletContextEvent

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

the class ContextHandler method stopContext.

/* ------------------------------------------------------------ */
protected void stopContext() throws Exception {
    //stop all the handler hierarchy
    super.doStop();
    //Call the context listeners
    ServletContextEvent event = new ServletContextEvent(_scontext);
    Collections.reverse(_destroySerletContextListeners);
    MultiException ex = new MultiException();
    for (ServletContextListener listener : _destroySerletContextListeners) {
        try {
            callContextDestroyed(listener, event);
        } catch (Exception x) {
            ex.add(x);
        }
    }
    ex.ifExceptionThrow();
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) MultiException(org.eclipse.jetty.util.MultiException) ServletContextEvent(javax.servlet.ServletContextEvent) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 9 with ServletContextEvent

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

the class TestJNDI method testThreadContextClassloaderAndCurrentContext.

@Test
public void testThreadContextClassloaderAndCurrentContext() throws Exception {
    //create a jetty context, and start it so that its classloader it created
    //and it is the current context
    ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
    ContextHandler ch = new ContextHandler();
    URLClassLoader chLoader = new URLClassLoader(new URL[0], currentLoader);
    ch.setClassLoader(chLoader);
    Server server = new Server();
    HandlerList hl = new HandlerList();
    server.setHandler(hl);
    hl.addHandler(ch);
    //Create another one
    ContextHandler ch2 = new ContextHandler();
    URLClassLoader ch2Loader = new URLClassLoader(new URL[0], currentLoader);
    ch2.setClassLoader(ch2Loader);
    hl.addHandler(ch2);
    try {
        ch.setContextPath("/ch");
        ch.addEventListener(new ServletContextListener() {

            private Context comp;

            private Object testObj = new Object();

            public void contextInitialized(ServletContextEvent sce) {
                try {
                    InitialContext initCtx = new InitialContext();
                    Context java = (Context) initCtx.lookup("java:");
                    assertNotNull(java);
                    comp = (Context) initCtx.lookup("java:comp");
                    assertNotNull(comp);
                    Context env = ((Context) comp).createSubcontext("env");
                    assertNotNull(env);
                    env.bind("ch", testObj);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }

            public void contextDestroyed(ServletContextEvent sce) {
                try {
                    assertNotNull(comp);
                    assertEquals(testObj, comp.lookup("env/ch"));
                    comp.destroySubcontext("env");
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        //Starting the context makes it current and creates a classloader for it
        ch.start();
        ch2.setContextPath("/ch2");
        ch2.addEventListener(new ServletContextListener() {

            private Context comp;

            private Object testObj = new Object();

            public void contextInitialized(ServletContextEvent sce) {
                try {
                    InitialContext initCtx = new InitialContext();
                    comp = (Context) initCtx.lookup("java:comp");
                    assertNotNull(comp);
                    //another context's bindings should not be visible
                    Context env = ((Context) comp).createSubcontext("env");
                    try {
                        env.lookup("ch");
                        fail("java:comp/env visible from another context!");
                    } catch (NameNotFoundException e) {
                    //expected
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }

            public void contextDestroyed(ServletContextEvent sce) {
                try {
                    assertNotNull(comp);
                    comp.destroySubcontext("env");
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        //make the new context the current one
        ch2.start();
    } finally {
        ch.stop();
        ch2.stop();
        Thread.currentThread().setContextClassLoader(currentLoader);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) Server(org.eclipse.jetty.server.Server) ServletContextListener(javax.servlet.ServletContextListener) NameNotFoundException(javax.naming.NameNotFoundException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 10 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project JessMA by ldcsaa.

the class SpringInjectFilter method init.

@Override
public void init() {
    servletContext = HttpHelper.getServletContext();
    springMap = new HashMap<CoupleKey<Class<?>, Method>, SpringAttr[]>();
    context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (context == null) {
        listener = new ContextLoaderListener();
        listener.contextInitialized(new ServletContextEvent(servletContext));
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) CoupleKey(org.jessma.util.CoupleKey) ServletContextEvent(javax.servlet.ServletContextEvent)

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