You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2010/08/13 04:59:50 UTC

svn commit: r985080 - in /myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl: scope/conversation/ util/

Author: gpetracek
Date: Fri Aug 13 02:59:49 2010
New Revision: 985080

URL: http://svn.apache.org/viewvc?rev=985080&view=rev
Log:
EXTCDI-1, EXTCDI-2 and EXTCDI-3 reused window-id refactoring

Modified:
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowContextManager.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowHandler.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/WindowContextIdHolderComponent.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ConversationUtils.java

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowContextManager.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowContextManager.java?rev=985080&r1=985079&r2=985080&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowContextManager.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowContextManager.java Fri Aug 13 02:59:49 2010
@@ -25,9 +25,8 @@ import org.apache.myfaces.extensions.cdi
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ConversationUtils;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.JsfUtils;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.RequestCache;
-import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ExceptionUtils.windowContextNotEditableException;
 import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ConversationUtils.*;
-import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ConversationUtils.resolveWindowContextId;
+import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ExceptionUtils.windowContextNotEditableException;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.scope.conversation.spi.EditableWindowContext;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.scope.conversation.spi.JsfAwareWindowContextConfig;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.scope.conversation.spi.EditableWindowContextManager;
@@ -41,7 +40,6 @@ import javax.faces.context.ExternalConte
 import javax.faces.context.FacesContext;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.concurrent.ConcurrentHashMap;
@@ -72,6 +70,9 @@ public class DefaultWindowContextManager
 
     private WindowContextQuotaHandler windowContextQuotaHandler;
 
+    //TODO add config + refactor DefaultWindowContextManager
+    private static final int DEFAULT_WINDOW_KEY_LENGTH = 3;
+
     protected DefaultWindowContextManager(JsfAwareWindowContextConfig jsfAwareWindowContextConfig,
                                           ProjectStage projectStage)
     {
@@ -118,6 +119,8 @@ public class DefaultWindowContextManager
     {
         String windowContextId = this.windowHandler.createWindowId();
 
+        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
+
         if(this.windowContextQuotaHandler.checkQuota(getNumberOfNextWindowContext()))
         {
             if(!cleanupInactiveWindowContexts(this))
@@ -134,7 +137,10 @@ public class DefaultWindowContextManager
             //however - such a cleanup shouldn't occur during development
             windowContextId = convertToDevWindowContextId(windowContextId, getNumberOfNextWindowContext());
         }
-        cacheWindowId(windowContextId, this.allowUnknownWindowIds);
+
+        storeCreatedWindowContextId(externalContext, windowContextId);
+        cacheWindowId(externalContext, windowContextId, this.allowUnknownWindowIds);
+
         return windowContextId;
     }
 
@@ -260,7 +266,7 @@ public class DefaultWindowContextManager
         removeWindowContextIdHolderComponent(facesContext);
 
         //reset existing information
-        getExistingWindowIdSet(externalContext).remove(windowContext.getId());
+        removeExistingWindowId(externalContext, windowContext.getId());
         externalContext.getRequestMap().remove(WINDOW_CONTEXT_ID_PARAMETER_KEY);
 
         windowContext.endConversations();
@@ -285,16 +291,13 @@ public class DefaultWindowContextManager
 
     private String convertToDevWindowContextId(String windowContextId, int currentWindowContextCount)
     {
-        Set<String> windowContextIdSet =
-                ConversationUtils.getExistingWindowIdSet(FacesContext.getCurrentInstance().getExternalContext());
+        String devWindowContextId = currentWindowContextCount + windowContextId;
 
-        if(windowContextIdSet.remove(windowContextId))
+        if(devWindowContextId.length() > DEFAULT_WINDOW_KEY_LENGTH + ("" + currentWindowContextCount).length())
         {
-            String devWindowContextId = currentWindowContextCount + windowContextId;
-            windowContextIdSet.add(devWindowContextId);
-            return devWindowContextId;
+            return windowContextId;
         }
-        return windowContextId;
+        return devWindowContextId;
     }
 
     public Collection<EditableWindowContext> getWindowContexts()

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowHandler.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowHandler.java?rev=985080&r1=985079&r2=985080&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowHandler.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/DefaultWindowHandler.java Fri Aug 13 02:59:49 2010
@@ -22,8 +22,11 @@ import static org.apache.myfaces.extensi
         .WINDOW_CONTEXT_ID_PARAMETER_KEY;
 
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.scope.conversation.spi.WindowHandler;
-import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ConversationUtils;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.RequestCache;
+import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ConversationUtils
+        .getExistingWindowIdSet;
+import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ConversationUtils
+        .getWindowContextIdHolderComponent;
 
 import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
@@ -88,13 +91,20 @@ public class DefaultWindowHandler implem
     //TODO add a counter in case of project stage dev
     public String createWindowId()
     {
+        String oldWindowContextId = resolveExpiredWindowContextId();
+
+        if(oldWindowContextId != null)
+        {
+            return oldWindowContextId;
+        }
+
         String uuid = UUID.randomUUID().toString().replace("-", "");
 
         ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
 
         synchronized (externalContext.getSessionMap())
         {
-            Set<String> existingWindowIdSet = ConversationUtils.getExistingWindowIdSet(externalContext);
+            Set<String> existingWindowIdSet = getExistingWindowIdSet(externalContext);
 
             String shortUuid;
             int startIndex = 0;
@@ -110,12 +120,26 @@ public class DefaultWindowHandler implem
                 }
                 startIndex++;
             }
-            existingWindowIdSet.add(uuid);
         }
 
         return uuid;
     }
 
+    //to avoid inconsistent behavior in case of re-activated but expired windows in combination with browser refreshes
+    //-> get and recycle old id to avoid a redirect
+    private String resolveExpiredWindowContextId()
+    {
+        WindowContextIdHolderComponent windowContextIdHolderComponent =
+                getWindowContextIdHolderComponent(FacesContext.getCurrentInstance());
+
+        if(windowContextIdHolderComponent == null)
+        {
+            return null;
+        }
+
+        return windowContextIdHolderComponent.getWindowContextId();
+    }
+
     public String restoreWindowId(ExternalContext externalContext)
     {
         if(!this.useWindowAwareUrlEncoding)

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/WindowContextIdHolderComponent.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/WindowContextIdHolderComponent.java?rev=985080&r1=985079&r2=985080&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/WindowContextIdHolderComponent.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/scope/conversation/WindowContextIdHolderComponent.java Fri Aug 13 02:59:49 2010
@@ -61,6 +61,7 @@ public class WindowContextIdHolderCompon
 
         this.windowContextId = (String) values[1];
 
+        //TODO test if we can remove the request-map based cache (there is a new approach)
         facesContext.getExternalContext().getRequestMap()
                 .put(WindowContextManager.WINDOW_CONTEXT_ID_PARAMETER_KEY, this.windowContextId);
     }

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ConversationUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ConversationUtils.java?rev=985080&r1=985079&r2=985080&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ConversationUtils.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ConversationUtils.java Fri Aug 13 02:59:49 2010
@@ -47,6 +47,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.Collection;
+import java.util.Collections;
 
 /**
  * internal! utils
@@ -200,14 +201,22 @@ public class ConversationUtils
 
         if (windowContextIdHolder != null)
         {
-            requestMap.put(
-                    WindowContextManager.WINDOW_CONTEXT_ID_PARAMETER_KEY, windowContextIdHolder.getWindowContextId());
-
             //TODO cache for request
-            return windowContextIdHolder.getWindowContextId();
+            id = windowContextIdHolder.getWindowContextId();
+
+            if(id != null && !cacheWindowId(externalContext, id, allowUnknownWindowIds))
+            {
+                id = null;
+            }
+
+            if(id != null)
+            {
+                requestMap.put(WindowContextManager.WINDOW_CONTEXT_ID_PARAMETER_KEY,
+                               windowContextIdHolder.getWindowContextId());
+            }
         }
 
-        return null;
+        return id;
     }
 
     private static String tryToRestoreWindowIdFromRequestParameterMap(
@@ -224,11 +233,6 @@ public class ConversationUtils
         return idViaGetRequest;
     }
 
-    public static void cacheWindowId(String id, boolean allowUnknownWindowIds)
-    {
-        cacheWindowId(FacesContext.getCurrentInstance().getExternalContext(), id, allowUnknownWindowIds);
-    }
-
     /**
      * @param externalContext externalContext
      * @param id windowId
@@ -376,8 +380,24 @@ public class ConversationUtils
         return RequestCache.getWindowContextManager();
     }
 
+    public static boolean removeExistingWindowId(ExternalContext externalContext, String windowContextId)
+    {
+        return getEditableWindowIdSet(externalContext).remove(windowContextId);
+    }
+
     public static Set<String> getExistingWindowIdSet(ExternalContext externalContext)
     {
+        Set<String> existingWindowIdSet = getEditableWindowIdSet(externalContext);
+        return Collections.unmodifiableSet(existingWindowIdSet);
+    }
+
+    public static void storeCreatedWindowContextId(ExternalContext externalContext, String windowContextId)
+    {
+        getEditableWindowIdSet(externalContext).add(windowContextId);
+    }
+
+    private static Set<String> getEditableWindowIdSet(ExternalContext externalContext)
+    {
         Map<String, Object> sessionMap = externalContext.getSessionMap();
 
         @SuppressWarnings({"unchecked"})