FELIX-2141: Add features:info command

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@956491 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/InfoFeatureCommand.java b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/InfoFeatureCommand.java
new file mode 100644
index 0000000..9418c75
--- /dev/null
+++ b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/InfoFeatureCommand.java
@@ -0,0 +1,182 @@
+/*
+ * 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.features.command;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.felix.karaf.features.Feature;
+import org.apache.felix.karaf.features.FeaturesService;
+
+/**
+ * Utility command to display info about features.
+ */
+@Command(scope = "features", name = "info", description = "Shows information about selected information.")
+public class InfoFeatureCommand extends FeaturesCommandSupport {
+
+    @Argument(index = 0, name = "name", description = "The name of the feature", required = true, multiValued = false)
+    private String name;
+
+    @Argument(index = 1, name = "version", description = "The version of the feature", required = false, multiValued = false)
+    private String version;
+
+    @Option(name = "-c", aliases={"--configuration"}, description="Display configuration info", required = false, multiValued = false)
+    private boolean config;
+
+    @Option(name = "-d", aliases={"--dependency"}, description="Display dependencies info", required = false, multiValued = false)
+    private boolean dependency;
+
+    @Option(name = "-b", aliases={"--bundle"}, description="Display bundles info", required = false, multiValued = false)
+    private boolean bundle;
+
+    @Option(name = "-t", aliases={"--tree"}, description="Display feature tree", required = false, multiValued = false)
+    private boolean tree;
+
+    protected void doExecute(FeaturesService admin) throws Exception {
+        Feature feature = null;
+
+        if (version != null && version.length() > 0) {
+            feature = admin.getFeature(name, version);
+        } else {
+            feature = admin.getFeature(name);
+        }
+
+        if (feature == null) {
+            System.out.println("Feature not found");
+            return;
+        }
+
+        // default behavior
+        if (!config && !dependency && !bundle) {
+            config = true;
+            dependency = true;
+            bundle = true;
+        }
+
+        System.out.println("Description of " + feature.getName() + " " + feature.getVersion() + " feature");
+        System.out.println("----------------------------------------------------------------");
+        if (config) {
+            displayConfigInformation(feature);
+        }
+
+        if (dependency) {
+            displayDependencyInformation(feature);
+        }
+
+        if (bundle) {
+            displayBundleInformation(feature);
+        }
+
+        if (tree) {
+            if (config || dependency || bundle) {
+                System.out.println("\nFeature tree");
+            }
+
+            int unresolved = displayFeatureTree(admin, feature, 0, false);
+            if (unresolved > 0) {
+                System.out.println("Tree contains " + unresolved + " unresolved dependencies");
+                System.out.println(" * means that node declares dependency but the dependant feature is not available.");
+            }
+        }
+    }
+
+    private void displayBundleInformation(Feature feature) {
+        List<String> bundles = feature.getBundles();
+        if (bundles.isEmpty()) {
+            System.out.println("Feature has no bundles.");
+        } else {
+            System.out.println("Feature contains followed bundles:");
+            for (String featureBundle : bundles) {
+                System.out.println("  " + featureBundle);
+            }
+        }
+    }
+
+    private void displayDependencyInformation(Feature feature) {
+        List<Feature> dependencies = feature.getDependencies();
+        if (dependencies.isEmpty()) {
+            System.out.println("Feature has no dependencies.");
+        } else {
+            System.out.println("Feature depends on:");
+            for (Feature featureDependency : dependencies) {
+                System.out.println("  " + featureDependency.getName() + " " + featureDependency.getVersion());
+            }
+        }
+    }
+
+    private void displayConfigInformation(Feature feature) {
+        Map<String, Map<String, String>> configurations = feature.getConfigurations();
+        if (configurations.isEmpty()) {
+            System.out.println("Feature has no configuration");
+        } else {
+            System.out.println("Feature configuration:");
+            for (String name : configurations.keySet()) {
+                System.out.println("  " + name);
+            }
+        }
+    }
+
+
+    private int displayFeatureTree(FeaturesService admin, Feature feature, int level, boolean last) throws Exception {
+        int unresolved = 0;
+        String prefix = repeat("   ", level);
+
+        Feature resolved = resolveFeature(admin, feature);
+        if (resolved != null) {
+            System.out.println(prefix + " " + resolved.getName() + " " + resolved.getVersion());
+        } else {
+            System.out.println(prefix + " " + feature.getName() + " " + feature.getVersion() + " *");
+            unresolved++;
+        }
+
+        if (bundle) {
+            List<String> bundles = resolved != null ? resolved.getBundles() : feature.getBundles();
+            for (int i = 0, j = bundles.size(); i < j; i++) {
+                System.out.println(prefix + " " + (i+1 == j ? "\\" : "+") + " " + bundles.get(i));
+            }
+        }
+        List<Feature> dependencies = resolved != null ? resolved.getDependencies() : feature.getDependencies();
+        for (int i = 0, j = dependencies.size(); i < j; i++) {
+            Feature toDisplay = resolveFeature(admin, dependencies.get(i));
+            if (toDisplay == null) {
+                toDisplay = dependencies.get(i);
+            }
+            unresolved += displayFeatureTree(admin, toDisplay, level+1, i + 1 == j);
+        }
+
+        return unresolved;
+    }
+
+    private Feature resolveFeature(FeaturesService admin, Feature feature) throws Exception {
+        return admin.getFeature(feature.getName(), feature.getVersion());
+    }
+
+    private static String repeat(String string, int times) {
+        if (times <= 0) {
+            return "";
+        }
+        else if (times % 2 == 0) {
+            return repeat(string+string, times/2);
+        }
+        else {
+           return string + repeat(string+string, times/2);
+        }
+    }
+}
diff --git a/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/AllFeatureCompleter.java b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/AllFeatureCompleter.java
new file mode 100644
index 0000000..2b42bcc
--- /dev/null
+++ b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/AllFeatureCompleter.java
@@ -0,0 +1,31 @@
+/*
+ * 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.features.command.completers;
+
+import org.apache.felix.karaf.features.Feature;
+
+/**
+ * {@link jline.Completor} for available features.
+ */
+public class AllFeatureCompleter extends FeatureCompleterSupport {
+
+    @Override
+    protected boolean acceptsFeature(Feature feature) {
+        return true;
+    }
+
+}
diff --git a/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/AvailableFeatureCompleter.java b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/AvailableFeatureCompleter.java
index 3286222..ae0b9a6 100644
--- a/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/AvailableFeatureCompleter.java
+++ b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/AvailableFeatureCompleter.java
@@ -16,40 +16,16 @@
  */
 package org.apache.felix.karaf.features.command.completers;
 
-import java.util.List;
-
-import org.apache.felix.karaf.shell.console.completer.StringsCompleter;
-import org.apache.felix.karaf.shell.console.Completer;
-import org.apache.felix.karaf.features.FeaturesService;
 import org.apache.felix.karaf.features.Feature;
 
 /**
- * {@link jline.Completor} for available features.
- *
- * Displays a list of available features from installed repositories.
- *
+ * {@link jline.Completor} for features not installed yet.
  */
-public class AvailableFeatureCompleter implements Completer {
+public class AvailableFeatureCompleter extends FeatureCompleterSupport {
 
-    private FeaturesService featuresService;
-
-    public void setFeaturesService(FeaturesService featuresService) {
-        this.featuresService = featuresService;
+    @Override
+    protected boolean acceptsFeature(Feature feature) {
+        return !featuresService.isInstalled(feature);
     }
 
-    public int complete(final String buffer, final int cursor, final List candidates) {
-        StringsCompleter delegate = new StringsCompleter();
-        try {
-            for (Feature feature : featuresService.listFeatures()) {
-                if (!featuresService.isInstalled( feature )) {
-                    delegate.getStrings().add(feature.getName());
-                }
-            }
-        } catch (Exception e) {
-            // Ignore
-        }
-        return delegate.complete(buffer, cursor, candidates);
-    }
-
-
 }
diff --git a/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/FeatureCompleterSupport.java b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/FeatureCompleterSupport.java
new file mode 100644
index 0000000..430f8b1
--- /dev/null
+++ b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/FeatureCompleterSupport.java
@@ -0,0 +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.karaf.features.command.completers;
+
+import java.util.List;
+
+import org.apache.felix.karaf.features.Feature;
+import org.apache.felix.karaf.features.FeaturesService;
+import org.apache.felix.karaf.shell.console.Completer;
+import org.apache.felix.karaf.shell.console.completer.StringsCompleter;
+
+/**
+ * Base completer for feature commands.
+ */
+public abstract class FeatureCompleterSupport implements Completer {
+
+    /**
+     * Feature service.
+     */
+    protected FeaturesService featuresService;
+
+    public void setFeaturesService(FeaturesService featuresService) {
+        this.featuresService = featuresService;
+    }
+
+    public int complete(final String buffer, final int cursor, final List candidates) {
+        StringsCompleter delegate = new StringsCompleter();
+        try {
+            for (Feature feature : featuresService.listFeatures()) {
+                if (acceptsFeature(feature)) {
+                    delegate.getStrings().add(feature.getName());
+                }
+            }
+        } catch (Exception e) {
+            // Ignore
+        }
+        return delegate.complete(buffer, cursor, candidates);
+    }
+
+    /**
+     * Method for filtering features.
+     *
+     * @param feature The feature.
+     * @return True if feature should be available in completer.
+     */
+    protected abstract boolean acceptsFeature(Feature feature);
+}
diff --git a/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/InstalledFeatureCompleter.java b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/InstalledFeatureCompleter.java
index a2a03fe..07b7b73 100644
--- a/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/InstalledFeatureCompleter.java
+++ b/karaf/features/command/src/main/java/org/apache/felix/karaf/features/command/completers/InstalledFeatureCompleter.java
@@ -16,37 +16,16 @@
  */
 package org.apache.felix.karaf.features.command.completers;
 
-import java.util.List;
-
-import org.apache.felix.karaf.shell.console.Completer;
-import org.apache.felix.karaf.shell.console.completer.StringsCompleter;
-import org.apache.felix.karaf.features.FeaturesService;
 import org.apache.felix.karaf.features.Feature;
 
 /**
  * {@link jline.Completor} for installed features.
- *
- * Displays a list of currently installed features.
- *
  */
-public class InstalledFeatureCompleter implements Completer {
+public class InstalledFeatureCompleter extends FeatureCompleterSupport {
 
-    private FeaturesService featuresService;
-
-    public void setFeaturesService(FeaturesService featuresService) {
-        this.featuresService = featuresService;
-    }
-
-    public int complete(final String buffer, final int cursor, final List candidates) {
-        StringsCompleter delegate = new StringsCompleter();
-        try {
-            for (Feature feature : featuresService.listInstalledFeatures()) {
-                delegate.getStrings().add(feature.getName());
-            }
-        } catch (Exception e) {
-            // Ignore
-        }
-        return delegate.complete(buffer, cursor, candidates);
+    @Override
+    protected boolean acceptsFeature(Feature feature) {
+        return featuresService.isInstalled(feature);
     }
 
 }
diff --git a/karaf/features/command/src/main/resources/OSGI-INF/blueprint/features-command.xml b/karaf/features/command/src/main/resources/OSGI-INF/blueprint/features-command.xml
index 5d8cff0..d9b3f8c 100644
--- a/karaf/features/command/src/main/resources/OSGI-INF/blueprint/features-command.xml
+++ b/karaf/features/command/src/main/resources/OSGI-INF/blueprint/features-command.xml
@@ -39,27 +39,37 @@
         <command name="features/install">
             <action class="org.apache.felix.karaf.features.command.InstallFeatureCommand"/>
             <completers>
-                <ref component-id="installFeatureCompleter" />
+                <ref component-id="uninstalledFeatureCompleter" />
             </completers>
         </command>
         <command name="features/uninstall">
             <action class="org.apache.felix.karaf.features.command.UninstallFeatureCommand"/>
             <completers>
-                <ref component-id="uninstallFeatureCompleter" />
+                <ref component-id="installedFeatureCompleter" />
             </completers>
         </command>
         <command name="features/list">
             <action class="org.apache.felix.karaf.features.command.ListFeaturesCommand"/>
         </command>
+        <command name="features/info">
+            <action class="org.apache.felix.karaf.features.command.InfoFeatureCommand"/>
+            <completers>
+                <ref component-id="allFeatureCompleter" />
+            </completers>
+        </command>
     </command-bundle>
 
     <reference id="featuresService" interface="org.apache.felix.karaf.features.FeaturesService" />
 
-    <bean id="installFeatureCompleter" class="org.apache.felix.karaf.features.command.completers.AvailableFeatureCompleter">
+    <bean id="uninstalledFeatureCompleter" class="org.apache.felix.karaf.features.command.completers.AvailableFeatureCompleter">
         <property name="featuresService" ref="featuresService" />
     </bean>
 
-    <bean id="uninstallFeatureCompleter" class="org.apache.felix.karaf.features.command.completers.InstalledFeatureCompleter">
+    <bean id="installedFeatureCompleter" class="org.apache.felix.karaf.features.command.completers.InstalledFeatureCompleter">
+        <property name="featuresService" ref="featuresService" />
+    </bean>
+
+    <bean id="allFeatureCompleter" class="org.apache.felix.karaf.features.command.completers.AllFeatureCompleter">
         <property name="featuresService" ref="featuresService" />
     </bean>
 
diff --git a/karaf/features/core/src/main/java/org/apache/felix/karaf/features/FeaturesService.java b/karaf/features/core/src/main/java/org/apache/felix/karaf/features/FeaturesService.java
index d8cc113..1e248e7 100644
--- a/karaf/features/core/src/main/java/org/apache/felix/karaf/features/FeaturesService.java
+++ b/karaf/features/core/src/main/java/org/apache/felix/karaf/features/FeaturesService.java
@@ -58,4 +58,7 @@
 
     boolean isInstalled(Feature f);
 
+    Feature getFeature(String name, String version) throws Exception;
+
+    Feature getFeature(String name) throws Exception;
 }
diff --git a/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java b/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java
index 3433b50..959cb4c 100644
--- a/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java
+++ b/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java
@@ -616,7 +616,11 @@
         return installed.containsKey(f);
     }
 
-    protected Feature getFeature(String name, String version) throws Exception {
+    public Feature getFeature(String name) throws Exception {
+        return getFeature(name, FeatureImpl.DEFAULT_VERSION);
+    }
+
+    public Feature getFeature(String name, String version) throws Exception {
         if (version != null) {
             version = version.trim();
         }