You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2009/07/10 19:21:31 UTC

svn commit: r793045 - in /felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime: Activator.java shell/Command.java shell/CommandProxy.java

Author: gnodet
Date: Fri Jul 10 17:21:31 2009
New Revision: 793045

URL: http://svn.apache.org/viewvc?rev=793045&view=rev
Log:
Allow retrieving the command lazily

Added:
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java
      - copied, changed from r793044, felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
Modified:
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java?rev=793045&r1=793044&r2=793045&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java Fri Jul 10 17:21:31 2009
@@ -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);
             }
         };

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java?rev=793045&r1=793044&r2=793045&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java Fri Jul 10 17:21:31 2009
@@ -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;
 

Copied: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java (from r793044, felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java)
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java?p2=felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java&p1=felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java&r1=793044&r2=793045&rev=793045&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/Command.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProxy.java Fri Jul 10 17:21:31 2009
@@ -18,25 +18,40 @@
  */
 package org.apache.felix.gogo.runtime.shell;
 
-import org.osgi.service.command.CommandSession;
+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;
 
-import java.util.List;
+public class CommandProxy extends Reflective implements Function {
 
-public class Command extends Reflective implements Function
-{
-    Object target;
+    BundleContext context;
+    ServiceReference reference;
     String function;
 
-    public Command(Object target, String function)
-    {
+    public CommandProxy(BundleContext context, ServiceReference reference, String function) {
+        this.context = context;
+        this.reference = reference;
         this.function = function;
-        this.target = target;
     }
 
-    public Object execute(CommandSession session, List<Object> arguments) throws Exception
-    {
-        return method(session, target, function, arguments);
+    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);
+        }
     }
-
 }