Initial version of Felix shell command for querying the status of services managed by the dependency manager.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@696809 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager.shell/src/main/java/org/apache/felix/dependencymanager/shell/Activator.java b/dependencymanager.shell/src/main/java/org/apache/felix/dependencymanager/shell/Activator.java
new file mode 100644
index 0000000..e519c65
--- /dev/null
+++ b/dependencymanager.shell/src/main/java/org/apache/felix/dependencymanager/shell/Activator.java
@@ -0,0 +1,18 @@
+package org.apache.felix.dependencymanager.shell;
+
+import org.apache.felix.shell.Command;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class Activator implements BundleActivator {
+    private ServiceRegistration m_serviceRegistration;
+
+    public void start(BundleContext context) throws Exception {
+        m_serviceRegistration = context.registerService(Command.class.getName(), new DMCommand(context), null);
+    }
+
+    public void stop(BundleContext context) throws Exception {
+        m_serviceRegistration.unregister();
+    }
+}
diff --git a/dependencymanager.shell/src/main/java/org/apache/felix/dependencymanager/shell/DMCommand.java b/dependencymanager.shell/src/main/java/org/apache/felix/dependencymanager/shell/DMCommand.java
new file mode 100644
index 0000000..35dcb5d
--- /dev/null
+++ b/dependencymanager.shell/src/main/java/org/apache/felix/dependencymanager/shell/DMCommand.java
@@ -0,0 +1,74 @@
+package org.apache.felix.dependencymanager.shell;
+
+import java.io.PrintStream;
+import java.util.Arrays;
+import java.util.Comparator;
+
+import org.apache.felix.dependencymanager.ServiceComponent;
+import org.apache.felix.dependencymanager.ServiceComponentDependency;
+import org.apache.felix.shell.Command;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+public class DMCommand implements Command {
+    private final BundleContext m_context;
+
+    public DMCommand(BundleContext context) {
+        m_context = context;
+    }
+
+    public void execute(String line, PrintStream out, PrintStream err) {
+        // lookup all dependency manager service components
+        try {
+            ServiceReference[] references = m_context.getServiceReferences(ServiceComponent.class.getName(), null);
+            // show their state
+            if (references != null) {
+                Arrays.sort(references, new Comparator() {
+                    public int compare(Object o1, Object o2) {
+                        ServiceReference r1 = (ServiceReference) o1;
+                        ServiceReference r2 = (ServiceReference) o2;
+                        return (int) Math.signum(r1.getBundle().getBundleId() - r2.getBundle().getBundleId());
+                    }});
+                for (int i = 0; i < references.length; i++) {
+                    ServiceReference ref = references[i];
+                    ServiceComponent sc = (ServiceComponent) m_context.getService(ref);
+                    if (sc != null) {
+                        String name = sc.getName();
+                        int state = sc.getState();
+                        out.println("[" + ref.getBundle().getBundleId() + "] " + ref.getBundle().getSymbolicName() + " " + name + " " + ServiceComponent.STATE_NAMES[state]);
+                        if (line.indexOf("deps") != -1) {
+                            ServiceComponentDependency[] dependencies = sc.getComponentDependencies();
+                            if (dependencies != null) {
+                                for (int j = 0; j < dependencies.length; j++) {
+                                    ServiceComponentDependency dep = dependencies[j];
+                                    String depName = dep.getName();
+                                    String depType = dep.getType();
+                                    int depState = dep.getState();
+                                    out.println(" " + depName + " " + depType + " " + ServiceComponentDependency.STATE_NAMES[depState]);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        catch (InvalidSyntaxException e) {
+            // very weird since I'm not specifying a filter
+            e.printStackTrace(err);
+        }
+    }
+
+    public String getName() {
+        return "dm";
+    }
+
+    public String getShortDescription() {
+        return "list dependency manager component diagnostics.";
+    }
+
+    public String getUsage() {
+        return "dm [deps]";
+    }
+
+}