Fire events when the ServletContext is created or destroyed
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/c161b112 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/c161b112 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/c161b112 Branch: refs/heads/master Commit: c161b112a43183ef4b1dbd64e130198c36180005 Parents: 8a42460 Author: Christian Kaltepoth <[email protected]> Authored: Wed May 22 17:22:48 2013 +0200 Committer: Christian Kaltepoth <[email protected]> Committed: Thu Jun 13 06:53:29 2013 +0200 ---------------------------------------------------------------------- .../impl/ServletEventBridgeListener.java | 17 ++++- .../context/ServletContextEventsObserver.java | 64 ++++++++++++++++ .../event/context/ServletContextEventsTest.java | 79 ++++++++++++++++++++ 3 files changed, 158 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/c161b112/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java index 6993351..a339c5c 100644 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java @@ -18,6 +18,8 @@ */ package org.apache.deltaspike.servlet.impl; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @@ -30,10 +32,22 @@ import org.apache.deltaspike.servlet.api.literal.WebLiteral; * * @author Christian Kaltepoth */ -public class ServletEventBridgeListener extends EventEmitter implements HttpSessionListener +public class ServletEventBridgeListener extends EventEmitter implements ServletContextListener, HttpSessionListener { @Override + public void contextInitialized(ServletContextEvent sce) + { + fireEvent(sce.getServletContext(), WebLiteral.INSTANCE, InitializedLiteral.INSTANCE); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) + { + fireEvent(sce.getServletContext(), WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE); + } + + @Override public void sessionCreated(HttpSessionEvent se) { fireEvent(se.getSession(), WebLiteral.INSTANCE, InitializedLiteral.INSTANCE); @@ -44,5 +58,4 @@ public class ServletEventBridgeListener extends EventEmitter implements HttpSess { fireEvent(se.getSession(), WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE); } - } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/c161b112/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsObserver.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsObserver.java b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsObserver.java new file mode 100644 index 0000000..9e9d1b4 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsObserver.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.servlet.impl.event.context; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.servlet.ServletContext; + +import org.apache.deltaspike.servlet.api.Destroyed; +import org.apache.deltaspike.servlet.api.Initialized; +import org.apache.deltaspike.servlet.api.Web; + +/** + * Application scoped observer which listens for {@link ServletContext} events on the CDI event bus. + * + * @author Christian Kaltepoth + */ +@ApplicationScoped +public class ServletContextEventsObserver +{ + + private final List<String> eventLog = new ArrayList<String>(); + + public void contextInitialized(@Observes @Web @Initialized ServletContext context) + { + eventLog.add("Initialized ServletContext: " + context.getServletContextName()); + } + + public void contextDestroyed(@Observes @Web @Destroyed ServletContext context) + { + eventLog.add("Destroyed ServletContext: " + context.getServletContextName()); + } + + public int getEventCount() + { + return eventLog.size(); + } + + public List<String> getEventLog() + { + return Collections.unmodifiableList(eventLog); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/c161b112/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsTest.java b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsTest.java new file mode 100644 index 0000000..6a18c1c --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/event/context/ServletContextEventsTest.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.servlet.impl.event.context; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import javax.inject.Inject; + +import org.apache.deltaspike.test.category.WebProfileCategory; +import org.apache.deltaspike.test.servlet.impl.Deployments; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.descriptor.api.Descriptors; +import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +/** + * Test which validates that CDI events are fired when the servlet context is created. + * + * @author Christian Kaltepoth + */ +@RunWith(Arquillian.class) +@Category(WebProfileCategory.class) +public class ServletContextEventsTest +{ + + private final static String MODULE = "servlet-context-events-test"; + + @Deployment + public static WebArchive getDeployment() + { + return ShrinkWrap.create(WebArchive.class, "test.war") + .addAsLibraries(Deployments.getDeltaSpikeCoreArchives()) + .addAsLibraries(Deployments.getDeltaSpikeServletArchives()) + .addAsLibraries(Deployments.getTestSupportArchives()) + .addClass(ServletContextEventsObserver.class) + .addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml") + .addAsWebResource(new StringAsset("foobar"), "foobar.txt") + .setWebXML(new StringAsset( + Descriptors.create(WebAppDescriptor.class) + .displayName(MODULE) + .exportAsString())); + + } + + @Inject + private ServletContextEventsObserver observer; + + @Test + public void shouldReceiveServletContextInitializedEvent() + { + assertEquals(1, observer.getEventCount()); + assertTrue("Didn't receive expected event", + observer.getEventLog().contains("Initialized ServletContext: " + MODULE)); + } + +}
