Initial source commit.


git-svn-id: https://svn.apache.org/repos/asf/incubator/oscar/trunk@233031 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/org/apache/osgi/bundle/shell/PackagesCommandImpl.java b/src/org/apache/osgi/bundle/shell/PackagesCommandImpl.java
new file mode 100644
index 0000000..2722cdd
--- /dev/null
+++ b/src/org/apache/osgi/bundle/shell/PackagesCommandImpl.java
@@ -0,0 +1,121 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.osgi.bundle.shell;
+
+import java.io.PrintStream;
+import java.util.StringTokenizer;
+
+import org.apache.osgi.service.shell.Command;
+import org.osgi.framework.*;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+public class PackagesCommandImpl implements Command
+{
+    private BundleContext m_context = null;
+
+    public PackagesCommandImpl(BundleContext context)
+    {
+        m_context = context;
+    }
+
+    public String getName()
+    {
+        return "packages";
+    }
+
+    public String getUsage()
+    {
+        return "packages [<id> ...]";
+    }
+
+    public String getShortDescription()
+    {
+        return "list exported packages.";
+    }
+
+    public void execute(String s, PrintStream out, PrintStream err)
+    {
+        // Get package admin service.
+        ServiceReference ref = m_context.getServiceReference(
+            org.osgi.service.packageadmin.PackageAdmin.class.getName());
+        if (ref == null)
+        {
+            out.println("PackageAdmin service is unavailable.");
+            return;
+        }
+
+        PackageAdmin pa = (PackageAdmin) m_context.getService(ref);
+        if (pa == null)
+        {
+            out.println("PackageAdmin service is unavailable.");
+            return;
+        }
+
+        // Parse command line.
+        StringTokenizer st = new StringTokenizer(s, " ");
+
+        // Ignore the command name.
+        st.nextToken();
+
+        if (st.hasMoreTokens())
+        {
+            while (st.hasMoreTokens())
+            {
+                String id = st.nextToken();
+                try
+                {
+                    long l = Long.parseLong(id);
+                    Bundle bundle = m_context.getBundle(l);
+                    ExportedPackage[] exports = pa.getExportedPackages(bundle);
+                    printExports(out, bundle, exports);
+                }
+                catch (NumberFormatException ex)
+                {
+                    err.println("Unable to parse id '" + id + "'.");
+                }
+                catch (Exception ex)
+                {
+                    err.println(ex.toString());
+                }
+            }
+        }
+        else
+        {
+            ExportedPackage[] exports = pa.getExportedPackages((Bundle) null);
+            printExports(out, null, exports);
+        }
+    }
+
+    private void printExports(PrintStream out, Bundle target, ExportedPackage[] exports)
+    {
+        if ((exports != null) && (exports.length > 0))
+        {
+            for (int i = 0; i < exports.length; i++)
+            {
+                Bundle bundle = exports[i].getExportingBundle();
+                out.print(Util.getBundleName(bundle));
+                out.println(": " + exports[i]);
+            }
+        }
+        else
+        {
+            out.println(Util.getBundleName(target)
+                + ": No active exported packages.");
+        }
+    }
+}
\ No newline at end of file