You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by db...@apache.org on 2011/03/02 22:50:40 UTC

svn commit: r1076415 - /felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java

Author: dbaum
Date: Wed Mar  2 21:50:40 2011
New Revision: 1076415

URL: http://svn.apache.org/viewvc?rev=1076415&view=rev
Log:
synchronize access to commands to avoid ConcurrentModificationException (FELIX-2870)

Modified:
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java?rev=1076415&r1=1076414&r2=1076415&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java Wed Mar  2 21:50:40 2011
@@ -21,6 +21,7 @@ package org.apache.felix.gogo.runtime;
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.lang.reflect.Method;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -67,7 +68,7 @@ public class CommandProcessorImpl implem
             session.close();
         }
     }
-    
+
     public void addConverter(Converter c)
     {
         converters.add(c);
@@ -88,10 +89,9 @@ public class CommandProcessorImpl implem
         listeners.remove(l);
     }
 
-
     public Set<String> getCommands()
     {
-        return commands.keySet();
+        return Collections.unmodifiableSet(commands.keySet());
     }
 
     Function getCommand(String name, final Object path)
@@ -104,10 +104,11 @@ public class CommandProcessorImpl implem
         }
 
         name = name.toLowerCase();
-        Object cmd = commands.get(name);
         String cfunction = name.substring(colon);
         boolean anyScope = (colon == 1 && name.charAt(0) == '*');
 
+        Object cmd = commands.get(name);
+
         if (null == cmd && anyScope)
         {
             String scopePath = (null == path ? "*" : path.toString());
@@ -116,12 +117,15 @@ public class CommandProcessorImpl implem
             {
                 if (scope.equals("*"))
                 {
-                    for (Entry<String, Object> entry : commands.entrySet())
+                    synchronized (commands)
                     {
-                        if (entry.getKey().endsWith(cfunction))
+                        for (Entry<String, Object> entry : commands.entrySet())
                         {
-                            cmd = entry.getValue();
-                            break;
+                            if (entry.getKey().endsWith(cfunction))
+                            {
+                                cmd = entry.getValue();
+                                break;
+                            }
                         }
                     }
                 }
@@ -178,22 +182,31 @@ public class CommandProcessorImpl implem
 
     public void addCommand(String scope, Object target, String function)
     {
-        commands.put((scope + ":" + function).toLowerCase(), target);
+        synchronized (commands)
+        {
+            commands.put((scope + ":" + function).toLowerCase(), target);
+        }
     }
 
     public void removeCommand(String scope, String function)
     {
         String func = (scope + ":" + function).toLowerCase();
-        commands.remove(func);
+        synchronized (commands)
+        {
+            commands.remove(func);
+        }
     }
 
     public void removeCommand(Object target)
     {
-        for (Iterator<Object> i = commands.values().iterator(); i.hasNext();)
+        synchronized (commands)
         {
-            if (i.next() == target)
+            for (Iterator<Object> i = commands.values().iterator(); i.hasNext();)
             {
-                i.remove();
+                if (i.next() == target)
+                {
+                    i.remove();
+                }
             }
         }
     }
@@ -226,7 +239,10 @@ public class CommandProcessorImpl implem
 
     protected void put(String name, Object target)
     {
-        commands.put(name, target);
+        synchronized (commands)
+        {
+            commands.put(name, target);
+        }
     }
 
     public Object convert(Class<?> desiredType, Object in)
@@ -279,7 +295,8 @@ public class CommandProcessorImpl implem
         }
     }
 
-    void afterExecute(CommandSession session, CharSequence commandline, Exception exception)
+    void afterExecute(CommandSession session, CharSequence commandline,
+        Exception exception)
     {
         for (CommandSessionListener l : listeners)
         {