You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/01/04 21:16:08 UTC

svn commit: r608980 - /servicemix/smx4/runtime/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/support/OsgiCommandSupport.java

Author: gnodet
Date: Fri Jan  4 12:16:07 2008
New Revision: 608980

URL: http://svn.apache.org/viewvc?rev=608980&view=rev
Log:
Add ServiceReference handling in OsgiCommandSupport

Modified:
    servicemix/smx4/runtime/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/support/OsgiCommandSupport.java

Modified: servicemix/smx4/runtime/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/support/OsgiCommandSupport.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/runtime/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/support/OsgiCommandSupport.java?rev=608980&r1=608979&r2=608980&view=diff
==============================================================================
--- servicemix/smx4/runtime/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/support/OsgiCommandSupport.java (original)
+++ servicemix/smx4/runtime/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/support/OsgiCommandSupport.java Fri Jan  4 12:16:07 2008
@@ -16,6 +16,9 @@
  */
 package org.apache.geronimo.gshell.support;
 
+import java.util.List;
+import java.util.ArrayList;
+
 import org.apache.geronimo.gshell.clp.CommandLineProcessor;
 import org.apache.geronimo.gshell.clp.Option;
 import org.apache.geronimo.gshell.clp.Printer;
@@ -26,6 +29,7 @@
 import org.apache.geronimo.gshell.command.annotation.CommandComponent;
 import org.apache.geronimo.gshell.common.Arguments;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.osgi.context.BundleContextAware;
@@ -49,6 +53,8 @@
 
     protected Variables variables;
 
+    protected List<ServiceReference> usedReferences;
+
     @Option(name="-h", aliases={"--help"}, description="Display this help message", requireOverride = true)
     private boolean displayHelp;
 
@@ -123,7 +129,11 @@
         assert io != null;
         assert variables != null;
 
-        return doExecute();
+        try {
+            return doExecute();
+        } finally {
+            ungetServices();
+        }
     }
 
     protected abstract Object doExecute() throws Exception;
@@ -142,6 +152,38 @@
         Printer printer = new Printer(clp);
         printer.printUsage(io.out);
         io.out.println();
+    }
+
+    protected <T> List<T> getAllServices(Class<T> clazz, String filter) throws Exception {
+        ServiceReference[] references = getBundleContext().getAllServiceReferences(clazz.getName(), filter);
+        if (references == null) {
+            return null;
+        }
+        List<T> services = new ArrayList<T>();
+        for (ServiceReference ref : references) {
+            T t = getService(clazz, ref);
+            services.add(t);
+        }
+        return services;
+    }
+
+    protected <T> T getService(Class<T> clazz, ServiceReference reference) {
+        T t = (T) getBundleContext().getService(reference);
+        if (t != null) {
+            if (usedReferences == null) {
+                usedReferences = new ArrayList<ServiceReference>();
+            }
+            usedReferences.add(reference);
+        }
+        return t;
+    }
+
+    protected void ungetServices() {
+        if (usedReferences != null) {
+            for (ServiceReference ref : usedReferences) {
+                getBundleContext().ungetService(ref);
+            }
+        }
     }
 
 }