Applied patch (FELIX-1058) to add a shell command to allow you to find
bundles by their name/symbolic name.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@768713 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/shell/src/main/java/org/apache/felix/shell/impl/Activator.java b/shell/src/main/java/org/apache/felix/shell/impl/Activator.java
index eb83ff6..8940ad6 100644
--- a/shell/src/main/java/org/apache/felix/shell/impl/Activator.java
+++ b/shell/src/main/java/org/apache/felix/shell/impl/Activator.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -97,6 +97,11 @@
             org.apache.felix.shell.Command.class.getName(),
             new ExportsCommandImpl(m_context), null);
 
+        // Register "find" command service.
+        context.registerService(
+            org.apache.felix.shell.Command.class.getName(),
+            new FindCommandImpl(m_context), null);
+
         // Register "headers" command service.
         context.registerService(
             org.apache.felix.shell.Command.class.getName(),
diff --git a/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java b/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java
new file mode 100644
index 0000000..99866a3
--- /dev/null
+++ b/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java
@@ -0,0 +1,99 @@
+/*
+ * 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.shell.impl;
+
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+
+/**
+ * Shell command to display a list of bundles whose
+ * Bundle-Name or Bundle-Symbolic-Name contains
+ * a specified string
+ *
+ */
+public class FindCommandImpl extends PsCommandImpl
+{
+    public FindCommandImpl(BundleContext context)
+    {
+        super(context);
+    }
+
+    public void execute(String line, PrintStream out, PrintStream err)
+    {
+        StringTokenizer st = new StringTokenizer(line);
+        if (st.countTokens() < 2)
+        {
+            out.println("Please specify a bundle name");
+            return;
+        }
+
+        st.nextToken();
+        String pattern = st.nextToken();
+
+        Bundle[] bundles = m_context.getBundles();
+
+        List found = new ArrayList();
+
+        for (int i = 0; i < bundles.length; i++)
+        {
+            Bundle bundle = bundles[i];
+            String name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME);
+            if (match(bundle.getSymbolicName(), pattern) || match(name, pattern))
+            {
+                found.add(bundle);
+            }
+        }
+
+        if (found.size() > 0)
+        {
+            printBundleList((Bundle[]) found.toArray(new Bundle[found.size()]), null, out, false, false, false);
+        }
+        else
+        {
+            out.println("No matching bundles found");
+        }
+
+    }
+
+    private boolean match(String name, String pattern)
+    {
+        return name != null && name.toLowerCase().contains(pattern.toLowerCase());
+    }
+
+    public String getName()
+    {
+        return "find";
+    }
+
+    public String getShortDescription()
+    {
+        return "find bundles by name.";
+    }
+
+    public String getUsage()
+    {
+        return "find <bundle name>";
+    }
+}
\ No newline at end of file
diff --git a/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java b/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java
index 8490b19..a9e78ec 100644
--- a/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java
+++ b/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java
@@ -30,7 +30,7 @@
 
 public class PsCommandImpl implements Command
 {
-    private BundleContext m_context = null;
+    protected final BundleContext m_context;
 
     public PsCommandImpl(BundleContext context)
     {
@@ -100,84 +100,7 @@
         Bundle[] bundles = m_context.getBundles();
         if (bundles != null)
         {
-            // Display active start level.
-            if (sl != null)
-            {
-                out.println("START LEVEL " + sl.getStartLevel());
-            }
-
-            // Print column headers.
-            String msg = " Name";
-            if (showLoc)
-            {
-               msg = " Location";
-            }
-            else if (showSymbolic)
-            {
-               msg = " Symbolic name";
-            }
-            else if (showUpdate)
-            {
-               msg = " Update location";
-            }
-            String level = (sl == null) ? "" : "  Level ";
-            out.println("   ID " + "  State       " + level + msg);
-            for (int i = 0; i < bundles.length; i++)
-            {
-                // Get the bundle name or location.
-                String name = (String)
-                    bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
-                // If there is no name, then default to symbolic name.
-                name = (name == null) ? bundles[i].getSymbolicName() : name;
-                // If there is no symbolic name, resort to location.
-                name = (name == null) ? bundles[i].getLocation() : name;
-
-                // Overwrite the default value is the user specifically
-                // requested to display one or the other.
-                if (showLoc)
-                {
-                    name = bundles[i].getLocation();
-                }
-                else if (showSymbolic)
-                {
-                    name = bundles[i].getSymbolicName();
-                    name = (name == null)
-                        ? "<no symbolic name>" : name;
-                }
-                else if (showUpdate)
-                {
-                    name = (String)
-                        bundles[i].getHeaders().get(Constants.BUNDLE_UPDATELOCATION);
-                    name = (name == null)
-                        ? bundles[i].getLocation() : name;
-                }
-                // Show bundle version if not showing location.
-                String version = (String)
-                    bundles[i].getHeaders().get(Constants.BUNDLE_VERSION);
-                name = (!showLoc && !showUpdate && (version != null))
-                    ? name + " (" + version + ")" : name;
-                long l = bundles[i].getBundleId();
-                String id = String.valueOf(l);
-                if (sl == null)
-                {
-                    level = "1";
-                }
-                else
-                {
-                    level = String.valueOf(sl.getBundleStartLevel(bundles[i]));
-                }
-                while (level.length() < 5)
-                {
-                    level = " " + level;
-                }
-                while (id.length() < 4)
-                {
-                    id = " " + id;
-                }
-                out.println("[" + id + "] ["
-                    + getStateString(bundles[i].getState())
-                    + "] [" + level + "] " + name);
-            }
+            printBundleList(bundles, sl, out, showLoc, showSymbolic, showUpdate);
         }
         else
         {
@@ -185,6 +108,90 @@
         }
     }
 
+    protected void printBundleList(
+        Bundle[] bundles, StartLevel startLevel, PrintStream out, boolean showLoc,
+        boolean showSymbolic, boolean showUpdate)
+    {
+        // Display active start level.
+        if (startLevel != null)
+        {
+            out.println("START LEVEL " + startLevel.getStartLevel());
+        }
+
+        // Print column headers.
+        String msg = " Name";
+        if (showLoc)
+        {
+           msg = " Location";
+        }
+        else if (showSymbolic)
+        {
+           msg = " Symbolic name";
+        }
+        else if (showUpdate)
+        {
+           msg = " Update location";
+        }
+        String level = (startLevel == null) ? "" : "  Level ";
+        out.println("   ID " + "  State       " + level + msg);
+        for (int i = 0; i < bundles.length; i++)
+        {
+            // Get the bundle name or location.
+            String name = (String)
+                bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
+            // If there is no name, then default to symbolic name.
+            name = (name == null) ? bundles[i].getSymbolicName() : name;
+            // If there is no symbolic name, resort to location.
+            name = (name == null) ? bundles[i].getLocation() : name;
+
+            // Overwrite the default value is the user specifically
+            // requested to display one or the other.
+            if (showLoc)
+            {
+                name = bundles[i].getLocation();
+            }
+            else if (showSymbolic)
+            {
+                name = bundles[i].getSymbolicName();
+                name = (name == null)
+                    ? "<no symbolic name>" : name;
+            }
+            else if (showUpdate)
+            {
+                name = (String)
+                    bundles[i].getHeaders().get(Constants.BUNDLE_UPDATELOCATION);
+                name = (name == null)
+                    ? bundles[i].getLocation() : name;
+            }
+            // Show bundle version if not showing location.
+            String version = (String)
+                bundles[i].getHeaders().get(Constants.BUNDLE_VERSION);
+            name = (!showLoc && !showUpdate && (version != null))
+                ? name + " (" + version + ")" : name;
+            long l = bundles[i].getBundleId();
+            String id = String.valueOf(l);
+            if (startLevel == null)
+            {
+                level = "1";
+            }
+            else
+            {
+                level = String.valueOf(startLevel.getBundleStartLevel(bundles[i]));
+            }
+            while (level.length() < 5)
+            {
+                level = " " + level;
+            }
+            while (id.length() < 4)
+            {
+                id = " " + id;
+            }
+            out.println("[" + id + "] ["
+                + getStateString(bundles[i].getState())
+                + "] [" + level + "] " + name);
+        }
+    }
+
     public String getStateString(int i)
     {
         if (i == Bundle.ACTIVE)
@@ -199,4 +206,4 @@
             return "Stopping   ";
         return "Unknown    ";
     }
-}
+}
\ No newline at end of file