Allow retrieving the command lazily

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@793045 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java
index 4b9d215..bca9811 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java
@@ -21,16 +21,20 @@
 import org.apache.felix.gogo.runtime.lang.Support;
 import org.apache.felix.gogo.runtime.osgi.OSGiShell;
 import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
+import org.apache.felix.gogo.runtime.shell.CommandProxy;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.command.CommandProcessor;
 import org.osgi.service.command.Converter;
+import org.osgi.service.command.Function;
 import org.osgi.service.threadio.ThreadIO;
 import org.osgi.util.tracker.ServiceTracker;
 
 import java.util.Hashtable;
+import java.util.ArrayList;
+import java.util.List;
 
 public class Activator implements BundleActivator
 {
@@ -78,21 +82,25 @@
             {
                 Object scope = reference.getProperty("osgi.command.scope");
                 Object function = reference.getProperty("osgi.command.function");
+                List<Object> commands = new ArrayList<Object>();
                 if(scope != null && function != null)
                 {
-                    Object target = context.getService(reference);
                     if (function.getClass().isArray())
                     {
                         for (Object f : ((Object[]) function))
                         {
+                            Function target = new CommandProxy(context, reference, f.toString());
                             shell.addCommand(scope.toString(), target, f.toString());
+                            commands.add(target);
                         }
                     }
                     else
                     {
+                        Function target = new CommandProxy(context, reference, function.toString());
                         shell.addCommand(scope.toString(), target, function.toString());
+                        commands.add(target);
                     }
-                    return target;
+                    return commands;
                 }
                 return null;
             }
@@ -100,6 +108,10 @@
             @Override
             public void removedService(ServiceReference reference, Object service)
             {
+                List<Object> commands = (List<Object>) service;
+                for (Object cmd : commands) {
+                    shell.removeCommand(cmd);
+                }
                 super.removedService(reference, service);
             }
         };
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
index 2d5f7d0..9d48c1a 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
@@ -20,6 +20,7 @@
 
 import org.osgi.service.command.CommandSession;
 import org.osgi.service.command.Function;
+import org.osgi.framework.ServiceReference;
 
 import java.util.List;
 
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java
new file mode 100644
index 0000000..448be1a
--- /dev/null
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.gogo.runtime.shell;
+
+import java.util.List;
+
+import org.osgi.service.command.Function;
+import org.osgi.service.command.CommandSession;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.BundleContext;
+
+public class CommandProxy extends Reflective implements Function {
+
+    BundleContext context;
+    ServiceReference reference;
+    String function;
+
+    public CommandProxy(BundleContext context, ServiceReference reference, String function) {
+        this.context = context;
+        this.reference = reference;
+        this.function = function;
+    }
+
+    public Object execute(CommandSession session, List<Object> arguments) throws Exception {
+        Object target = context.getService(reference);
+        try {
+            if (target instanceof Function)
+            {
+                return ((Function) target).execute(session, arguments);
+            }
+            else
+            {
+                return method(session, target, function, arguments);
+            }
+        }
+        finally
+        {
+            context.ungetService(reference);
+        }
+    }
+}