Search in sources :

Example 1 with LifecycleEvent

use of org.apache.catalina.LifecycleEvent in project tomcat70 by apache.

the class TestStandardContextResources method testResourcesAbsoluteOrdering.

@Test
public void testResourcesAbsoluteOrdering() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0-fragments");
    // app dir is relative to server home
    StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    LifecycleListener[] listener = ctx.findLifecycleListeners();
    Assert.assertEquals(3, listener.length);
    Assert.assertTrue(listener[1] instanceof ContextConfig);
    ContextConfig config = new ContextConfig() {

        @Override
        protected WebXml createWebXml() {
            WebXml wxml = new WebXml();
            wxml.addAbsoluteOrdering("resources");
            wxml.addAbsoluteOrdering("resources2");
            return wxml;
        }
    };
    // prevent it from looking ( if it finds one - it'll have dup error )
    config.setDefaultWebXml(Constants.NoDefaultWebXml);
    listener[1] = config;
    Tomcat.addServlet(ctx, "getresource", new GetResourceServlet());
    ctx.addServletMapping("/getresource", "getresource");
    tomcat.start();
    assertPageContains("/test/getresource?path=/resourceF.jsp", "<p>resourceF.jsp in resources2.jar</p>");
    assertPageContains("/test/getresource?path=/resourceB.jsp", "<p>resourceB.jsp in resources.jar</p>");
    // Check ordering, for BZ 54391
    Assert.assertEquals(Arrays.asList("resources.jar", "resources2.jar"), ctx.getServletContext().getAttribute(ServletContext.ORDERED_LIBS));
    ctx.stop();
    LifecycleListener[] listener1 = ctx.findLifecycleListeners();
    // change ordering and reload
    ContextConfig config1 = new ContextConfig() {

        @Override
        protected WebXml createWebXml() {
            WebXml wxml = new WebXml();
            wxml.addAbsoluteOrdering("resources2");
            wxml.addAbsoluteOrdering("resources");
            return wxml;
        }
    };
    // prevent it from looking ( if it finds one - it'll have dup error )
    config1.setDefaultWebXml(Constants.NoDefaultWebXml);
    listener1[1] = config1;
    // Need to init since context won't call init
    config1.lifecycleEvent(new LifecycleEvent(ctx, Lifecycle.AFTER_INIT_EVENT, null));
    Tomcat.addServlet(ctx, "getresource", new GetResourceServlet());
    ctx.addServletMapping("/getresource", "getresource");
    ctx.start();
    assertPageContains("/test/getresource?path=/resourceF.jsp", "<p>resourceF.jsp in resources2.jar</p>");
    assertPageContains("/test/getresource?path=/resourceB.jsp", "<p>resourceB.jsp in resources2.jar</p>");
    // Check ordering, for BZ 54391
    Assert.assertEquals(Arrays.asList("resources2.jar", "resources.jar"), ctx.getServletContext().getAttribute(ServletContext.ORDERED_LIBS));
}
Also used : ContextConfig(org.apache.catalina.startup.ContextConfig) WebXml(org.apache.catalina.deploy.WebXml) Tomcat(org.apache.catalina.startup.Tomcat) LifecycleEvent(org.apache.catalina.LifecycleEvent) LifecycleListener(org.apache.catalina.LifecycleListener) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 2 with LifecycleEvent

use of org.apache.catalina.LifecycleEvent in project tomcat70 by apache.

the class LifecycleSupport method fireLifecycleEvent.

/**
 * Notify all lifecycle event listeners that a particular event has
 * occurred for this Container.  The default implementation performs
 * this notification synchronously using the calling thread.
 *
 * @param type Event type
 * @param data Event data
 */
public void fireLifecycleEvent(String type, Object data) {
    LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
    LifecycleListener[] interested = listeners;
    for (int i = 0; i < interested.length; i++) interested[i].lifecycleEvent(event);
}
Also used : LifecycleEvent(org.apache.catalina.LifecycleEvent) LifecycleListener(org.apache.catalina.LifecycleListener)

Example 3 with LifecycleEvent

use of org.apache.catalina.LifecycleEvent in project spring-boot by spring-projects.

the class TomcatReactiveWebServerFactoryTests method tomcatListeners.

@Test
void tomcatListeners() {
    TomcatReactiveWebServerFactory factory = getFactory();
    LifecycleListener[] listeners = new LifecycleListener[4];
    Arrays.setAll(listeners, (i) -> mock(LifecycleListener.class));
    factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1]));
    factory.addContextLifecycleListeners(listeners[2], listeners[3]);
    this.webServer = factory.getWebServer(mock(HttpHandler.class));
    InOrder ordered = inOrder((Object[]) listeners);
    for (LifecycleListener listener : listeners) {
        then(listener).should(ordered).lifecycleEvent(any(LifecycleEvent.class));
    }
}
Also used : InOrder(org.mockito.InOrder) LifecycleEvent(org.apache.catalina.LifecycleEvent) LifecycleListener(org.apache.catalina.LifecycleListener) AprLifecycleListener(org.apache.catalina.core.AprLifecycleListener) Test(org.junit.jupiter.api.Test)

Example 4 with LifecycleEvent

use of org.apache.catalina.LifecycleEvent in project spring-boot by spring-projects.

the class TomcatServletWebServerFactoryTests method tomcatListeners.

@Test
void tomcatListeners() {
    TomcatServletWebServerFactory factory = getFactory();
    LifecycleListener[] listeners = new LifecycleListener[4];
    Arrays.setAll(listeners, (i) -> mock(LifecycleListener.class));
    factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1]));
    factory.addContextLifecycleListeners(listeners[2], listeners[3]);
    this.webServer = factory.getWebServer();
    InOrder ordered = inOrder((Object[]) listeners);
    for (LifecycleListener listener : listeners) {
        then(listener).should(ordered).lifecycleEvent(any(LifecycleEvent.class));
    }
}
Also used : InOrder(org.mockito.InOrder) LifecycleEvent(org.apache.catalina.LifecycleEvent) LifecycleListener(org.apache.catalina.LifecycleListener) AprLifecycleListener(org.apache.catalina.core.AprLifecycleListener) Test(org.junit.jupiter.api.Test)

Example 5 with LifecycleEvent

use of org.apache.catalina.LifecycleEvent in project tomee by apache.

the class TomEEUndeployTest method justAContextStop.

@Test
public void justAContextStop() throws Exception {
    container.start();
    assertEquals(0, webapps().length);
    final StandardHost standardHost = StandardHost.class.cast(TomcatHelper.getServer().findService("Tomcat").getContainer().findChild("localhost"));
    // not done in embedded but that's the way autodeploy works in normal tomcat
    final HostConfig listener = new HostConfig();
    standardHost.addLifecycleListener(listener);
    createWebapp(new File(WORK_DIR, "tomee/webapps/my-webapp"));
    listener.lifecycleEvent(new LifecycleEvent(standardHost, Lifecycle.START_EVENT, standardHost));
    assertEquals(1, webapps().length);
    webapps()[0].stop();
    assertEquals(1, webapps().length);
    webapps()[0].start();
    assertEquals(1, webapps().length);
    assertEquals("test", IO.slurp(new URL("http://localhost:" + http + "/my-webapp/")));
}
Also used : StandardHost(org.apache.catalina.core.StandardHost) HostConfig(org.apache.catalina.startup.HostConfig) LifecycleEvent(org.apache.catalina.LifecycleEvent) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Aggregations

LifecycleEvent (org.apache.catalina.LifecycleEvent)8 LifecycleListener (org.apache.catalina.LifecycleListener)5 Test (org.junit.Test)4 File (java.io.File)3 AprLifecycleListener (org.apache.catalina.core.AprLifecycleListener)2 StandardHost (org.apache.catalina.core.StandardHost)2 HostConfig (org.apache.catalina.startup.HostConfig)2 Test (org.junit.jupiter.api.Test)2 InOrder (org.mockito.InOrder)2 URL (java.net.URL)1 Lifecycle (org.apache.catalina.Lifecycle)1 WebXml (org.apache.catalina.deploy.WebXml)1 ContextConfig (org.apache.catalina.startup.ContextConfig)1 Tomcat (org.apache.catalina.startup.Tomcat)1 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)1 Logger (org.sonar.api.utils.log.Logger)1