You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2009/04/26 16:37:03 UTC

svn commit: r768713 - in /felix/trunk/shell/src/main/java/org/apache/felix/shell/impl: Activator.java FindCommandImpl.java PsCommandImpl.java

Author: rickhall
Date: Sun Apr 26 14:37:02 2009
New Revision: 768713

URL: http://svn.apache.org/viewvc?rev=768713&view=rev
Log:
Applied patch (FELIX-1058) to add a shell command to allow you to find
bundles by their name/symbolic name.

Added:
    felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java
Modified:
    felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/Activator.java
    felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java

Modified: felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/Activator.java?rev=768713&r1=768712&r2=768713&view=diff
==============================================================================
--- felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/Activator.java (original)
+++ felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/Activator.java Sun Apr 26 14:37:02 2009
@@ -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(),

Added: felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java?rev=768713&view=auto
==============================================================================
--- felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java (added)
+++ felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/FindCommandImpl.java Sun Apr 26 14:37:02 2009
@@ -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

Modified: felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java?rev=768713&r1=768712&r2=768713&view=diff
==============================================================================
--- felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java (original)
+++ felix/trunk/shell/src/main/java/org/apache/felix/shell/impl/PsCommandImpl.java Sun Apr 26 14:37:02 2009
@@ -30,7 +30,7 @@
 
 public class PsCommandImpl implements Command
 {
-    private BundleContext m_context = null;
+    protected final BundleContext m_context;
 
     public PsCommandImpl(BundleContext context)
     {
@@ -100,88 +100,95 @@
         Bundle[] bundles = m_context.getBundles();
         if (bundles != null)
         {
-            // Display active start level.
-            if (sl != null)
-            {
-                out.println("START LEVEL " + sl.getStartLevel());
-            }
+            printBundleList(bundles, sl, out, showLoc, showSymbolic, showUpdate);
+        }
+        else
+        {
+            out.println("There are no installed bundles.");
+        }
+    }
+
+    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";
+        // 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)
             {
-               msg = " Location";
+                name = bundles[i].getLocation();
             }
             else if (showSymbolic)
             {
-               msg = " Symbolic name";
+                name = bundles[i].getSymbolicName();
+                name = (name == null)
+                    ? "<no symbolic name>" : name;
             }
             else if (showUpdate)
             {
-               msg = " Update location";
+                name = (String)
+                    bundles[i].getHeaders().get(Constants.BUNDLE_UPDATELOCATION);
+                name = (name == null)
+                    ? bundles[i].getLocation() : name;
             }
-            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);
+            // 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
-        {
-            out.println("There are no installed bundles.");
+            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);
         }
     }
 
@@ -199,4 +206,4 @@
             return "Stopping   ";
         return "Unknown    ";
     }
-}
+}
\ No newline at end of file