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 2013/12/17 23:57:48 UTC

[1/2] git commit: [KARAF-2628] Fix synchronization issues in the commands completer

Updated Branches:
  refs/heads/karaf-2.x 13ac82343 -> 97914c265


[KARAF-2628] Fix synchronization issues in the commands completer


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

Branch: refs/heads/karaf-2.x
Commit: 538d2df83f16fff71893325cd2d04c3c3cc25581
Parents: 13ac823
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Tue Dec 17 10:37:07 2013 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Tue Dec 17 23:57:23 2013 +0100

----------------------------------------------------------------------
 .../completer/CommandNamesCompleter.java        |  3 +
 .../console/completer/CommandsCompleter.java    | 64 +++++++++++++++++---
 2 files changed, 57 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/538d2df8/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandNamesCompleter.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandNamesCompleter.java b/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandNamesCompleter.java
index 8039fc5..e113686 100644
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandNamesCompleter.java
+++ b/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandNamesCompleter.java
@@ -83,6 +83,9 @@ public class CommandNamesCompleter implements Completer {
     private class CommandTracker {
         public CommandTracker() throws Exception {
             BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
+            if (context == null) {
+                throw new IllegalStateException("Bundle is stopped");
+            }
             ServiceListener listener = new ServiceListener() {
                 public void serviceChanged(ServiceEvent event) {
                     commands.clear();

http://git-wip-us.apache.org/repos/asf/karaf/blob/538d2df8/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java b/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java
index 65e1405..bed1148 100644
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java
+++ b/shell/console/src/main/java/org/apache/karaf/shell/console/completer/CommandsCompleter.java
@@ -26,11 +26,15 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.felix.gogo.commands.basic.AbstractCommand;
+import org.apache.felix.service.command.CommandProcessor;
 import org.apache.felix.service.command.CommandSession;
 import org.apache.felix.service.command.Function;
 import org.apache.karaf.shell.console.Completer;
 import org.apache.karaf.shell.console.jline.CommandSessionHolder;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -55,6 +59,11 @@ public class CommandsCompleter implements Completer {
 
     public CommandsCompleter(CommandSession session) {
         this.session = session;
+        try {
+            new CommandTracker();
+        } catch (Throwable t) {
+            // Ignore in case we're not in OSGi
+        }
     }
 
 
@@ -63,21 +72,28 @@ public class CommandsCompleter implements Completer {
             session = CommandSessionHolder.getSession();
         }
         checkData();
-        int res = new AggregateCompleter(completers).complete(buffer, cursor, candidates);
+        int res;
+        synchronized (this) {
+            res = new AggregateCompleter(completers).complete(buffer, cursor, candidates);
+        }
         Collections.sort(candidates);
         return res;
     }
 
-    protected synchronized void checkData() {
+    protected void checkData() {
         // Copy the set to avoid concurrent modification exceptions
         // TODO: fix that in gogo instead
-        Set<String> names = new HashSet<String>((Set<String>) session.get(COMMANDS));
-        if (!names.equals(commands)) {
-            commands.clear();
-            completers.clear();
-
+        Set<String> names;
+        boolean update;
+        synchronized (this) {
+            names = new HashSet<String>((Set<String>) session.get(COMMANDS));
+            update = !names.equals(commands);
+        }
+        if (update) {
             // get command aliases
             Set<String> aliases = this.getAliases();
+            Set<String> commands = new HashSet<String>();
+            List<Completer> completers = new ArrayList<Completer>();
             completers.add(new StringsCompleter(aliases));
 
             // add argument completers for each command
@@ -93,6 +109,13 @@ public class CommandsCompleter implements Completer {
                 }
                 commands.add(command);
             }
+
+            synchronized (this) {
+                this.commands.clear();
+                this.completers.clear();
+                this.commands.addAll(commands);
+                this.completers.addAll(completers);
+            }
         }
     }
 
@@ -106,7 +129,7 @@ public class CommandsCompleter implements Completer {
         Set<String> aliases = new HashSet<String>();
         for (String var : vars) {
             Object content = session.get(var);
-            if ("org.apache.felix.gogo.runtime.Closure".equals(content.getClass().getName()))  {
+            if (content != null && "org.apache.felix.gogo.runtime.Closure".equals(content.getClass().getName()))  {
                 aliases.add(var);
             }
         }
@@ -122,13 +145,15 @@ public class CommandsCompleter implements Completer {
                 referenceField.setAccessible(true);
                 BundleContext context = (BundleContext) contextField.get(function);
                 ServiceReference reference = (ServiceReference) referenceField.get(function);
-                Object target = context.getService(reference);
+                Object target = context != null ? context.getService(reference) : null;
                 try {
                     if (target instanceof Function) {
                         function = (Function) target;
                     }
                 } finally {
-                    context.ungetService(reference);
+                    if (context != null) {
+                        context.ungetService(reference);
+                    }
                 }
             }
         } catch (Throwable t) {
@@ -136,5 +161,24 @@ public class CommandsCompleter implements Completer {
         return function;
     }
 
+    private class CommandTracker {
+        public CommandTracker() throws Exception {
+            BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
+            if (context == null) {
+                throw new IllegalStateException("Bundle is stopped");
+            }
+            ServiceListener listener = new ServiceListener() {
+                public void serviceChanged(ServiceEvent event) {
+                    synchronized (CommandsCompleter.this) {
+                        commands.clear();
+                    }
+                }
+            };
+            context.addServiceListener(listener,
+                    String.format("(&(%s=*)(%s=*))",
+                            CommandProcessor.COMMAND_SCOPE,
+                            CommandProcessor.COMMAND_FUNCTION));
+        }
+    }
 }
 


[2/2] git commit: [KARAF-2632] Handle backslashes at end of line in the console

Posted by gn...@apache.org.
[KARAF-2632] Handle backslashes at end of line in the console


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

Branch: refs/heads/karaf-2.x
Commit: 97914c265a9935ec26a89db2259fb8e8e1c40c9b
Parents: 538d2df
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Tue Dec 17 23:56:24 2013 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Tue Dec 17 23:57:26 2013 +0100

----------------------------------------------------------------------
 .../karaf/shell/console/jline/Console.java      | 29 +++++++++++++-------
 1 file changed, 19 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/97914c26/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java b/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java
index 26adaae..4930d99 100644
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java
+++ b/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java
@@ -261,7 +261,11 @@ public class Console implements Runnable
             if (command == null) {
                 command = line;
             } else {
-                command += " " + line;
+                if (command.charAt(command.length() - 1) == '\\') {
+                    command = command.substring(0, command.length() - 1) + line;
+                } else {
+                    command += "\n" + line;
+                }
             }
             if (reader.getHistory().size()==0) {
                 reader.getHistory().add(command);
@@ -273,17 +277,22 @@ public class Console implements Runnable
                     reader.getHistory().replace(command);    
                 }                                
             }
-            try {
-                Class<?> cl = CommandSession.class.getClassLoader().loadClass("org.apache.felix.gogo.runtime.Parser");
-                Object parser = cl.getConstructor(CharSequence.class).newInstance(command);
-                cl.getMethod("program").invoke(parser);
-                loop = false;
-            } catch (Exception e) {
+            if (command.length() > 0 && command.charAt(command.length() - 1) == '\\') {
                 loop = true;
                 first = false;
-            } catch (Throwable t) {
-                // Reflection problem ? just quit
-                loop = false;
+            } else {
+                try {
+                    Class<?> cl = CommandSession.class.getClassLoader().loadClass("org.apache.felix.gogo.runtime.Parser");
+                    Object parser = cl.getConstructor(CharSequence.class).newInstance(command);
+                    cl.getMethod("program").invoke(parser);
+                    loop = false;
+                } catch (Exception e) {
+                    loop = true;
+                    first = false;
+                } catch (Throwable t) {
+                    // Reflection problem ? just quit
+                    loop = false;
+                }
             }
         }
         return command;