openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1673005 - in /openwebbeans/trunk: samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/ webbeans-web/src/main/java/org/apache/webbeans/web/context/ webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/
Date Sun, 12 Apr 2015 12:12:30 GMT
Author: struberg
Date: Sun Apr 12 12:12:30 2015
New Revision: 1673005

URL: http://svn.apache.org/r1673005
Log:
OWB-1025 implement @Initialized and @Destroyed for native tomcat and other servlet webapps


Added:
    openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/SampleContextObserver.java
Modified:
    openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/CurrentDateProvider.java
    openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/MyFilter.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java

Modified: openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/CurrentDateProvider.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/CurrentDateProvider.java?rev=1673005&r1=1673004&r2=1673005&view=diff
==============================================================================
--- openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/CurrentDateProvider.java
(original)
+++ openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/CurrentDateProvider.java
Sun Apr 12 12:12:30 2015
@@ -18,13 +18,16 @@
  */
 package org.apache.webbeans.samples.tomcat;
 
+import java.io.Serializable;
 import java.security.Principal;
 import java.util.Date;
 
+import javax.enterprise.context.SessionScoped;
 import javax.enterprise.inject.Produces;
 import javax.inject.Inject;
 
-public class CurrentDateProvider
+@SessionScoped // just to test @Initialized(SessionScoped.class)
+public class CurrentDateProvider implements Serializable
 {
     private @Inject Principal principal;
     

Modified: openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/MyFilter.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/MyFilter.java?rev=1673005&r1=1673004&r2=1673005&view=diff
==============================================================================
--- openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/MyFilter.java
(original)
+++ openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/MyFilter.java
Sun Apr 12 12:12:30 2015
@@ -48,7 +48,7 @@ public class MyFilter implements Filter
 
     @Override
     @SuppressWarnings("unchecked")
-    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws
IOException, ServletException
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException
     {
         Set<Bean<?>> beans = manager.getBeans(CurrentDateProvider.class);
         log.info("Total found beans : " + beans.size());
@@ -57,7 +57,7 @@ public class MyFilter implements Filter
         
         log.info("Current time is : " + instance.getCurrentDate());
         
-        arg2.doFilter(arg0, arg1);
+        filterChain.doFilter(request, response);
         
     }
 

Added: openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/SampleContextObserver.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/SampleContextObserver.java?rev=1673005&view=auto
==============================================================================
--- openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/SampleContextObserver.java
(added)
+++ openwebbeans/trunk/samples/tomcat7-sample/src/main/java/org/apache/webbeans/samples/tomcat/SampleContextObserver.java
Sun Apr 12 12:12:30 2015
@@ -0,0 +1,69 @@
+/*
+ * 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.webbeans.samples.tomcat;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Destroyed;
+import javax.enterprise.context.Initialized;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Observes;
+import java.util.logging.Logger;
+
+/**
+ * A sample bean which observes the new CDI-1.1 &#064;Initialized and &#064;Destroyed
events
+ */
+@ApplicationScoped
+public class SampleContextObserver
+{
+    private static final Logger log = Logger.getLogger(SampleContextObserver.class.getName());
+
+    public void onAppInit(@Observes @Initialized(ApplicationScoped.class) Object payload)
+    {
+        log.info("ApplicationContext got started. Init Object: " + payload.toString());
+    }
+
+    public void onAppDestroy(@Observes @Destroyed(ApplicationScoped.class) Object payload)
+    {
+        // this is unlikely to ever fire - we just did destroy our applicationscoped bean
;)
+        log.info("ApplicationContext got destroyed. Init Object: " + payload.toString());
+    }
+    
+    public void onSessionInit(@Observes @Initialized(SessionScoped.class) Object payload)
+    {
+        log.info("SessionContext got started. Init Object: " + payload.toString());
+    }
+
+    public void onSessionDestroy(@Observes @Destroyed(SessionScoped.class) Object payload)
+    {
+        log.info("SessionContext got destroyed. Init Object: " + payload.toString());
+    }
+    
+    public void onRequestInit(@Observes @Initialized(RequestScoped.class) Object payload)
+    {
+        log.info("RequestContext got started. Init Object: " + payload.toString());
+    }
+
+    public void onRequestDestroy(@Observes @Destroyed(RequestScoped.class) Object payload)
+    {
+        log.info("RequestContext got destroyed. Init Object: " + payload.toString());
+    }
+    
+    
+}

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java?rev=1673005&r1=1673004&r2=1673005&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
Sun Apr 12 12:12:30 2015
@@ -497,34 +497,36 @@ public class WebContextsService extends
     /**
      * Destroys the session context and all of its components at the end of the
      * session. 
-     * @param session http session object
+     * @param session http session object. Can be {@code null} for non-http SessionContexts.
Such a context only lives for one thread.
      */
     private void destroySessionContext(HttpSession session)
     {
+        //Get current session context from ThreadLocal
+        SessionContext context = sessionContexts.get();
+
         if (session != null)
         {
-            //Get current session context
-            SessionContext context = sessionContexts.get();
-
             if (context == null)
             {
                 initSessionContext(session);
                 context = sessionContexts.get();
             }
 
-            //Destroy context
-            if (context != null)
-            {
-                context.destroy();
-            }
-
-            //Clear thread locals
-            sessionContexts.set(null);
-            sessionContexts.remove();
-
             //Remove session from manager
             sessionCtxManager.removeSessionContextWithSessionId(session.getId());
         }
+
+        //Destroy context
+        if (context != null)
+        {
+            context.destroy();
+            webBeansContext.getBeanManagerImpl().fireEvent(session, DestroyedLiteral.INSTANCE_SESSION_SCOPED);
+        }
+
+        //Clear thread locals
+        sessionContexts.set(null);
+        sessionContexts.remove();
+
     }
 
     /**

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java?rev=1673005&r1=1673004&r2=1673005&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
Sun Apr 12 12:12:30 2015
@@ -18,6 +18,7 @@
  */
 package org.apache.webbeans.web.lifecycle;
 
+import org.apache.webbeans.annotation.InitializedLiteral;
 import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.OpenWebBeansConfiguration;
 import org.apache.webbeans.config.WebBeansContext;
@@ -144,6 +145,7 @@ public final class WebContainerLifecycle
         ServletContext servletContext = (ServletContext)(startupObject);
         servletContext.setAttribute(BeanManager.class.getName(), getBeanManager());
 
+        webBeansContext.getBeanManagerImpl().fireEvent(servletContext != null ? servletContext
: new Object(), InitializedLiteral.INSTANCE_APPLICATION_SCOPED);
     }
 
     @Override



Mime
View raw message