[FELIX-2606] Decoupled the CommandProcessorImpl from osgi a bit so that it can be reused to implement non-osgi shells.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@999273 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
index bed918c..724f189 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
@@ -21,13 +21,7 @@
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 @@
{
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 @@
return commands.keySet();
}
- BundleContext getContext()
- {
- return context;
- }
-
Function getCommand(String name, final Object path)
{
int colon = name.indexOf(':');
@@ -161,6 +146,16 @@
}
}
+ 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);
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
index 6159511..a8b042a 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
@@ -36,7 +36,6 @@
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 @@
return processor.getCommands();
}
- if (CONTEXT.equals(name))
+ if( processor.constants.containsKey(name) )
{
- return processor.getContext();
+ return processor.constants.get(name);
}
if (variables.containsKey(name))
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
index d5823c1..d42a898 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
@@ -45,9 +45,18 @@
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);
}
diff --git a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java
index 42ba180..adbb66f 100644
--- a/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java
+++ b/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java
@@ -36,7 +36,10 @@
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);
}