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/pom.xml b/dependencymanager.shell/pom.xml
new file mode 100644
index 0000000..b4c154e
--- /dev/null
+++ b/dependencymanager.shell/pom.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix</artifactId>
+    <version>1.0.2</version>
+    <relativePath>../pom/pom.xml</relativePath>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>bundle</packaging>
+  <name>Apache Felix Dependency Manager Shell</name>
+  <version>1.1.0-SNAPSHOT</version>
+  <artifactId>org.apache.felix.dependencymanager.shell</artifactId>
+  <dependencies>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>1.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>1.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.shell</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <version>1.4.0</version>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Bundle-SymbolicName>org.apache.felix.dependencymanager.shell</Bundle-SymbolicName>
+            <Bundle-Activator>org.apache.felix.dependencymanager.shell.Activator</Bundle-Activator>
+            <Bundle-Name>Apache Felix Dependency Manager Shell</Bundle-Name>
+            <Bundle-Description>Shell commands for the dependency manager.</Bundle-Description>
+            <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
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]";
+    }
+
+}