Search in sources :

Example 26 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project hevelian-activemq by Hevelian.

the class WebBrokerInitializerTest method contextInitialized_waitUntilStartedReturnsFalse_ExcThrown.

@Test(expected = BrokerLifecycleException.class)
public void contextInitialized_waitUntilStartedReturnsFalse_ExcThrown() throws Exception {
    ServletContext sc = mock(ServletContext.class);
    BrokerService broker = mock(BrokerService.class);
    doReturn(false).when(broker).waitUntilStarted();
    WebBrokerInitializer i = spy(WebBrokerInitializer.class);
    doReturn(broker).when(i).createBroker(sc);
    i.contextInitialized(new ServletContextEvent(sc));
}
Also used : ServletContext(javax.servlet.ServletContext) BrokerService(org.apache.activemq.broker.BrokerService) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 27 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project hevelian-activemq by Hevelian.

the class ServletContextHolderInitializerTest method contextDestroyed.

@Test
public void contextDestroyed() {
    ServletContextHolderInitializer i = new ServletContextHolderInitializer();
    ServletContext sc = Mockito.mock(ServletContext.class);
    ServletContextHolder.setServletContext(sc);
    i.contextDestroyed(new ServletContextEvent(sc));
    assertFalse(ServletContextHolder.isServletContextSet());
}
Also used : ServletContext(javax.servlet.ServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 28 with ServletContextEvent

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

the class HttpUtil method addServlet.

public static boolean addServlet(final String classname, final WebContext wc, final String mapping) {
    final HttpListenerRegistry registry = SystemInstance.get().getComponent(HttpListenerRegistry.class);
    if (registry == null || mapping == null) {
        return false;
    }
    final ServletListener listener;
    try {
        ServletContext servletContext = wc.getServletContext();
        if (servletContext == null) {
            servletContext = SystemInstance.get().getComponent(ServletContext.class);
        }
        if ("javax.faces.webapp.FacesServlet".equals(classname)) {
            try {
                // faking it to let the FacesServlet starting
                // NOTE: needs myfaces-impl + tomcat-jasper (JspFactory)
                // TODO: handle the whole lifecycle (cleanup mainly) + use myfaces SPI to make scanning really faster (take care should work in tomee were we already have it impl)
                final Class<?> mfListenerClass = wc.getClassLoader().loadClass("org.apache.myfaces.webapp.StartupServletContextListener");
                final ServletContextListener servletContextListener = ServletContextListener.class.cast(mfListenerClass.newInstance());
                servletContext.setAttribute("javax.enterprise.inject.spi.BeanManager", new InjectableBeanManager(wc.getWebBeansContext().getBeanManagerImpl()));
                final Thread thread = Thread.currentThread();
                final ClassLoader old = setClassLoader(wc, thread);
                try {
                    servletContextListener.contextInitialized(new ServletContextEvent(servletContext));
                } finally {
                    thread.setContextClassLoader(old);
                }
                servletContext.removeAttribute("javax.enterprise.inject.spi.BeanManager");
            } catch (final Exception e) {
            // no-op
            }
        }
        final Thread thread = Thread.currentThread();
        final ClassLoader old = setClassLoader(wc, thread);
        try {
            listener = new ServletListener((Servlet) wc.newInstance(wc.getClassLoader().loadClass(classname)), wc.getContextRoot());
            final ServletContext sc = servletContext;
            listener.getDelegate().init(new ServletConfig() {

                @Override
                public String getServletName() {
                    return classname;
                }

                @Override
                public ServletContext getServletContext() {
                    return sc;
                }

                @Override
                public String getInitParameter(final String s) {
                    return sc.getInitParameter(s);
                }

                @Override
                public Enumeration<String> getInitParameterNames() {
                    final Enumeration<String> parameterNames = sc.getInitParameterNames();
                    return parameterNames == null ? Collections.<String>emptyEnumeration() : parameterNames;
                }
            });
        } finally {
            thread.setContextClassLoader(old);
        }
    } catch (final Exception e) {
        throw new OpenEJBRuntimeException(e);
    }
    registry.addHttpListener(listener, pattern(wc.getContextRoot(), "/".equals(mapping) ? "/*" : mapping));
    return true;
}
Also used : Enumeration(java.util.Enumeration) ServletContextListener(javax.servlet.ServletContextListener) InjectableBeanManager(org.apache.webbeans.container.InjectableBeanManager) ServletConfig(javax.servlet.ServletConfig) HttpListenerRegistry(org.apache.openejb.server.httpd.HttpListenerRegistry) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ServletListener(org.apache.openejb.server.httpd.ServletListener) ServletContext(javax.servlet.ServletContext) Servlet(javax.servlet.Servlet) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 29 with ServletContextEvent

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

the class LiveReloadInstaller method install.

public static void install(String path, final int port, final String folder) {
    final Server server = TomcatHelper.getServer();
    if (server == null) {
        throw new IllegalStateException("tomcat not yet starting");
    }
    // checking which one is localhost could be better but should be fine
    final Service service = server.findServices()[0];
    final Engine engine = Engine.class.cast(service.getContainer());
    final Container host = engine.findChild(engine.getDefaultHost());
    if (LifecycleState.STARTED != host.getState()) {
        throw new IllegalStateException("host not started, call LiveReloadInstaller.install() later.");
    }
    // add connector
    final Connector connector = new Connector();
    connector.setPort(port);
    connector.setAttribute("connectionTimeout", "30000");
    service.addConnector(connector);
    // and the endpoint and start the watcher
    final Closeable watch = Instances.get().getWatcher().watch(folder);
    final LiveReloadWebapp liveReloadWebapp = new LiveReloadWebapp(path);
    liveReloadWebapp.addApplicationLifecycleListener(new ServletContextListener() {

        @Override
        public void contextInitialized(final ServletContextEvent servletContextEvent) {
            servletContextEvent.getServletContext().log("Started livereload server on port " + port);
        }

        @Override
        public void contextDestroyed(final ServletContextEvent servletContextEvent) {
            try {
                watch.close();
            } catch (final IOException e) {
            // no-op: not that important, we shutdown anyway
            }
        }
    });
    host.addChild(liveReloadWebapp);
}
Also used : Connector(org.apache.catalina.connector.Connector) Container(org.apache.catalina.Container) Server(org.apache.catalina.Server) ServletContextListener(javax.servlet.ServletContextListener) Closeable(java.io.Closeable) Service(org.apache.catalina.Service) IOException(java.io.IOException) Engine(org.apache.catalina.Engine) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 30 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)

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