openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1677358 - in /openwebbeans/trunk/webbeans-web/src: main/java/org/apache/webbeans/web/context/ test/resources/META-INF/openwebbeans/
Date Sat, 02 May 2015 22:22:27 GMT
Author: struberg
Date: Sat May  2 22:22:27 2015
New Revision: 1677358

URL: http://svn.apache.org/r1677358
Log:
OWB-1050 also support conversations in servlets

Added:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebConversationService.java
      - copied, changed from r1676851, openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/conversation/DefaultConversationService.java
Modified:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
    openwebbeans/trunk/webbeans-web/src/test/resources/META-INF/openwebbeans/openwebbeans.properties

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=1677358&r1=1677357&r2=1677358&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
Sat May  2 22:22:27 2015
@@ -61,7 +61,7 @@ public class WebContextsService extends
     /**Logger instance*/
     private static final Logger logger = WebBeansLoggerFacade.getLogger(WebContextsService.class);
 
-    private static final String OWB_SESSION_CONTEXT_ATTRIBUTE = "OPENWEBBEANS_SESSION_CONTEXT";
+    private static final String OWB_SESSION_CONTEXT_ATTRIBUTE_NAME = "openWebBeansSessionContext";
 
     /**Current request context*/
     protected static ThreadLocal<RequestContext> requestContexts = null;
@@ -416,20 +416,20 @@ public class WebContextsService extends
         else
         {
             // we need to get it latest here to make sure we work on the same instance
-            currentSessionContext = (SessionContext) session.getAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE);
+            currentSessionContext = (SessionContext) session.getAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE_NAME);
 
             if (currentSessionContext == null)
             {
                 // no current context, so lets create a new one
-                synchronized (OWB_SESSION_CONTEXT_ATTRIBUTE)
+                synchronized (OWB_SESSION_CONTEXT_ATTRIBUTE_NAME)
                 {
-                    currentSessionContext = (SessionContext) session.getAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE);
+                    currentSessionContext = (SessionContext) session.getAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE_NAME);
                     if (currentSessionContext == null)
                     {
                         currentSessionContext = new SessionContext();
                         currentSessionContext.setActive(true);
                         webBeansContext.getBeanManagerImpl().fireEvent(session, InitializedLiteral.INSTANCE_SESSION_SCOPED);
-                        session.setAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE, currentSessionContext);
+                        session.setAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE_NAME, currentSessionContext);
                     }
                 }
             }
@@ -437,7 +437,7 @@ public class WebContextsService extends
             {
                 // we do that in any case.
                 // This is needed to trigger delta-replication on most servers
-                session.setAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE, currentSessionContext);
+                session.setAttribute(OWB_SESSION_CONTEXT_ATTRIBUTE_NAME, currentSessionContext);
             }
         }
 

Copied: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebConversationService.java
(from r1676851, openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/conversation/DefaultConversationService.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebConversationService.java?p2=openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebConversationService.java&p1=openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/conversation/DefaultConversationService.java&r1=1676851&r2=1677358&rev=1677358&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/conversation/DefaultConversationService.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebConversationService.java
Sat May  2 22:22:27 2015
@@ -16,32 +16,123 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.webbeans.conversation;
+package org.apache.webbeans.web.context;
 
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.context.RequestContext;
+import org.apache.webbeans.spi.ContextsService;
 import org.apache.webbeans.spi.ConversationService;
 
 /**
- * We do not support Conversation propagation in JavaSE yet.
+ * Conversation propagation in pure Servlets happens via cid servlet parameter.
  */
-public class DefaultConversationService implements ConversationService
+public class WebConversationService implements ConversationService
 {
+    public static final String REQUEST_PARAM_CONVERSATION_ID = "cid";
+    public static final String REQUEST_PARAM_SUPPRESS_CONVERSATION_PROPAGATION = "conversationPropagation";
+    public static final String SESSION_CONVERSATION_ID_PARAM_NAME = "openWebBeansConversationIdCounter";
+
+    private final WebBeansContext webBeansContext;
+
     /**
-     * For SE we only need one single counter as there is no
-     * serialisation anyway.
+     * For non-servlet requests we simply generate the numbers internally
      */
-    private AtomicInteger conversationIdCounter = new AtomicInteger(0);
+    private AtomicInteger nonServletRequestConversationIdCounter = new AtomicInteger(0);
 
+
+    public WebConversationService(WebBeansContext webBeansContext)
+    {
+        this.webBeansContext = webBeansContext;
+    }
+
+    /**
+     * Try to get the current conversationId from the servlet.
+     */
     @Override
     public String getConversationId()
     {
+        ServletRequest servletRequest = getCurrentServletRequest();
+        if (servletRequest != null)
+        {
+            // get the cid parameter from the servlet request
+            String cidParamValue = servletRequest.getParameter(REQUEST_PARAM_CONVERSATION_ID);
+            if (cidParamValue != null && cidParamValue.length() > 0)
+            {
+                // also check the suppress conversation propagation parameter
+                String suppressConversationPropagation = servletRequest.getParameter(REQUEST_PARAM_SUPPRESS_CONVERSATION_PROPAGATION);
+                if (!"none".equals(suppressConversationPropagation))
+                {
+                    // wohu, we found our cid parameter
+                    return cidParamValue;
+                }
+            }
+        }
+
+        // seems we cannot find any cid parameter
         return null;
     }
 
     @Override
     public String generateConversationId()
     {
-        return Long.toString(conversationIdCounter.incrementAndGet());
+        return Long.toString(getConversationIdCounter().incrementAndGet());
+    }
+
+    protected AtomicInteger getConversationIdCounter()
+    {
+        AtomicInteger counter = getSessionConversationIdCounter();
+        return counter != null ? counter : nonServletRequestConversationIdCounter;
+    }
+
+    /**
+     * @return conversationId counter AtomicInteger if this is a real http request, {@code
null} otherwise
+     */
+    protected AtomicInteger getSessionConversationIdCounter()
+    {
+        ServletRequest servletRequest = getCurrentServletRequest();
+        if (servletRequest instanceof HttpServletRequest)
+        {
+            HttpSession session = ((HttpServletRequest) servletRequest).getSession(true);
+            AtomicInteger sessionCounter = (AtomicInteger) session.getAttribute(SESSION_CONVERSATION_ID_PARAM_NAME);
+            if (sessionCounter == null)
+            {
+                synchronized (session)
+                {
+                    sessionCounter = (AtomicInteger) session.getAttribute(SESSION_CONVERSATION_ID_PARAM_NAME);
+                    if (sessionCounter == null)
+                    {
+                        sessionCounter = new AtomicInteger(0);
+                        session.setAttribute(SESSION_CONVERSATION_ID_PARAM_NAME, sessionCounter);
+                    }
+                }
+            }
+            return sessionCounter;
+        }
+
+
+        return null;
+    }
+
+    /**
+     * @return the current ServletRequest or {@code null} if this thread is not attached
to a servlet request
+     */
+    protected ServletRequest getCurrentServletRequest()
+    {
+        ContextsService contextsService = webBeansContext.getContextsService();
+        if (contextsService instanceof WebContextsService)
+        {
+            RequestContext requestContext = ((WebContextsService) contextsService).getRequestContext(false);
+            if (requestContext instanceof ServletRequestContext)
+            {
+                return ((ServletRequestContext) requestContext).getServletRequest();
+            }
+        }
+
+        return null;
     }
 }

Modified: openwebbeans/trunk/webbeans-web/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/test/resources/META-INF/openwebbeans/openwebbeans.properties?rev=1677358&r1=1677357&r2=1677358&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
(original)
+++ openwebbeans/trunk/webbeans-web/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
Sat May  2 22:22:27 2015
@@ -24,6 +24,14 @@
 
 configuration.ordinal=15
 
+################################### Default Conversation Service ###############################
+# Servlet backed implementation of org.apache.webbeans.corespi.ConversationService.
+# This looks in the servlet request if there is a cid parameter.
+# As per spec the conversation propagation can be suppressed with a conversationPropagation=none
parameter
+org.apache.webbeans.spi.ConversationService=org.apache.webbeans.web.context.WebConversationService
+################################################################################################
+
+
 ########################### Proxy Implmenentation Mapping ######################################
 # This allows mapping a Scope Annotation class to a specific InterceptorProxy which are
 # typically sub classes of NormalScopedBeanInterceptorHandler



Mime
View raw message