You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2010/07/26 12:26:53 UTC

svn commit: r979229 - in /myfaces/core/trunk/impl/src/main/java/org/apache/myfaces: context/servlet/SessionMap.java util/AbstractAttributeMap.java

Author: jakobk
Date: Mon Jul 26 10:26:53 2010
New Revision: 979229

URL: http://svn.apache.org/viewvc?rev=979229&view=rev
Log:
MYFACES-2840 Use a copied Iterator instead of the real Enumeration in AbstractAttributeMap.AbstractAttributeIterator (fix for 2.0.x + minor cleanup in AbstractAttributeMap and in SessionMap)

Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/SessionMap.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/util/AbstractAttributeMap.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/SessionMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/SessionMap.java?rev=979229&r1=979228&r2=979229&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/SessionMap.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/SessionMap.java Mon Jul 26 10:26:53 2010
@@ -46,7 +46,7 @@ public final class SessionMap extends Ab
     @Override
     protected Object getAttribute(final String key)
     {
-        final HttpSession httpSession = getSession();
+        final HttpSession httpSession = _getSession();
         return (httpSession == null) ? null : httpSession.getAttribute(key);
     }
 
@@ -59,7 +59,7 @@ public final class SessionMap extends Ab
     @Override
     protected void removeAttribute(final String key)
     {
-        final HttpSession httpSession = getSession();
+        final HttpSession httpSession = _getSession();
         if (httpSession != null)
         {
             httpSession.removeAttribute(key);
@@ -70,38 +70,21 @@ public final class SessionMap extends Ab
     @SuppressWarnings("unchecked")
     protected Enumeration<String> getAttributeNames()
     {
-        final HttpSession httpSession = getSession();
+        final HttpSession httpSession = _getSession();
         return (httpSession == null) ? NullEnumeration.instance() : httpSession.getAttributeNames();
     }
 
-    private HttpSession getSession()
-    {
-        return _httpRequest.getSession(false);
-    }
-
     @Override
     public void putAll(final Map<? extends String, ? extends Object> t)
     {
         throw new UnsupportedOperationException();
     }
-
-    /**
-     * This will clear the session without invalidation. If no session has been created, it will simply return.
-     */
-    @SuppressWarnings("unchecked")
-    @Override
-    public void clear()
+    
+    // we can use public void clear() from super-class
+    
+    private HttpSession _getSession()
     {
-        final HttpSession session = getSession();
-        if (session == null)
-        {
-            return;
-        }
-        
-        Enumeration<String> attributeNames = session.getAttributeNames();
-        while (attributeNames.hasMoreElements())
-        {
-            session.removeAttribute(attributeNames.nextElement());
-        }
+        return _httpRequest.getSession(false);
     }
-}
\ No newline at end of file
+
+}

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/util/AbstractAttributeMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/util/AbstractAttributeMap.java?rev=979229&r1=979228&r2=979229&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/util/AbstractAttributeMap.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/util/AbstractAttributeMap.java Mon Jul 26 10:26:53 2010
@@ -20,8 +20,8 @@ package org.apache.myfaces.util;
 
 import java.util.AbstractMap;
 import java.util.AbstractSet;
-import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.List;
@@ -44,11 +44,7 @@ public abstract class AbstractAttributeM
     @Override
     public void clear()
     {
-        final List<String> names = new ArrayList<String>();
-        for (final Enumeration<String> e = getAttributeNames(); e.hasMoreElements();)
-        {
-            names.add(e.nextElement());
-        }
+        final List<String> names = Collections.list(getAttributeNames());
 
         for (String name : names)
         {
@@ -203,14 +199,17 @@ public abstract class AbstractAttributeM
 
     private abstract class AbstractAttributeIterator<E> implements Iterator<E>
     {
-        protected final Enumeration<String> _e = getAttributeNames();
+        // We use a copied version of the Enumeration from getAttributeNames()
+        // here, because directly using it might cause a ConcurrentModificationException
+        // when performing remove(). Note that we can do this since the Enumeration
+        // from getAttributeNames() will contain exactly the attribute names from the time
+        // getAttributeNames() was called and it will not be updated if attributes are 
+        // removed or added.
+        protected final Iterator<String> _i = Collections.list(getAttributeNames()).iterator();
         protected String _currentKey;
 
         public void remove()
         {
-            // remove() may cause ConcurrentModificationException.
-            // We could throw an exception here, but not throwing an exception
-            // allows one call to remove() to succeed
             if (_currentKey == null)
             {
                 throw new NoSuchElementException("You must call next() at least once");
@@ -220,12 +219,12 @@ public abstract class AbstractAttributeM
 
         public boolean hasNext()
         {
-            return _e.hasMoreElements();
+            return _i.hasNext();
         }
 
         public E next()
         {
-            return getValue(_currentKey = _e.nextElement());
+            return getValue(_currentKey = _i.next());
         }
 
         protected abstract E getValue(String attributeName);