[karaf] rename gshell to shell and rename submodules to avoid having the parent module name in their names

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@816769 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/packages/pom.xml b/karaf/shell/packages/pom.xml
new file mode 100644
index 0000000..4f5b613
--- /dev/null
+++ b/karaf/shell/packages/pom.xml
@@ -0,0 +1,81 @@
+<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">
+
+    <!--
+
+        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.
+    -->
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.felix.karaf.shell</groupId>
+        <artifactId>shell</artifactId>
+        <version>0.9.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>org.apache.felix.karaf.shell</groupId>
+    <artifactId>org.apache.felix.karaf.shell.packages</artifactId>
+    <packaging>bundle</packaging>
+    <version>0.9.0-SNAPSHOT</version>
+    <name>Apache Felix Karaf :: Shell PackageAdmin Commands</name>
+
+    <description>
+        Provides the PackageAdmin Shell commands
+    </description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix.karaf.shell</groupId>
+            <artifactId>org.apache.felix.karaf.shell.console</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <configuration>
+                    <instructions>
+                        <Bundle-SymbolicName>${artifactId}</Bundle-SymbolicName>
+                        <Export-Package>${pom.artifactId}*;version=${project.version}</Export-Package>
+                        <Import-Package>
+                            org.osgi.service.command,
+                            org.apache.felix.gogo.commands,
+                            org.apache.felix.karaf.shell.console,
+                            *
+                        </Import-Package>
+                        <Private-Package>!*</Private-Package>
+                        <_versionpolicy>${bnd.version.policy}</_versionpolicy>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/ExportsCommand.java b/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/ExportsCommand.java
new file mode 100644
index 0000000..96ae308
--- /dev/null
+++ b/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/ExportsCommand.java
@@ -0,0 +1,85 @@
+/*
+ * 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.karaf.shell.packages;
+
+import java.io.PrintStream;
+import java.util.List;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.felix.gogo.commands.Command;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+@Command(scope = "packages", name = "exports", description = "Display exported packages")
+public class ExportsCommand extends PackageCommandSupport {
+
+    @Option(name = "-i", aliases = { "--imports"}, description = "List bundles importing the packages")
+    boolean imports;
+
+    @Argument(required = false, multiValued = true, description = "bundle ids")
+    List<Long> ids;
+
+    protected void doExecute(PackageAdmin admin) throws Exception {
+        if (ids != null && !ids.isEmpty()) {
+            for (long id : ids) {
+                Bundle bundle = getBundleContext().getBundle(id);
+                if (bundle != null) {
+                    printExports(System.out, bundle, admin.getExportedPackages(bundle));
+                } else {
+                    System.err.println("Bundle ID " + id + " is invalid.");
+                }
+            }
+        }
+        else {
+            printExports(System.out, null, admin.getExportedPackages((Bundle) null));
+        }
+    }
+
+    protected 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(getBundleName(bundle));
+                out.println(": " + exports[i]);
+                if (imports) {
+                    Bundle[] bs = exports[i].getImportingBundles();
+                    if (bs != null) {
+                        for (Bundle b : bs) {
+                            out.println("\t" + getBundleName(b));
+                        }
+                    }
+                }
+            }
+        } else {
+            out.println(getBundleName(target) + ": No active exported packages.");
+        }
+    }
+
+    public static String getBundleName(Bundle bundle) {
+        if (bundle != null) {
+            String name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME);
+            return (name == null)
+                ? "Bundle " + Long.toString(bundle.getBundleId())
+                : name + " (" + Long.toString(bundle.getBundleId()) + ")";
+        }
+        return "[STALE BUNDLE]";
+    }
+
+}
diff --git a/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/ImportsCommand.java b/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/ImportsCommand.java
new file mode 100644
index 0000000..f2d9733
--- /dev/null
+++ b/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/ImportsCommand.java
@@ -0,0 +1,95 @@
+/*
+ * 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.karaf.shell.packages;
+
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+
+@Command(scope = "packages", name = "imports", description = "Display imported packages")
+public class ImportsCommand extends PackageCommandSupport {
+
+    @Argument(required = false, multiValued = true, description = "bundle ids")
+    List<Long> ids;
+
+    protected void doExecute(PackageAdmin admin) throws Exception {
+        Map<Long, List<ExportedPackage>> packages = new HashMap<Long, List<ExportedPackage>>();
+        ExportedPackage[] exported = admin.getExportedPackages((Bundle) null);
+        for (ExportedPackage pkg : exported) {
+            Bundle[] bundles = pkg.getImportingBundles();
+            if (bundles != null) {
+                for (Bundle b : bundles) {
+                    List<ExportedPackage> p = packages.get(b.getBundleId());
+                    if (p == null) {
+                        p = new ArrayList<ExportedPackage>();
+                        packages.put(b.getBundleId(), p);
+                    }
+                    p.add(pkg);
+                }
+            }
+        }
+        if (ids != null && !ids.isEmpty()) {
+            for (long id : ids) {
+                Bundle bundle = getBundleContext().getBundle(id);
+                if (bundle != null) {
+                    printImports(System.out, bundle, packages.get(bundle.getBundleId()));
+                } else {
+                    System.err.println("Bundle ID " + id + " is invalid.");
+                }
+            }
+        }
+        else {
+            List<ExportedPackage> pkgs = new ArrayList<ExportedPackage>();
+            for (List<ExportedPackage> l : packages.values()) {
+                pkgs.addAll(l);
+            }
+            printImports(System.out, null, pkgs);
+        }
+    }
+
+    protected void printImports(PrintStream out, Bundle target, List<ExportedPackage> imports) {
+        if ((imports != null) && (imports.size() > 0)) {
+            for (ExportedPackage p : imports) {
+                Bundle bundle = p.getExportingBundle();
+                out.print(getBundleName(bundle));
+                out.println(": " + p);
+            }
+        } else {
+            out.println(getBundleName(target) + ": No active imported packages.");
+        }
+    }
+
+    public static String getBundleName(Bundle bundle) {
+        if (bundle != null) {
+            String name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME);
+            return (name == null)
+                ? "Bundle " + Long.toString(bundle.getBundleId())
+                : name + " (" + Long.toString(bundle.getBundleId()) + ")";
+        }
+        return "[STALE BUNDLE]";
+    }
+
+}
diff --git a/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/PackageCommandSupport.java b/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/PackageCommandSupport.java
new file mode 100644
index 0000000..84ae132
--- /dev/null
+++ b/karaf/shell/packages/src/main/java/org/apache/felix/karaf/shell/packages/PackageCommandSupport.java
@@ -0,0 +1,55 @@
+/*
+ * 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.karaf.shell.packages;
+
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+/**
+ * Abstract class from which all commands related to the PackageAdmin
+ * service should derive.
+ * This command retrieves a reference to the PackageAdmin service before
+ * calling another method to actually process the command.
+ */
+public abstract class PackageCommandSupport extends OsgiCommandSupport {
+
+    protected Object doExecute() throws Exception {
+        // Get package admin service.
+        ServiceReference ref = getBundleContext().getServiceReference(PackageAdmin.class.getName());
+        if (ref == null) {
+            System.out.println("PackageAdmin service is unavailable.");
+            return null;
+        }
+        try {
+            PackageAdmin admin = (PackageAdmin) getBundleContext().getService(ref);
+            if (admin == null) {
+                System.out.println("PackageAdmin service is unavailable.");
+                return null;
+            }
+
+            doExecute(admin);
+        }
+        finally {
+            getBundleContext().ungetService(ref);
+        }
+        return null;
+    }
+
+    protected abstract void doExecute(PackageAdmin admin) throws Exception;
+
+}
diff --git a/karaf/shell/packages/src/main/resources/OSGI-INF/blueprint/shell-packages.xml b/karaf/shell/packages/src/main/resources/OSGI-INF/blueprint/shell-packages.xml
new file mode 100644
index 0000000..59f63ca
--- /dev/null
+++ b/karaf/shell/packages/src/main/resources/OSGI-INF/blueprint/shell-packages.xml
@@ -0,0 +1,31 @@
+<?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.
+
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <command-bundle xmlns="http://felix.apache.org/karaf/xmlns/shell/v1.0.0">
+        <command name="packages/exports">
+            <action class="org.apache.felix.karaf.shell.packages.ExportsCommand"/>
+        </command>
+        <command name="packages/imports">
+            <action class="org.apache.felix.karaf.shell.packages.ImportsCommand"/>
+        </command>
+    </command-bundle>
+
+</blueprint>