Author: niallp
Date: Thu Jun  8 09:19:14 2006
New Revision: 412789

URL: http://svn.apache.org/viewvc?rev=412789&view=rev
Log:
Fix for CHAIN-30 - ServletSessionScopeMap always forces a Session to be Created

Modified:
    
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletSessionScopeMap.java
    
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
    
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletSessionScopeMap.java
    
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
    
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
    
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java

Modified: 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletSessionScopeMap.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletSessionScopeMap.java?rev=412789&r1=412788&r2=412789&view=diff
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletSessionScopeMap.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletSessionScopeMap.java
 Thu Jun  8 09:19:14 2006
@@ -25,6 +25,7 @@
 import java.util.Map;
 import java.util.Set;
 import javax.portlet.PortletSession;
+import javax.portlet.PortletRequest;
 import org.apache.commons.chain.web.MapEntry;
 
 
@@ -39,29 +40,37 @@
 final class PortletSessionScopeMap implements Map {
 
 
-    public PortletSessionScopeMap(PortletSession session) {
-        this.session = session;
+    public PortletSessionScopeMap(PortletRequest request) {
+        this.request = request;
+        sessionExists();
     }
 
 
     private PortletSession session = null;
+    private PortletRequest request = null;
 
 
     public void clear() {
-        Iterator keys = keySet().iterator();
-        while (keys.hasNext()) {
-            session.removeAttribute((String) keys.next());
+        if (sessionExists()) {
+            Iterator keys = keySet().iterator();
+            while (keys.hasNext()) {
+                session.removeAttribute((String) keys.next());
+            }
         }
     }
 
 
     public boolean containsKey(Object key) {
-        return (session.getAttribute(key(key)) != null);
+        if (sessionExists()) {
+            return (session.getAttribute(key(key)) != null);
+        } else {
+            return false;
+        }
     }
 
 
     public boolean containsValue(Object value) {
-        if (value == null) {
+        if (value == null || !sessionExists()) {
             return (false);
         }
         Enumeration keys =
@@ -78,43 +87,64 @@
 
     public Set entrySet() {
         Set set = new HashSet();
-        Enumeration keys =
-        session.getAttributeNames(PortletSession.PORTLET_SCOPE);
-        String key;
-        while (keys.hasMoreElements()) {
-            key = (String) keys.nextElement();
-            set.add(new MapEntry(key, session.getAttribute(key), true));
+        if (sessionExists()) {
+            Enumeration keys =
+            session.getAttributeNames(PortletSession.PORTLET_SCOPE);
+            String key;
+            while (keys.hasMoreElements()) {
+                key = (String) keys.nextElement();
+                set.add(new MapEntry(key, session.getAttribute(key), true));
+            }
         }
         return (set);
     }
 
 
     public boolean equals(Object o) {
-        return (session.equals(o));
+        if (sessionExists()) {
+            return (session.equals(o));
+        } else {
+            return false;
+        }
     }
 
 
     public Object get(Object key) {
-        return (session.getAttribute(key(key)));
+        if (sessionExists()) {
+            return (session.getAttribute(key(key)));
+        } else {
+            return null;
+        }
     }
 
 
     public int hashCode() {
-        return (session.hashCode());
+        if (sessionExists()) {
+            return (session.hashCode());
+        } else {
+            return 0;
+        }
     }
 
 
     public boolean isEmpty() {
-        return (size() < 1);
+        if (sessionExists() &&
+            session.getAttributeNames().hasMoreElements()) {
+            return false;
+        } else {
+            return true;
+        }
     }
 
 
     public Set keySet() {
         Set set = new HashSet();
-        Enumeration keys =
-        session.getAttributeNames(PortletSession.PORTLET_SCOPE);
-        while (keys.hasMoreElements()) {
-            set.add(keys.nextElement());
+        if (sessionExists()) {
+            Enumeration keys =
+            session.getAttributeNames(PortletSession.PORTLET_SCOPE);
+            while (keys.hasMoreElements()) {
+                set.add(keys.nextElement());
+            }
         }
         return (set);
     }
@@ -124,6 +154,14 @@
         if (value == null) {
             return (remove(key));
         }
+
+        // Ensure the Session is created, if it
+        // doesn't exist
+        if (session == null) {
+            session = request.getPortletSession();
+            request = null;
+        }
+
         String skey = key(key);
         Object previous = session.getAttribute(skey);
         session.setAttribute(skey, value);
@@ -134,27 +172,33 @@
     public void putAll(Map map) {
         Iterator keys = map.keySet().iterator();
         while (keys.hasNext()) {
-            String key = (String) keys.next();
-            session.setAttribute(key, map.get(key));
+            Object key = keys.next();
+            put(key, map.get(key));
         }
     }
 
 
     public Object remove(Object key) {
-        String skey = key(key);
-        Object previous = session.getAttribute(skey);
-        session.removeAttribute(skey);
-        return (previous);
+        if (sessionExists()) {
+            String skey = key(key);
+            Object previous = session.getAttribute(skey);
+            session.removeAttribute(skey);
+            return (previous);
+        } else {
+            return (null);
+        }
     }
 
 
     public int size() {
         int n = 0;
-        Enumeration keys =
-        session.getAttributeNames(PortletSession.PORTLET_SCOPE);
-        while (keys.hasMoreElements()) {
-            keys.nextElement();
-            n++;
+        if (sessionExists()) {
+            Enumeration keys =
+            session.getAttributeNames(PortletSession.PORTLET_SCOPE);
+            while (keys.hasMoreElements()) {
+                keys.nextElement();
+                n++;
+            }
         }
         return (n);
     }
@@ -162,10 +206,12 @@
 
     public Collection values() {
         List list = new ArrayList();
-        Enumeration keys =
-        session.getAttributeNames(PortletSession.PORTLET_SCOPE);
-        while (keys.hasMoreElements()) {
-            list.add(session.getAttribute((String) keys.nextElement()));
+        if (sessionExists()) {
+            Enumeration keys =
+            session.getAttributeNames(PortletSession.PORTLET_SCOPE);
+            while (keys.hasMoreElements()) {
+                list.add(session.getAttribute((String) keys.nextElement()));
+            }
         }
         return (list);
     }
@@ -181,5 +227,18 @@
         }
     }
 
+    private boolean sessionExists() {
+        if (session == null) {
+            session = request.getPortletSession(false);
+            if (session != null) {
+                request = null;
+            }
+        }
+        if (session != null) {
+            return true;
+        } else {
+            return false;
+        }
+    }
 
 }

Modified: 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java?rev=412789&r1=412788&r2=412789&view=diff
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
 Thu Jun  8 09:19:14 2006
@@ -361,7 +361,7 @@
 
         if ((sessionScope == null) && (request != null)) {
             sessionScope =
-        new PortletSessionScopeMap(request.getPortletSession());
+            new PortletSessionScopeMap(request);
         }
         return (sessionScope);
 

Modified: 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletSessionScopeMap.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletSessionScopeMap.java?rev=412789&r1=412788&r2=412789&view=diff
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletSessionScopeMap.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletSessionScopeMap.java
 Thu Jun  8 09:19:14 2006
@@ -25,6 +25,7 @@
 import java.util.Map;
 import java.util.Set;
 import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpServletRequest;
 import org.apache.commons.chain.web.MapEntry;
 
 
@@ -39,29 +40,37 @@
 final class ServletSessionScopeMap implements Map {
 
 
-    public ServletSessionScopeMap(HttpSession session) {
-        this.session = session;
+    public ServletSessionScopeMap(HttpServletRequest request) {
+        this.request = request;
+        sessionExists();
     }
 
 
     private HttpSession session = null;
+    private HttpServletRequest request = null;
 
 
     public void clear() {
-        Iterator keys = keySet().iterator();
-        while (keys.hasNext()) {
-            session.removeAttribute((String) keys.next());
+        if (sessionExists()) {
+            Iterator keys = keySet().iterator();
+            while (keys.hasNext()) {
+                session.removeAttribute((String) keys.next());
+            }
         }
     }
 
 
     public boolean containsKey(Object key) {
-        return (session.getAttribute(key(key)) != null);
+        if (sessionExists()) {
+            return (session.getAttribute(key(key)) != null);
+        } else {
+            return false;
+        }
     }
 
 
     public boolean containsValue(Object value) {
-        if (value == null) {
+        if (value == null || !sessionExists()) {
             return (false);
         }
         Enumeration keys = session.getAttributeNames();
@@ -77,41 +86,62 @@
 
     public Set entrySet() {
         Set set = new HashSet();
-        Enumeration keys = session.getAttributeNames();
-        String key;
-        while (keys.hasMoreElements()) {
-            key = (String) keys.nextElement();
-            set.add(new MapEntry(key, session.getAttribute(key), true));
+        if (sessionExists()) {
+            Enumeration keys = session.getAttributeNames();
+            String key;
+            while (keys.hasMoreElements()) {
+                key = (String) keys.nextElement();
+                set.add(new MapEntry(key, session.getAttribute(key), true));
+            }
         }
         return (set);
     }
 
 
     public boolean equals(Object o) {
-        return (session.equals(o));
+        if (sessionExists()) {
+            return (session.equals(o));
+        } else {
+            return false;
+        }
     }
 
 
     public Object get(Object key) {
-        return (session.getAttribute(key(key)));
+        if (sessionExists()) {
+            return (session.getAttribute(key(key)));
+        } else {
+            return null;
+        }
     }
 
 
     public int hashCode() {
-        return (session.hashCode());
+        if (sessionExists()) {
+            return (session.hashCode());
+        } else {
+            return 0;
+        }
     }
 
 
     public boolean isEmpty() {
-        return (size() < 1);
+        if (sessionExists() &&
+            session.getAttributeNames().hasMoreElements()) {
+            return false;
+        } else {
+            return true;
+        }
     }
 
 
     public Set keySet() {
         Set set = new HashSet();
-        Enumeration keys = session.getAttributeNames();
-        while (keys.hasMoreElements()) {
-            set.add(keys.nextElement());
+        if (sessionExists()) {
+            Enumeration keys = session.getAttributeNames();
+            while (keys.hasMoreElements()) {
+                set.add(keys.nextElement());
+            }
         }
         return (set);
     }
@@ -121,6 +151,14 @@
         if (value == null) {
             return (remove(key));
         }
+
+        // Ensure the Session is created, if it
+        // doesn't exist
+        if (session == null) {
+            session = request.getSession();
+            request = null;
+        }
+
         String skey = key(key);
         Object previous = session.getAttribute(skey);
         session.setAttribute(skey, value);
@@ -131,26 +169,32 @@
     public void putAll(Map map) {
         Iterator keys = map.keySet().iterator();
         while (keys.hasNext()) {
-            String key = (String) keys.next();
-            session.setAttribute(key, map.get(key));
+            Object key = keys.next();
+            put(key, map.get(key));
         }
     }
 
 
     public Object remove(Object key) {
-        String skey = key(key);
-        Object previous = session.getAttribute(skey);
-        session.removeAttribute(skey);
-        return (previous);
+        if (sessionExists()) {
+            String skey = key(key);
+            Object previous = session.getAttribute(skey);
+            session.removeAttribute(skey);
+            return (previous);
+        } else {
+            return (null);
+        }
     }
 
 
     public int size() {
         int n = 0;
-        Enumeration keys = session.getAttributeNames();
-        while (keys.hasMoreElements()) {
-            keys.nextElement();
-            n++;
+        if (sessionExists()) {
+            Enumeration keys = session.getAttributeNames();
+            while (keys.hasMoreElements()) {
+                keys.nextElement();
+                n++;
+            }
         }
         return (n);
     }
@@ -158,9 +202,11 @@
 
     public Collection values() {
         List list = new ArrayList();
-        Enumeration keys = session.getAttributeNames();
-        while (keys.hasMoreElements()) {
-            list.add(session.getAttribute((String) keys.nextElement()));
+        if (sessionExists()) {
+            Enumeration keys = session.getAttributeNames();
+            while (keys.hasMoreElements()) {
+                list.add(session.getAttribute((String) keys.nextElement()));
+            }
         }
         return (list);
     }
@@ -176,5 +222,18 @@
         }
     }
 
+    private boolean sessionExists() {
+        if (session == null) {
+            session = request.getSession(false);
+            if (session != null) {
+                request = null;
+            }
+        }
+        if (session != null) {
+            return true;
+        } else {
+            return false;
+        }
+    }
 
 }

Modified: 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java?rev=412789&r1=412788&r2=412789&view=diff
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
 Thu Jun  8 09:19:14 2006
@@ -367,7 +367,7 @@
     public Map getSessionScope() {
 
         if ((sessionScope == null) && (request != null)) {
-            sessionScope = new ServletSessionScopeMap(request.getSession());
+            sessionScope = new ServletSessionScopeMap(request);
         }
         return (sessionScope);
 

Modified: 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java?rev=412789&r1=412788&r2=412789&view=diff
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/portlet/PortletWebContextTestCase.java
 Thu Jun  8 09:19:14 2006
@@ -573,6 +573,89 @@
     }
 
 
+    // Test getSessionScope() without Session
+    public void testSessionScopeWithoutSession() {
+
+        // Create a Context without a session
+        PortletWebContext ctx = new PortletWebContext(pcontext, 
+           new MockPortletRequest(), response);
+        assertNull("Session(A)", ctx.getRequest().getPortletSession(false));
+
+        // Get the session Map & check session doesn't exist
+        Map sessionMap = ctx.getSessionScope();
+        assertNull("Session(B)", ctx.getRequest().getPortletSession(false));
+        assertNotNull("Session Map(A)", sessionMap);
+
+        // test clear()
+        sessionMap.clear();
+        assertNull("Session(C)", ctx.getRequest().getPortletSession(false));
+
+        // test containsKey()
+        assertFalse("containsKey()", sessionMap.containsKey("ABC"));
+        assertNull("Session(D)", ctx.getRequest().getPortletSession(false));
+
+        // test containsValue()
+        assertFalse("containsValue()", sessionMap.containsValue("ABC"));
+        assertNull("Session(E)", ctx.getRequest().getPortletSession(false));
+
+        // test entrySet()
+        Set entrySet = sessionMap.entrySet();
+        assertNotNull("entrySet", entrySet);
+        assertEquals("entrySet Size", 0, entrySet.size());
+        assertNull("Session(F)", ctx.getRequest().getPortletSession(false));
+
+        // test equals()
+        assertFalse("equals()", sessionMap.equals("ABC"));
+        assertNull("Session(G)", ctx.getRequest().getPortletSession(false));
+
+        // test get()
+        assertNull("get()", sessionMap.get("ABC"));
+        assertNull("Session(H)", ctx.getRequest().getPortletSession(false));
+
+        // test hashCode()
+        sessionMap.hashCode();
+        assertNull("Session(I)", ctx.getRequest().getPortletSession(false));
+
+        // test isEmpty()
+        assertTrue("isEmpty()", sessionMap.isEmpty());
+        assertNull("Session(J)", ctx.getRequest().getPortletSession(false));
+
+        // test keySet()
+        Set keySet = sessionMap.keySet();
+        assertNotNull("keySet", keySet);
+        assertEquals("keySet Size", 0, keySet.size());
+        assertNull("Session(K)", ctx.getRequest().getPortletSession(false));
+
+        // test putAll() with an empty Map
+        sessionMap.putAll(new HashMap());
+        assertNull("Session(L)", ctx.getRequest().getPortletSession(false));
+
+        // test remove()
+        assertNull("remove()", sessionMap.remove("ABC"));
+        assertNull("Session(M)", ctx.getRequest().getPortletSession(false));
+
+        // test size()
+        assertEquals("size() Size", 0, sessionMap.size());
+        assertNull("Session(N)", ctx.getRequest().getPortletSession(false));
+
+        // test values()
+        Collection values = sessionMap.values();
+        assertNotNull("values", values);
+        assertEquals("values Size", 0, values.size());
+        assertNull("Session(O)", ctx.getRequest().getPortletSession(false));
+
+        // test put()
+        try {
+            assertNull("put()", sessionMap.put("ABC", "XYZ"));
+            assertNotNull("Session(P)", 
ctx.getRequest().getPortletSession(false));
+        } catch(UnsupportedOperationException ex) {
+            // expected: currently MockPortletRequest throws this
+            //           when trying to create a PortletSession
+        }
+
+    }
+
+
     // ------------------------------------------------------- Protected 
Methods
 
 

Modified: 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java?rev=412789&r1=412788&r2=412789&view=diff
==============================================================================
--- 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
 (original)
+++ 
jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
 Thu Jun  8 09:19:14 2006
@@ -31,6 +31,7 @@
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
+import java.util.Collection;
 
 
 /**
@@ -678,6 +679,89 @@
         // Clearing the map
         map.clear();
         checkMapSize(map, 0);
+
+    }
+
+
+    // Test getSessionScope() without Session
+    public void testSessionScopeWithoutSession() {
+
+        // Create a Context without a session
+        ServletWebContext ctx = new ServletWebContext(scontext, 
+           new MockHttpServletRequest(), response);
+        assertNull("Session(A)", ctx.getRequest().getSession(false));
+
+        // Get the session Map & check session doesn't exist
+        Map sessionMap = ctx.getSessionScope();
+        assertNull("Session(B)", ctx.getRequest().getSession(false));
+        assertNotNull("Session Map(A)", sessionMap);
+
+        // test clear()
+        sessionMap.clear();
+        assertNull("Session(C)", ctx.getRequest().getSession(false));
+
+        // test containsKey()
+        assertFalse("containsKey()", sessionMap.containsKey("ABC"));
+        assertNull("Session(D)", ctx.getRequest().getSession(false));
+
+        // test containsValue()
+        assertFalse("containsValue()", sessionMap.containsValue("ABC"));
+        assertNull("Session(E)", ctx.getRequest().getSession(false));
+
+        // test entrySet()
+        Set entrySet = sessionMap.entrySet();
+        assertNotNull("entrySet", entrySet);
+        assertEquals("entrySet Size", 0, entrySet.size());
+        assertNull("Session(F)", ctx.getRequest().getSession(false));
+
+        // test equals()
+        assertFalse("equals()", sessionMap.equals("ABC"));
+        assertNull("Session(G)", ctx.getRequest().getSession(false));
+
+        // test get()
+        assertNull("get()", sessionMap.get("ABC"));
+        assertNull("Session(H)", ctx.getRequest().getSession(false));
+
+        // test hashCode()
+        sessionMap.hashCode();
+        assertNull("Session(I)", ctx.getRequest().getSession(false));
+
+        // test isEmpty()
+        assertTrue("isEmpty()", sessionMap.isEmpty());
+        assertNull("Session(J)", ctx.getRequest().getSession(false));
+
+        // test keySet()
+        Set keySet = sessionMap.keySet();
+        assertNotNull("keySet", keySet);
+        assertEquals("keySet Size", 0, keySet.size());
+        assertNull("Session(K)", ctx.getRequest().getSession(false));
+
+        // test putAll() with an empty Map
+        sessionMap.putAll(new HashMap());
+        assertNull("Session(L)", ctx.getRequest().getSession(false));
+
+        // test remove()
+        assertNull("remove()", sessionMap.remove("ABC"));
+        assertNull("Session(M)", ctx.getRequest().getSession(false));
+
+        // test size()
+        assertEquals("size() Size", 0, sessionMap.size());
+        assertNull("Session(N)", ctx.getRequest().getSession(false));
+
+        // test values()
+        Collection values = sessionMap.values();
+        assertNotNull("values", values);
+        assertEquals("values Size", 0, values.size());
+        assertNull("Session(O)", ctx.getRequest().getSession(false));
+
+        // test put()
+        try {
+            assertNull("put()", sessionMap.put("ABC", "XYZ"));
+            assertNotNull("Session(P)", ctx.getRequest().getSession(false));
+        } catch(UnsupportedOperationException ex) {
+            // expected: currently MockHttpServletRequest throws this
+            //           when trying to create a HttpSession
+        }
 
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to