You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2011/12/01 17:30:52 UTC

svn commit: r1209142 - in /karaf/trunk/shell/services: ./ src/main/java/org/apache/karaf/shell/services/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/karaf/ src/test/java/org/apache/karaf/shell/ src/tes...

Author: cschneider
Date: Thu Dec  1 16:30:51 2011
New Revision: 1209142

URL: http://svn.apache.org/viewvc?rev=1209142&view=rev
Log:
Refactored ListServices and added a test for it

Added:
    karaf/trunk/shell/services/src/test/
    karaf/trunk/shell/services/src/test/java/
    karaf/trunk/shell/services/src/test/java/org/
    karaf/trunk/shell/services/src/test/java/org/apache/
    karaf/trunk/shell/services/src/test/java/org/apache/karaf/
    karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/
    karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/services/
    karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/services/ListServicesTest.java   (with props)
Modified:
    karaf/trunk/shell/services/   (props changed)
    karaf/trunk/shell/services/pom.xml
    karaf/trunk/shell/services/src/main/java/org/apache/karaf/shell/services/ListServices.java

Propchange: karaf/trunk/shell/services/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Dec  1 16:30:51 2011
@@ -0,0 +1,6 @@
+target
+.settings
+
+.project
+
+.classpath

Modified: karaf/trunk/shell/services/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/services/pom.xml?rev=1209142&r1=1209141&r2=1209142&view=diff
==============================================================================
--- karaf/trunk/shell/services/pom.xml (original)
+++ karaf/trunk/shell/services/pom.xml Thu Dec  1 16:30:51 2011
@@ -63,6 +63,11 @@
             <artifactId>org.apache.karaf.util</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-jdk14</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

Modified: karaf/trunk/shell/services/src/main/java/org/apache/karaf/shell/services/ListServices.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/services/src/main/java/org/apache/karaf/shell/services/ListServices.java?rev=1209142&r1=1209141&r2=1209142&view=diff
==============================================================================
--- karaf/trunk/shell/services/src/main/java/org/apache/karaf/shell/services/ListServices.java (original)
+++ karaf/trunk/shell/services/src/main/java/org/apache/karaf/shell/services/ListServices.java Thu Dec  1 16:30:51 2011
@@ -18,82 +18,78 @@ package org.apache.karaf.shell.services;
 
 import java.util.List;
 
+import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
+import org.apache.karaf.shell.console.OsgiCommandSupport;
 import org.apache.felix.service.command.Function;
-import org.apache.karaf.shell.bundles.BundlesCommand;
+import org.apache.karaf.shell.bundles.BundleSelector;
 import org.apache.karaf.util.ShellUtil;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
 
 @Command(scope = "service", name = "list", description = "Lists OSGi services.")
-public class ListServices extends BundlesCommand {
+public class ListServices extends OsgiCommandSupport {
+    @Argument(index = 0, name = "ids", description = "The list of bundle (identified by IDs or name or name/version) separated by whitespaces", required = false, multiValued = true)
+    List<String> ids;
 
-    @Option(name = "-a", aliases = {}, description = "Shows all services", required = false, multiValued = false)
+    @Option(name = "-a", aliases = {}, description = "Shows all services. (By default Karaf commands are hidden)", required = false, multiValued = false)
     boolean showAll;
 
-    @Option(name = "-u", aliases = {}, description = "Shows services which are in use", required = false, multiValued = false)
+    @Option(name = "-u", aliases = {}, description = "Shows the services each bundle uses. (By default the provided services are shown)", required = false, multiValued = false)
     boolean inUse;
 
-    protected void doExecute(List<Bundle> bundles) throws Exception {
+    protected Object doExecute() throws Exception {
+        BundleSelector selector = new BundleSelector(getBundleContext(), session);
+        List<Bundle> bundles = selector.selectBundles(ids, true);
         if (bundles == null || bundles.isEmpty()) {
             Bundle[] allBundles = getBundleContext().getBundles();
-            for (int i = 0; i < allBundles.length; i++) {
-                printServicesShort(allBundles[i]);
-            }
+            printBundles(allBundles, false);
         } else {
-            for (Bundle bundle : bundles) {
-                printServices(bundle);
-            }
+            printBundles(bundles.toArray(new Bundle[]{}), true);
         }
+        return null;
     }
 
-    private void printServices(Bundle bundle) {
+    private void printBundles(Bundle[] bundles, boolean showProperties) {
+        for (Bundle bundle : bundles) {
+            ServiceReference<?>[] refs = (inUse) ? bundle.getServicesInUse() : bundle.getRegisteredServices();
+            printServices(bundle, refs, showProperties);
+        }
+    }
+    
+    private void printServices(Bundle bundle, ServiceReference<?>[] refs, boolean showProperties) {
         boolean headerPrinted = false;
         boolean needSeparator = false;
-        ServiceReference[] refs = null;
-
-        // Get registered or in-use services.
-        if (inUse) {
-            refs = bundle.getServicesInUse();
-        } else {
-            refs = bundle.getRegisteredServices();
+        
+        if (refs == null) {
+            return;
         }
 
-        // Print properties for each service.
-        for (int refIdx = 0; (refs != null) && (refIdx < refs.length); refIdx++) {
-            String[] objectClass = (String[]) refs[refIdx].getProperty("objectClass");
-
-            // Determine if we need to print the service, depending
-            // on whether it is a command service or not.
-            boolean print = true;
-            for (int ocIdx = 0; !showAll && (ocIdx < objectClass.length); ocIdx++) {
-                if (objectClass[ocIdx].equals(Function.class.getName())) {
-                    print = false;
-                }
-            }
+        for (ServiceReference<?> serviceRef : refs) {
+            String[] objectClass = (String[]) serviceRef.getProperty("objectClass");
+
+            boolean print = showAll || !isCommand(objectClass);
 
             // Print header if we have not already done so.
             if (!headerPrinted) {
                 headerPrinted = true;
-                String title = ShellUtil.getBundleName(bundle);
-                title = (inUse) ? title + " uses:" : title + " provides:";
                 System.out.println("");
+                String title = ShellUtil.getBundleName(bundle) + ((inUse) ? " uses:" : " provides:");
                 System.out.println(title);
                 System.out.println(ShellUtil.getUnderlineString(title));
             }
 
-            if (showAll || print) {
+            if (print) {
                 // Print service separator if necessary.
                 if (needSeparator) {
                     System.out.println("----");
                 }
 
-                // Print service properties.
-                String[] keys = refs[refIdx].getPropertyKeys();
-                for (int keyIdx = 0; (keys != null) && (keyIdx < keys.length); keyIdx++) {
-                    Object v = refs[refIdx].getProperty(keys[keyIdx]);
-                    System.out.println(keys[keyIdx] + " = " + ShellUtil.getValueString(v));
+                if (showProperties) {
+                    printProperties(serviceRef);
+                } else {
+                    System.out.println(ShellUtil.getValueString(objectClass));
                 }
 
                 needSeparator = true;
@@ -101,42 +97,19 @@ public class ListServices extends Bundle
         }
     }
 
-    private void printServicesShort(Bundle bundle) {
-        boolean headerPrinted = false;
-        ServiceReference[] refs = null;
-
-        // Get registered or in-use services.
-        if (inUse) {
-            refs = bundle.getServicesInUse();
-        } else {
-            refs = bundle.getRegisteredServices();
-        }
-
-        for (int refIdx = 0; (refs != null) && (refIdx < refs.length); refIdx++) {
-            String[] objectClass = (String[]) refs[refIdx].getProperty("objectClass");
-
-            // Determine if we need to print the service, depending
-            // on whether it is a command service or not.
-            boolean print = true;
-            for (int ocIdx = 0; !showAll && (ocIdx < objectClass.length); ocIdx++) {
-                if (objectClass[ocIdx].equals(Function.class.getName())) {
-                    print = false;
-                }
-            }
-
-            // Print the service if necessary.
-            if (showAll || print) {
-                if (!headerPrinted) {
-                    headerPrinted = true;
-                    String title = ShellUtil.getBundleName(bundle);
-                    title = (inUse) ? title + " uses:" : title + " provides:";
-                    System.out.println("\n" + title);
-                    System.out.println(ShellUtil.getUnderlineString(title));
-                }
-                System.out.println(ShellUtil.getValueString(objectClass));
+    private boolean isCommand(String[] objectClasses) {
+        for (String objectClass : objectClasses) {
+            if (objectClass.equals(Function.class.getName())) {
+                return true;
             }
         }
+        return false;
+    }
 
+    private void printProperties(ServiceReference<?> serviceRef) {
+        for (String key : serviceRef.getPropertyKeys()) {
+            System.out.println(key + " = " + ShellUtil.getValueString(serviceRef.getProperty(key)));
+        }
     }
 
 }

Added: karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/services/ListServicesTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/services/ListServicesTest.java?rev=1209142&view=auto
==============================================================================
--- karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/services/ListServicesTest.java (added)
+++ karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/services/ListServicesTest.java Thu Dec  1 16:30:51 2011
@@ -0,0 +1,85 @@
+package org.apache.karaf.shell.services;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+public class ListServicesTest {
+
+    public ListServicesTest() {
+        
+    }
+    
+    ServiceReference<?> createServiceRef(String ... objectClass) {
+        ServiceReference<?> serviceRef = createMock(ServiceReference.class);
+        expect(serviceRef.getProperty("objectClass")).andReturn(objectClass).atLeastOnce();
+        expect(serviceRef.getPropertyKeys()).andReturn(new String[]{"objectClass"});
+        replay(serviceRef);
+        return serviceRef;
+    }
+    
+    Bundle createBundle(long id, String name, ServiceReference<?>[] providedServices, ServiceReference<?>[] usedServices) {
+        Bundle bundle = createMock(Bundle.class);
+        expect(bundle.getRegisteredServices()).andReturn(providedServices);
+        expect(bundle.getServicesInUse()).andReturn(usedServices);
+        expect(bundle.getBundleId()).andReturn(id);
+        Dictionary<String, String> headers = new Hashtable<String, String>();
+        headers.put(Constants.BUNDLE_NAME, name);
+        expect(bundle.getHeaders()).andReturn(headers).atLeastOnce();
+        replay(bundle);
+        return bundle;
+    }
+    
+    private Bundle[] createBundles() {
+        Bundle bundle1 = createBundle(1, "ABundle", new ServiceReference<?>[]{createServiceRef("org.example.MyService")}, new ServiceReference<?>[]{});
+        Bundle bundle2 = createBundle(2, "AnotherBundle", new ServiceReference<?>[]{}, new ServiceReference<?>[]{createServiceRef("org.example.MyService")});
+        return new Bundle[] { bundle1, bundle2 };
+    }
+    
+    private BundleContext createBundleContext() {
+        BundleContext bundleContext = createMock(BundleContext.class);
+        Bundle[] bundles = createBundles();
+        expect(bundleContext.getBundles()).andReturn(bundles);
+        expect(bundleContext.getBundle(1)).andReturn(bundles[0]);
+        expect(bundleContext.getBundle(2)).andReturn(bundles[1]);
+        replay(bundleContext);
+        return bundleContext;
+    }
+
+    
+    @Test
+    public void listAllShort() throws Exception {
+        ListServices listServices = new ListServices();
+        listServices.setBundleContext(createBundleContext());
+        listServices.doExecute();
+    }
+
+    
+    @Test
+    public void listAllLong() throws Exception {
+        ListServices listServices = new ListServices();
+        listServices.ids = Arrays.asList(new String[]{"1", "2"});
+        listServices.setBundleContext(createBundleContext());
+        listServices.doExecute();
+    }
+    
+    @Test
+    public void listAllLongServiceUse() throws Exception {
+        ListServices listServices = new ListServices();
+        listServices.ids = Arrays.asList(new String[]{"1", "2"});
+        listServices.inUse = true;
+        listServices.setBundleContext(createBundleContext());
+        listServices.doExecute();
+    }
+
+
+}

Propchange: karaf/trunk/shell/services/src/test/java/org/apache/karaf/shell/services/ListServicesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain