You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ma...@apache.org on 2011/04/26 12:48:26 UTC

svn commit: r1096720 - in /felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell: DMCommand.java EquinoxDMCommand.java FelixDMCommand.java GogoDMCommand.java

Author: marrs
Date: Tue Apr 26 10:48:25 2011
New Revision: 1096720

URL: http://svn.apache.org/viewvc?rev=1096720&view=rev
Log:
Fixed missing license headers.
Added a 'stats' option to the 'dm' command that shows the total amount of components and dependencies.
Implemented the shell without having to rely on ComponentDeclaration services.

Modified:
    felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/DMCommand.java
    felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/EquinoxDMCommand.java
    felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/FelixDMCommand.java
    felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/GogoDMCommand.java

Modified: felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/DMCommand.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/DMCommand.java?rev=1096720&r1=1096719&r2=1096720&view=diff
==============================================================================
--- felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/DMCommand.java (original)
+++ felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/DMCommand.java Tue Apr 26 10:48:25 2011
@@ -20,16 +20,18 @@ package org.apache.felix.dm.shell;
 
 import java.io.PrintStream;
 import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collections;
 import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
 import java.util.StringTokenizer;
 
+import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentDeclaration;
 import org.apache.felix.dm.ComponentDependencyDeclaration;
+import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
 
 /**
  * Shell command for showing all services and dependencies that are managed
@@ -38,8 +40,8 @@ import org.osgi.framework.ServiceReferen
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
 public class DMCommand {
-    private static final BundleIdSorter SORTER = new BundleIdSorter();
     private final BundleContext m_context;
+    private static final DependencyManagerSorter SORTER = new DependencyManagerSorter();
 
     public DMCommand(BundleContext context) {
         m_context = context;
@@ -49,6 +51,7 @@ public class DMCommand {
         boolean nodeps = false;
         boolean notavail = false;
         boolean compact = false;
+        boolean stats = false;
         ArrayList ids = new ArrayList();
 
         // parse the command line
@@ -66,6 +69,9 @@ public class DMCommand {
                 else if ("compact".equals(token)) {
                     compact = true;
                 }
+                else if ("stats".equals(token)) {
+                    stats = true;
+                }
                 else {
                     try {
                         ids.add(Long.valueOf(token));
@@ -77,77 +83,84 @@ public class DMCommand {
                     }
                 }
             }
+            
             // lookup all dependency manager service components
-            try {
-                ServiceReference[] references = m_context.getServiceReferences(ComponentDeclaration.class.getName(), null);
-                // show their state
-                if (references != null) {
-                    Arrays.sort(references, SORTER);
-                    long lastBundleId = -1;
-                    for (int i = 0; i < references.length; i++) {
-                        ServiceReference ref = references[i];
-                        ComponentDeclaration sc = (ComponentDeclaration) m_context.getService(ref);
-                        if (sc != null) {
-                            String name = sc.getName();
-                            int state = sc.getState();
-                            Bundle bundle = ref.getBundle();
-                            long bundleId = bundle.getBundleId();
-                            if (ids.size() == 0 || ids.contains(Long.valueOf(bundleId))) {
-                                if (!notavail || (notavail && sc.getState() == ComponentDeclaration.STATE_UNREGISTERED)) {
-                                    if (lastBundleId != bundleId) {
-                                        lastBundleId = bundleId;
-                                        if (compact) {
-                                            out.println("[" + bundleId + "] " + compactName(bundle.getSymbolicName()));
-                                        }
-                                        else {
-                                            out.println("[" + bundleId + "] " + bundle.getSymbolicName());
-                                        }
-                                    }
+            List managers = DependencyManager.getDependencyManagers();
+            Collections.sort(managers, SORTER);
+            Iterator iterator = managers.iterator();
+            long numberOfComponents = 0;
+            long numberOfDependencies = 0;
+            long lastBundleId = -1;
+            while (iterator.hasNext()) {
+                DependencyManager manager = (DependencyManager) iterator.next();
+                List components = manager.getComponents();
+                Iterator componentIterator = components.iterator();
+                while (componentIterator.hasNext()) {
+                    Component component = (Component) componentIterator.next();
+                    ComponentDeclaration sc = (ComponentDeclaration) component;
+                    String name = sc.getName();
+                    int state = sc.getState();
+                    Bundle bundle = sc.getBundleContext().getBundle();
+                    long bundleId = bundle.getBundleId();
+                    if (ids.size() == 0 || ids.contains(Long.valueOf(bundleId))) {
+                        if (!notavail || (notavail && sc.getState() == ComponentDeclaration.STATE_UNREGISTERED)) {
+                            numberOfComponents++;
+                            if (lastBundleId != bundleId) {
+                                lastBundleId = bundleId;
+                                if (compact) {
+                                    out.println("[" + bundleId + "] " + compactName(bundle.getSymbolicName()));
+                                }
+                                else {
+                                    out.println("[" + bundleId + "] " + bundle.getSymbolicName());
+                                }
+                            }
+                            if (compact) {
+                                out.print(" " + compactName(name) + " " + compactState(ComponentDeclaration.STATE_NAMES[state]));
+                            }
+                            else {
+                                out.println("  " + name + " " + ComponentDeclaration.STATE_NAMES[state]);
+                            }
+                            if (!nodeps) {
+                                ComponentDependencyDeclaration[] dependencies = sc.getComponentDependencies();
+                                numberOfDependencies += dependencies.length;
+                                if (dependencies != null && dependencies.length > 0) {
                                     if (compact) {
-                                        out.print(" " + compactName(name) + " " + compactState(ComponentDeclaration.STATE_NAMES[state]));
-                                    }
-                                    else {
-                                        out.println("  " + name + " " + ComponentDeclaration.STATE_NAMES[state]);
+                                        out.print('(');
                                     }
-                                    if (!nodeps) {
-                                        ComponentDependencyDeclaration[] dependencies = sc.getComponentDependencies();
-                                        if (dependencies != null && dependencies.length > 0) {
-                                            if (compact) {
-                                                out.print('(');
-                                            }
-                                            for (int j = 0; j < dependencies.length; j++) {
-                                                ComponentDependencyDeclaration dep = dependencies[j];
-                                                String depName = dep.getName();
-                                                String depType = dep.getType();
-                                                int depState = dep.getState();
-                                                if (compact) {
-                                                    if (j > 0) {
-                                                        out.print(' ');
-                                                    }
-                                                    out.print(compactName(depName) + " " + compactState(depType) + " " + compactState(ComponentDependencyDeclaration.STATE_NAMES[depState]));
-                                                }
-                                                else {
-                                                    out.println("    " + depName + " " + depType + " " + ComponentDependencyDeclaration.STATE_NAMES[depState]);
-                                                }
-                                            }
-                                            if (compact) {
-                                                out.print(')');
+                                    for (int j = 0; j < dependencies.length; j++) {
+                                        ComponentDependencyDeclaration dep = dependencies[j];
+                                        String depName = dep.getName();
+                                        String depType = dep.getType();
+                                        int depState = dep.getState();
+                                        if (compact) {
+                                            if (j > 0) {
+                                                out.print(' ');
                                             }
+                                            out.print(compactName(depName) + " " + compactState(depType) + " " + compactState(ComponentDependencyDeclaration.STATE_NAMES[depState]));
+                                        }
+                                        else {
+                                            out.println("    " + depName + " " + depType + " " + ComponentDependencyDeclaration.STATE_NAMES[depState]);
                                         }
                                     }
                                     if (compact) {
-                                        out.println();
+                                        out.print(')');
                                     }
                                 }
                             }
-                            m_context.ungetService(ref);
+                            if (compact) {
+                                out.println();
+                            }
                         }
                     }
                 }
             }
-            catch (InvalidSyntaxException e) {
-                // very weird since I'm not specifying a filter
-                e.printStackTrace(err);
+            if (stats) {
+                out.println("Statistics:");
+                out.println(" - Dependency managers: " + managers.size());
+                out.println(" - Components: " + numberOfComponents);
+                if (!nodeps) {
+                    out.println(" - Dependencies: " + numberOfDependencies);
+                }
             }
         }
         else {
@@ -210,16 +223,16 @@ public class DMCommand {
     }
 
     public String getUsage() {
-        return "dm [nodeps] [notavail] [compact] [<bundleid> ...]";
+        return "dm [nodeps] [notavail] [compact] [stats] [<bundleid> ...]";
     }
-}
-
-final class BundleIdSorter implements Comparator {
-    public int compare(Object o1, Object o2) {
-        ServiceReference r1 = (ServiceReference) o1;
-        ServiceReference r2 = (ServiceReference) o2;
-        long id1 = r1.getBundle().getBundleId();
-        long id2 = r2.getBundle().getBundleId();
-        return id1 > id2 ? 1 : -1;
+    
+    public static class DependencyManagerSorter implements Comparator {
+        public int compare(Object o1, Object o2) {
+            DependencyManager dm1 = (DependencyManager) o1;
+            DependencyManager dm2 = (DependencyManager) o2;
+            long id1 = dm1.getBundleContext().getBundle().getBundleId();
+            long id2 = dm2.getBundleContext().getBundle().getBundleId();
+            return id1 > id2 ? 1 : -1;
+        }
     }
 }

Modified: felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/EquinoxDMCommand.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/EquinoxDMCommand.java?rev=1096720&r1=1096719&r2=1096720&view=diff
==============================================================================
--- felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/EquinoxDMCommand.java (original)
+++ felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/EquinoxDMCommand.java Tue Apr 26 10:48:25 2011
@@ -1,3 +1,21 @@
+/*
+ * 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.
+ */
 package org.apache.felix.dm.shell;
 
 import java.io.ByteArrayOutputStream;
@@ -7,6 +25,9 @@ import org.eclipse.osgi.framework.consol
 import org.eclipse.osgi.framework.console.CommandProvider;
 import org.osgi.framework.BundleContext;
 
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
 public class EquinoxDMCommand extends DMCommand implements CommandProvider {
     public EquinoxDMCommand(BundleContext context) {
         super(context);

Modified: felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/FelixDMCommand.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/FelixDMCommand.java?rev=1096720&r1=1096719&r2=1096720&view=diff
==============================================================================
--- felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/FelixDMCommand.java (original)
+++ felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/FelixDMCommand.java Tue Apr 26 10:48:25 2011
@@ -1,8 +1,29 @@
+/*
+ * 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.
+ */
 package org.apache.felix.dm.shell;
 
 import org.apache.felix.shell.Command;
 import org.osgi.framework.BundleContext;
 
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
 public class FelixDMCommand extends DMCommand implements Command {
     public FelixDMCommand(BundleContext context) {
         super(context);

Modified: felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/GogoDMCommand.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/GogoDMCommand.java?rev=1096720&r1=1096719&r2=1096720&view=diff
==============================================================================
--- felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/GogoDMCommand.java (original)
+++ felix/trunk/dependencymanager/shell/src/main/java/org/apache/felix/dm/shell/GogoDMCommand.java Tue Apr 26 10:48:25 2011
@@ -1,46 +1,61 @@
+/*
+ * 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.
+ */
 package org.apache.felix.dm.shell;
 
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.PrintStream;
 
 import org.osgi.framework.BundleContext;
 
 /**
  * This class provides DependencyManager commands for the Gogo shell.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
-public class GogoDMCommand extends DMCommand
-{
-    public GogoDMCommand(BundleContext context)
-    {
+public class GogoDMCommand extends DMCommand {
+    public GogoDMCommand(BundleContext context) {
         super(context);
     }
-    
+
     public void dmhelp() {
-        System.out.println("list Dependency Manager component diagnostics. Usage: dm [nodeps] [notavail] [compact] [<bundleid> ...]");
+        System.out.println(super.getUsage() + " - " + super.getShortDescription());
     }
     
     public void dm(String[] args) {
         execute("dm", args);
     }
         
-   private void execute(String line, String[] args) {
-       ByteArrayOutputStream bytes = new ByteArrayOutputStream();
-       ByteArrayOutputStream errorBytes = new ByteArrayOutputStream();
-       PrintStream out = new PrintStream(bytes);
-       PrintStream err = new PrintStream(errorBytes);
-        
-       for (int i = 0; i < args.length; i ++) {
-           line += " " + args[i];
-       }
-        
-       super.execute(line.toString(), out, err);
-       if (bytes.size() > 0) {
-           System.out.println(new String(bytes.toByteArray()));
-       }
-       if (errorBytes.size() > 0) {
-           System.out.print("Error:\n");
-           System.out.println(new String(errorBytes.toByteArray()));
-       }
+    private void execute(String line, String[] args) {
+        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+        ByteArrayOutputStream errorBytes = new ByteArrayOutputStream();
+        PrintStream out = new PrintStream(bytes);
+        PrintStream err = new PrintStream(errorBytes);
+        for (int i = 0; i < args.length; i++) {
+            line += " " + args[i];
+        }
+        super.execute(line.toString(), out, err);
+        if (bytes.size() > 0) {
+            System.out.println(new String(bytes.toByteArray()));
+        }
+        if (errorBytes.size() > 0) {
+            System.out.print("Error:\n");
+            System.out.println(new String(errorBytes.toByteArray()));
+        }
     }
 }