You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2014/03/24 17:32:01 UTC

[03/24] git commit: Fix concurrency issues in shell/core

Fix concurrency issues in shell/core


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/9483e506
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/9483e506
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/9483e506

Branch: refs/heads/master
Commit: 9483e50620e379ae05e6d63fd6fbb3a528e48656
Parents: b509da2
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Mon Mar 24 13:17:48 2014 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Mon Mar 24 17:30:12 2014 +0100

----------------------------------------------------------------------
 .../karaf/shell/impl/console/RegistryImpl.java  |  4 +-
 .../shell/impl/console/SessionFactoryImpl.java  | 54 +++++++++++---------
 2 files changed, 31 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/9483e506/shell/core/src/main/java/org/apache/karaf/shell/impl/console/RegistryImpl.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/RegistryImpl.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/RegistryImpl.java
index 43ed27e..b607c5e 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/RegistryImpl.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/RegistryImpl.java
@@ -29,8 +29,8 @@ import org.apache.karaf.shell.api.console.Registry;
 
 public class RegistryImpl implements Registry {
 
-    private final Registry parent;
-    private final Map<Object, Object> services = new LinkedHashMap<Object, Object>();
+    protected final Registry parent;
+    protected final Map<Object, Object> services = new LinkedHashMap<Object, Object>();
 
     public RegistryImpl(Registry parent) {
         this.parent = parent;

http://git-wip-us.apache.org/repos/asf/karaf/blob/9483e506/shell/core/src/main/java/org/apache/karaf/shell/impl/console/SessionFactoryImpl.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/SessionFactoryImpl.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/SessionFactoryImpl.java
index be7aaea..1a72e33 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/SessionFactoryImpl.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/SessionFactoryImpl.java
@@ -64,21 +64,23 @@ public class SessionFactoryImpl extends RegistryImpl implements SessionFactory,
 
     @Override
     public void register(Object service) {
-        if (service instanceof Command) {
-            Command command = (Command) service;
-            String scope = command.getScope();
-            String name = command.getName();
-            if (!Session.SCOPE_GLOBAL.equals(scope)) {
-                if (!subshells.containsKey(scope)) {
-                    SubShellCommand subShell = new SubShellCommand(scope);
-                    subshells.put(scope, subShell);
-                    register(subShell);
+        synchronized (services) {
+            if (service instanceof Command) {
+                Command command = (Command) service;
+                String scope = command.getScope();
+                String name = command.getName();
+                if (!Session.SCOPE_GLOBAL.equals(scope)) {
+                    if (!subshells.containsKey(scope)) {
+                        SubShellCommand subShell = new SubShellCommand(scope);
+                        subshells.put(scope, subShell);
+                        register(subShell);
+                    }
+                    subshells.get(scope).increment();
                 }
-                subshells.get(scope).increment();
+                commandProcessor.addCommand(scope, wrap(command), name);
             }
-            commandProcessor.addCommand(scope, wrap(command), name);
+            super.register(service);
         }
-        super.register(service);
     }
 
     protected Function wrap(Command command) {
@@ -87,16 +89,18 @@ public class SessionFactoryImpl extends RegistryImpl implements SessionFactory,
 
     @Override
     public void unregister(Object service) {
-        super.unregister(service);
-        if (service instanceof Command) {
-            Command command = (Command) service;
-            String scope = command.getScope();
-            String name = command.getName();
-            commandProcessor.removeCommand(scope, name);
-            if (!Session.SCOPE_GLOBAL.equals(scope)) {
-                if (subshells.get(scope).decrement() == 0) {
-                    SubShellCommand subShell = subshells.remove(scope);
-                    unregister(subShell);
+        synchronized (services) {
+            super.unregister(service);
+            if (service instanceof Command) {
+                Command command = (Command) service;
+                String scope = command.getScope();
+                String name = command.getName();
+                commandProcessor.removeCommand(scope, name);
+                if (!Session.SCOPE_GLOBAL.equals(scope)) {
+                    if (subshells.get(scope).decrement() == 0) {
+                        SubShellCommand subShell = subshells.remove(scope);
+                        unregister(subShell);
+                    }
                 }
             }
         }
@@ -104,7 +108,7 @@ public class SessionFactoryImpl extends RegistryImpl implements SessionFactory,
 
     @Override
     public Session create(InputStream in, PrintStream out, PrintStream err, Terminal term, String encoding, Runnable closeCallback) {
-        synchronized (this) {
+        synchronized (sessions) {
             if (closed) {
                 throw new IllegalStateException("SessionFactory has been closed");
             }
@@ -116,7 +120,7 @@ public class SessionFactoryImpl extends RegistryImpl implements SessionFactory,
 
     @Override
     public Session create(InputStream in, PrintStream out, PrintStream err) {
-        synchronized (this) {
+        synchronized (sessions) {
             if (closed) {
                 throw new IllegalStateException("SessionFactory has been closed");
             }
@@ -127,7 +131,7 @@ public class SessionFactoryImpl extends RegistryImpl implements SessionFactory,
     }
 
     public void stop() {
-        synchronized (this) {
+        synchronized (sessions) {
             closed = true;
             for (Session session : sessions) {
                 session.close();