Author: gnodet
Date: Tue Sep 21 08:43:27 2010
New Revision: 999273

URL: http://svn.apache.org/viewvc?rev=999273&view=rev
Log:
[FELIX-2606] Decoupled the CommandProcessorImpl from osgi a bit so that it can 
be reused  to implement non-osgi shells.

Modified:
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
    
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
    
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.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=999273&r1=999272&r2=999273&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
 Tue Sep 21 08:43:27 2010
@@ -21,13 +21,7 @@ package org.apache.felix.gogo.runtime;
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.lang.reflect.Method;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-import java.util.WeakHashMap;
+import java.util.*;
 import java.util.Map.Entry;
 
 import org.osgi.framework.BundleContext;
@@ -41,17 +35,13 @@ public class CommandProcessorImpl implem
 {
     protected final Set<Converter> converters = new HashSet<Converter>();
     protected final Map<String, Object> commands = new LinkedHashMap<String, 
Object>();
-    protected final BundleContext context;
+    protected final Map<String, Object> constants = new HashMap<String, 
Object>();
     protected final ThreadIO threadIO;
     protected final WeakHashMap<CommandSession, Object> sessions = new 
WeakHashMap<CommandSession, Object>();
 
-    public CommandProcessorImpl(ThreadIO tio, BundleContext context)
+    public CommandProcessorImpl(ThreadIO tio)
     {
         threadIO = tio;
-        this.context = context;
-        addCommand("osgi", this, "addCommand");
-        addCommand("osgi", this, "removeCommand");
-        addCommand("osgi", this, "eval");
     }
 
     public CommandSession createSession(InputStream in, PrintStream out, 
PrintStream err)
@@ -84,11 +74,6 @@ public class CommandProcessorImpl implem
         return commands.keySet();
     }
 
-    BundleContext getContext()
-    {
-        return context;
-    }
-
     Function getCommand(String name, final Object path)
     {
         int colon = name.indexOf(':');
@@ -161,6 +146,16 @@ public class CommandProcessorImpl implem
         }
     }
 
+    public Object addConstant(String name, Object target)
+    {
+        return constants.put(name, target);
+    }
+
+    public Object removeConstant(String name)
+    {
+        return constants.remove(name);
+    }
+
     public void addCommand(String scope, Object target, String function)
     {
         commands.put((scope + ":" + function).toLowerCase(), target);

Modified: 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java?rev=999273&r1=999272&r2=999273&view=diff
==============================================================================
--- 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
 (original)
+++ 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
 Tue Sep 21 08:43:27 2010
@@ -36,7 +36,6 @@ public class CommandSessionImpl implemen
     public static final String SESSION_CLOSED = "session is closed";
     public static final String VARIABLES = ".variables";
     public static final String COMMANDS = ".commands";
-    public static final String CONTEXT = ".context";
     private static final String COLUMN = "%-20s %s\n";
     
     protected InputStream in;
@@ -98,9 +97,9 @@ public class CommandSessionImpl implemen
             return processor.getCommands();
         }
 
-        if (CONTEXT.equals(name))
+        if( processor.constants.containsKey(name) )
         {
-            return processor.getContext();
+            return processor.constants.get(name);
         }
 
         if (variables.containsKey(name))

Modified: 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java?rev=999273&r1=999272&r2=999273&view=diff
==============================================================================
--- 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
 (original)
+++ 
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
 Tue Sep 21 08:43:27 2010
@@ -45,9 +45,18 @@ public class Activator implements Bundle
     private ServiceRegistration processorRegistration;
     private ServiceRegistration threadioRegistration;
     
+    public static final String CONTEXT = ".context";
+
     protected ServiceRegistration newProcessor(ThreadIO tio, BundleContext 
context)
     {
-        processor = new CommandProcessorImpl(tio, context);
+        processor = new CommandProcessorImpl(tio);
+
+        // Setup the variables and commands exposed in an OSGi environment.
+        processor.addConstant(CONTEXT, context);
+        processor.addCommand("osgi", processor, "addCommand");
+        processor.addCommand("osgi", processor, "removeCommand");
+        processor.addCommand("osgi", processor, "eval");
+
         return context.registerService(CommandProcessor.class.getName(), 
processor, null);
     }
 

Modified: 
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java?rev=999273&r1=999272&r2=999273&view=diff
==============================================================================
--- 
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java
 (original)
+++ 
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java
 Tue Sep 21 08:43:27 2010
@@ -36,7 +36,10 @@ public class Context extends CommandProc
 
     public Context()
     {
-        super(threadio, null);
+        super(threadio);
+        addCommand("osgi", this, "addCommand");
+        addCommand("osgi", this, "removeCommand");
+        addCommand("osgi", this, "eval");
         session = (CommandSessionImpl) createSession(System.in, System.out, 
System.err);
     }
 


Reply via email to