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 2010/09/24 01:09:42 UTC

svn commit: r1000661 - in /openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans: servlet/WebBeansConfigurationListener.java web/context/ServletRequestContext.java web/context/WebContextsService.java

Author: struberg
Date: Thu Sep 23 23:09:42 2010
New Revision: 1000661

URL: http://svn.apache.org/viewvc?rev=1000661&view=rev
Log:
OWB-457 lazy-initialise the SessionContext 

txs to Rohit Kelapur for his original patch!
tweaked + reformatted + fixed by me

Added:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/ServletRequestContext.java
Modified:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
    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/servlet/WebBeansConfigurationListener.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java?rev=1000661&r1=1000660&r2=1000661&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java (original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java Thu Sep 23 23:09:42 2010
@@ -144,34 +144,9 @@ public class WebBeansConfigurationListen
             }
             
             this.lifeCycle.getContextService().startContext(RequestScoped.class, event);
-            
-            //Session Conext Must be Active
-            Object request = event.getServletRequest();
-            if(request instanceof HttpServletRequest)
-            {
-                HttpServletRequest httpRequest = (HttpServletRequest)request;
-                HttpSession currentSession = httpRequest.getSession(false);
-                if(currentSession == null)
-                {
-                    //To activate session context
-                    try 
-                    {
-                        httpRequest.getSession();
-                    }
-                    catch(Exception e)
-                    {
-                        logger.error(OWBLogConst.ERROR_0013, e);
-                    }
-                } 
-                else 
-                {
-                    if (failoverService != null && 
-                            failoverService.isSupportFailOver()) 
-                    {
-                        failoverService.sessionIsInUse(currentSession);
-                    }
-                }                
-            }
+
+            // we don't initialise the Session here but do it lazily if it gets requested
+            // the first time. See OWB-457            
 
         }
         catch (Exception e)

Added: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/ServletRequestContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/ServletRequestContext.java?rev=1000661&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/ServletRequestContext.java (added)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/ServletRequestContext.java Thu Sep 23 23:09:42 2010
@@ -0,0 +1,66 @@
+/*
+ * 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.web.context;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.webbeans.context.RequestContext;
+
+/**
+ * RequestContext which additionally holds the current servletRequest
+ * for being able to lazily initialise the SessionContext if needed.
+ */
+public class ServletRequestContext extends RequestContext
+{
+
+    private static final long serialVersionUID = -8375349845543590243L;
+
+    // this can only be accessed when the context is active
+    private transient HttpServletRequest servletRequest;
+
+
+    public ServletRequestContext()
+    {
+        super();
+    }
+
+    public HttpServletRequest getServletRequest()
+    {
+        if (active)
+        {
+            return servletRequest;
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+    public void setServletRequest(HttpServletRequest servletRequest)
+    {
+        this.servletRequest = servletRequest;
+    }
+
+    public void destroy()
+    {
+        super.destroy();
+        servletRequest = null;
+    }
+
+}

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=1000661&r1=1000660&r2=1000661&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 Thu Sep 23 23:09:42 2010
@@ -35,6 +35,7 @@ import javax.servlet.ServletRequestEvent
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
+import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.OpenWebBeansConfiguration;
 import org.apache.webbeans.context.AbstractContextsService;
 import org.apache.webbeans.context.ApplicationContext;
@@ -44,7 +45,10 @@ import org.apache.webbeans.context.Reque
 import org.apache.webbeans.context.SessionContext;
 import org.apache.webbeans.context.SingletonContext;
 import org.apache.webbeans.conversation.ConversationManager;
+import org.apache.webbeans.corespi.ServiceLoader;
 import org.apache.webbeans.el.ELContextStore;
+import org.apache.webbeans.logger.WebBeansLogger;
+import org.apache.webbeans.spi.FailOverService;
 
 /**
  * Web container {@link org.apache.webbeans.spi.ContextsService}
@@ -52,6 +56,9 @@ import org.apache.webbeans.el.ELContextS
  */
 public class WebContextsService extends AbstractContextsService
 {
+    /**Logger instance*/
+    private static final WebBeansLogger logger = WebBeansLogger.getLogger(WebContextsService.class);
+
     /**Current request context*/
     private static ThreadLocal<RequestContext> requestContext = null;
 
@@ -83,6 +90,8 @@ public class WebContextsService extends 
     private final ConversationManager conversationManager = ConversationManager.getInstance();
     
     private boolean supportsConversation = false;
+    
+    protected FailOverService failoverService = null;
 
     /**Initialize thread locals*/
     static
@@ -105,6 +114,8 @@ public class WebContextsService extends 
     public WebContextsService()
     {
         supportsConversation =  OpenWebBeansConfiguration.getInstance().supportsConversation();
+        failoverService = (FailOverService) ServiceLoader.getService(FailOverService.class);
+
     }
     
     /**
@@ -280,13 +291,14 @@ public class WebContextsService extends 
     private void initRequestContext(ServletRequestEvent event)
     {
         
-        RequestContext rq = new RequestContext();
+        RequestContext rq = new ServletRequestContext();
         rq.setActive(true);
 
         requestContext.set(rq);// set thread local
         if(event != null)
         {
             HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
+            ((ServletRequestContext)rq).setServletRequest(request);
             
             if (request != null)
             {
@@ -585,7 +597,15 @@ public class WebContextsService extends 
      */
     private  SessionContext getSessionContext()
     {
-        return sessionContext.get();
+
+        SessionContext context = sessionContext.get();
+        if (null == context)
+        {
+            lazyStartSessionContext();
+            context = sessionContext.get();
+        }
+
+        return context;
     }
 
     /**
@@ -614,4 +634,48 @@ public class WebContextsService extends 
     {
         return conversationContext.get();
     }
+
+    private Context lazyStartSessionContext()
+    {
+
+        logger.debug(">lazyStartSessionContext");
+
+        Context webContext = null;
+        Context context = getCurrentContext(RequestScoped.class);
+        if (context instanceof ServletRequestContext)
+        {
+            ServletRequestContext requestContext = (ServletRequestContext) context;
+            HttpServletRequest servletRequest = requestContext.getServletRequest();
+            if (null != servletRequest)
+            { // this could be null if there is no active request context
+                try
+                {
+                    HttpSession currentSession = servletRequest.getSession();
+                    initSessionContext(currentSession);
+                    if (failoverService != null && failoverService.isSupportFailOver())
+                    {
+                        failoverService.sessionIsInUse(currentSession);
+                    }
+
+                    logger.debug("Lazy SESSION context initialization SUCCESS");
+                }
+                catch (Exception e)
+                {
+                    logger.error(OWBLogConst.ERROR_0013, e);
+                }
+
+            }
+            else
+            {
+                logger.warn("Could NOT lazily initialize session context because NO active request context");
+            }
+        }
+        else
+        {
+            logger.warn("Could NOT lazily initialize session context because of "+context+" RequestContext");
+        }
+
+        logger.debug("<lazyStartSessionContext "+ webContext);
+        return webContext;
+    }
 }
\ No newline at end of file