You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2011/08/09 16:04:49 UTC

svn commit: r1155372 - /openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java

Author: struberg
Date: Tue Aug  9 14:04:49 2011
New Revision: 1155372

URL: http://svn.apache.org/viewvc?rev=1155372&view=rev
Log:
OWB-601 support for activateContexts for @ApplicationScoped

we now set a shared ApplicationContext for all non-ServletContext
related threads.

Modified:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java

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=1155372&r1=1155371&r2=1155372&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 Tue Aug  9 14:04:49 2011
@@ -67,6 +67,11 @@ public class WebContextsService extends 
     /**Current application context*/
     private static ThreadLocal<ApplicationContext> applicationContext = null;
 
+    /**
+     * This applicationContext will be used for all non servlet-request threads
+     */
+    private ApplicationContext sharedApplicationContext ;
+
     /**Current conversation context*/
     private static ThreadLocal<ConversationContext> conversationContext = null;
     
@@ -127,6 +132,9 @@ public class WebContextsService extends 
         supportsConversation =  webBeansContext.getOpenWebBeansConfiguration().supportsConversation();
         failoverService = webBeansContext.getService(FailOverService.class);
         conversationManager = webBeansContext.getConversationManager();
+
+        sharedApplicationContext = new ApplicationContext();
+        sharedApplicationContext.setActive(true);
     }
 
     public SessionContextManager getSessionContextManager()
@@ -155,6 +163,9 @@ public class WebContextsService extends 
     {
         //Destroy application context
         endContext(ApplicationScoped.class, destroyObject);
+
+        // we also need to destroy the shared ApplicationContext
+        sharedApplicationContext.destroy();
         
         //Destroy singleton context
         endContext(Singleton.class, destroyObject);
@@ -446,23 +457,27 @@ public class WebContextsService extends 
     private void initApplicationContext(ServletContext servletContext)
     {
         
-        if(servletContext != null && currentApplicationContexts.containsKey(servletContext))
-        {
-            applicationContext.set(currentApplicationContexts.get(servletContext));
-        }
-        
-        else
+        if(servletContext != null)
         {
-            ApplicationContext currentApplicationContext = new ApplicationContext();         
-            currentApplicationContext.setActive(true);
-            
-            if(servletContext != null)
+            if (currentApplicationContexts.containsKey(servletContext))
+            {
+                applicationContext.set(currentApplicationContexts.get(servletContext));
+            }
+            else
             {
+                ApplicationContext currentApplicationContext = new ApplicationContext();
+                currentApplicationContext.setActive(true);
                 currentApplicationContexts.put(servletContext, currentApplicationContext);
-                
+
+                applicationContext.set(currentApplicationContext);
             }
-            
-            applicationContext.set(currentApplicationContext);
+        }
+        else
+        {
+            // if we are in a thread which is not related to a Servlet request,
+            // then we use a shared ApplicationContext.
+            // this happens for asynchronous jobs and JMS invocations, etc.
+            applicationContext.set(sharedApplicationContext);
         }
     }
 
@@ -629,7 +644,6 @@ public class WebContextsService extends 
      */
     private  SessionContext getSessionContext()
     {
-
         SessionContext context = sessionContext.get();
         if (null == context)
         {
@@ -721,4 +735,29 @@ public class WebContextsService extends 
     }
 
 
+    /**
+     * This might be needed when you aim to start a new thread in a WebApp.
+     * @param scopeType
+     */
+    @Override
+    public void activateContext(Class<? extends Annotation> scopeType)
+    {
+        if (scopeType.equals(ApplicationScoped.class))
+        {
+            if (applicationContext.get() == null)
+            {
+                applicationContext.set(sharedApplicationContext);
+            }
+        }
+        else
+        {
+            super.activateContext(scopeType);
+        }
+    }
+
+    @Override
+    public void deActivateContext(Class<? extends Annotation> scopeType)
+    {
+        super.deActivateContext(scopeType);
+    }
 }