CAMEL-7999: Maven plugin to generate a list of all Camel components.

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a6f335bc
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a6f335bc
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a6f335bc

Branch: refs/heads/master
Commit: a6f335bcd99382210605c138ea9817ee5506ea08
Parents: 251010e
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Nov 13 18:23:43 2014 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Nov 13 18:23:43 2014 +0100

----------------------------------------------------------------------
 .../camel/commands/AbstractCamelController.java |  59 ++++++
 .../apache/camel/commands/CamelController.java  |   8 +
 .../commands/ComponentCatalogListCommand.java   | 183 +++++++++++++++++++
 .../camel/commands/ComponentListCommand.java    |   2 +-
 .../karaf/commands/ComponentCatalogList.java    |  35 ++++
 .../camel/karaf/commands/ComponentList.java     |   2 +-
 .../OSGI-INF/blueprint/camel-commands.xml       |   9 +
 7 files changed, 296 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a6f335bc/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
index 5781342..23fde80 100644
--- 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
@@ -270,4 +270,63 @@ public abstract class AbstractCamelController implements 
CamelController {
         return answer;
     }
 
+    @Override
+    public List<Map<String, String>> listComponentsCatalog() throws Exception {
+        List<Map<String, String>> answer = new ArrayList<Map<String, 
String>>();
+
+        CamelComponentCatalogService catalog = new 
CamelComponentCatalogService();
+        List<String> names = catalog.findComponentNames();
+        for (String name : names) {
+            // load component json data, and parse it to gather the component 
meta-data
+            String json = catalog.componentJSonSchema(name);
+            List<Map<String, String>> rows = 
JsonSchemaHelper.parseJsonSchema("component", json, false);
+
+            String description = null;
+            // the status can be:
+            // - loaded = in use
+            // - classpath = on the classpath
+            // - release = available from the Apache Camel release
+            String status = "release";
+            String type = null;
+            String groupId = null;
+            String artifactId = null;
+            String version = null;
+            for (Map<String, String> row : rows) {
+                if (row.containsKey("description")) {
+                    description = row.get("description");
+                } else if (row.containsKey("javaType")) {
+                    type = row.get("javaType");
+                } else if (row.containsKey("groupId")) {
+                    groupId = row.get("groupId");
+                } else if (row.containsKey("artifactId")) {
+                    artifactId = row.get("artifactId");
+                } else if (row.containsKey("version")) {
+                    version = row.get("version");
+                }
+            }
+
+            Map<String, String> row = new HashMap<String, String>();
+            row.put("name", name);
+            row.put("status", status);
+            if (description != null) {
+                row.put("description", description);
+            }
+            if (type != null) {
+                row.put("type", type);
+            }
+            if (groupId != null) {
+                row.put("groupId", groupId);
+            }
+            if (artifactId != null) {
+                row.put("artifactId", artifactId);
+            }
+            if (version != null) {
+                row.put("version", version);
+            }
+
+            answer.add(row);
+        }
+
+        return answer;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a6f335bc/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
index 199f58e..eb8f5a5 100644
--- 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
@@ -125,4 +125,12 @@ public interface CamelController {
      */
     List<Map<String, String>> listComponents(String camelContextName) throws 
Exception;
 
+    /**
+     * Lists all components from the Camel components catalog
+     *
+     * @return a list of key/value pairs with component information
+     * @throws java.lang.Exception is thrown if error loading resources to 
gather component information
+     */
+    List<Map<String, String>> listComponentsCatalog() throws Exception;
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a6f335bc/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentCatalogListCommand.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentCatalogListCommand.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentCatalogListCommand.java
new file mode 100644
index 0000000..d63ec00
--- /dev/null
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentCatalogListCommand.java
@@ -0,0 +1,183 @@
+/**
+ * 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.camel.commands;
+
+import java.io.PrintStream;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * List all the Camel components from the Camel catalog
+ */
+public class ComponentCatalogListCommand extends AbstractCamelCommand {
+
+    private static final String NAME_COLUMN_LABEL = "Name";
+    private static final String STATUS_COLUMN_LABEL = "Status";
+    private static final String MAVEN_COLUMN_LABEL = "Maven Coordinate";
+    private static final String DESCRIPTION_COLUMN_LABEL = "Description";
+
+    private static final int DEFAULT_COLUMN_WIDTH_INCREMENT = 0;
+    private static final String DEFAULT_FIELD_PREAMBLE = " ";
+    private static final String DEFAULT_FIELD_POSTAMBLE = " ";
+    private static final String DEFAULT_HEADER_PREAMBLE = " ";
+    private static final String DEFAULT_HEADER_POSTAMBLE = " ";
+    private static final int DEFAULT_FORMAT_BUFFER_LENGTH = 24;
+    // descriptions can be very long so clip by default after 120 chars
+    private static final int MAX_COLUMN_WIDTH = 120;
+    private static final int MIN_COLUMN_WIDTH = 12;
+
+    private boolean verbose;
+
+    public ComponentCatalogListCommand(boolean verbose) {
+        this.verbose = verbose;
+    }
+
+    @Override
+    public Object execute(CamelController camelController, PrintStream out, 
PrintStream err) throws Exception {
+        List<Map<String, String>> components = 
camelController.listComponentsCatalog();
+
+        if (components == null || components.isEmpty()) {
+            return null;
+        }
+
+        final Map<String, Integer> columnWidths = 
computeColumnWidths(components);
+        final String headerFormat = buildFormatString(columnWidths, true, 
verbose);
+        final String rowFormat = buildFormatString(columnWidths, false, 
verbose);
+
+        if (verbose) {
+            out.println(String.format(headerFormat, NAME_COLUMN_LABEL, 
STATUS_COLUMN_LABEL, MAVEN_COLUMN_LABEL, DESCRIPTION_COLUMN_LABEL));
+            out.println(String.format(headerFormat, "----", "------", 
"----------------", "-----------"));
+        } else {
+            out.println(String.format(headerFormat, NAME_COLUMN_LABEL, 
DESCRIPTION_COLUMN_LABEL));
+            out.println(String.format(headerFormat, "----", "-----------"));
+        }
+        for (final Map<String, String> component : components) {
+            if (verbose) {
+                String name = safeNull(component.get("name"));
+                String status = safeNull(component.get("status"));
+                String maven = "";
+                if (component.containsKey("groupId") && 
component.containsKey("artifactId") && component.containsKey("version")) {
+                    maven = component.get("groupId") + "/" + 
component.get("artifactId") + "/" + component.get("version");
+                }
+                String description = safeNull(component.get("description"));
+                out.println(String.format(rowFormat, name, status, maven, 
description));
+            } else {
+                String name = safeNull(component.get("name"));
+                String description = safeNull(component.get("description"));
+                out.println(String.format(rowFormat, name, description));
+            }
+        }
+
+        return null;
+    }
+
+    private Map<String, Integer> computeColumnWidths(final 
Iterable<Map<String, String>> components) throws Exception {
+        if (components == null) {
+            return null;
+        } else {
+            // some of the options is optional so we need to start from 1
+            int maxNameLen = NAME_COLUMN_LABEL.length();
+            int maxStatusLen = STATUS_COLUMN_LABEL.length();
+            int maxMavenLen = MAVEN_COLUMN_LABEL.length();
+            int maxDescriptionLen = DESCRIPTION_COLUMN_LABEL.length();
+
+            for (final Map<String, String> component : components) {
+
+                // grab the information and compute max len
+                String name = component.get("name");
+                if (name != null) {
+                    maxNameLen = Math.max(maxNameLen, name.length());
+                }
+                String status = component.get("status");
+                if (status != null) {
+                    maxStatusLen = Math.max(maxStatusLen, status.length());
+                }
+                if (component.containsKey("groupId") && 
component.containsKey("artifactId") && component.containsKey("version")) {
+                    String mvn = component.get("groupId") + "/" + 
component.get("artifactId") + "/" + component.get("version");
+                    maxMavenLen = Math.max(maxMavenLen, mvn.length());
+                }
+                String description = component.get("description");
+                if (description != null) {
+                    maxDescriptionLen = Math.max(maxDescriptionLen, 
description.length());
+                }
+            }
+
+            final Map<String, Integer> retval = new Hashtable<String, 
Integer>(4);
+            retval.put(NAME_COLUMN_LABEL, maxNameLen);
+            retval.put(STATUS_COLUMN_LABEL, maxStatusLen);
+            retval.put(MAVEN_COLUMN_LABEL, maxMavenLen);
+            retval.put(DESCRIPTION_COLUMN_LABEL, maxDescriptionLen);
+
+            return retval;
+        }
+    }
+
+    private String buildFormatString(Map<String, Integer> columnWidths, 
boolean isHeader, boolean isVerbose) {
+        final String fieldPreamble;
+        final String fieldPostamble;
+        final int columnWidthIncrement;
+
+        if (isHeader) {
+            fieldPreamble = DEFAULT_HEADER_PREAMBLE;
+            fieldPostamble = DEFAULT_HEADER_POSTAMBLE;
+        } else {
+            fieldPreamble = DEFAULT_FIELD_PREAMBLE;
+            fieldPostamble = DEFAULT_FIELD_POSTAMBLE;
+        }
+        columnWidthIncrement = DEFAULT_COLUMN_WIDTH_INCREMENT;
+
+        if (verbose) {
+            int nameLen = Math.min(columnWidths.get(NAME_COLUMN_LABEL) + 
columnWidthIncrement, getMaxColumnWidth());
+            int statusLen = Math.min(columnWidths.get(STATUS_COLUMN_LABEL) + 
columnWidthIncrement, getMaxColumnWidth());
+            int mavenLen = Math.min(columnWidths.get(MAVEN_COLUMN_LABEL) + 
columnWidthIncrement, getMaxColumnWidth());
+            int descriptionLen = 
Math.min(columnWidths.get(DESCRIPTION_COLUMN_LABEL) + columnWidthIncrement, 
getMaxColumnWidth());
+
+            nameLen = Math.max(MIN_COLUMN_WIDTH, nameLen);
+            statusLen = Math.max(MIN_COLUMN_WIDTH, statusLen);
+            mavenLen = Math.max(MIN_COLUMN_WIDTH, mavenLen);
+            descriptionLen = Math.max(MIN_COLUMN_WIDTH, descriptionLen);
+
+            final StringBuilder retval = new 
StringBuilder(DEFAULT_FORMAT_BUFFER_LENGTH);
+            
retval.append(fieldPreamble).append("%-").append(nameLen).append('.').append(nameLen).append('s').append(fieldPostamble).append('
 ');
+            
retval.append(fieldPreamble).append("%-").append(statusLen).append('.').append(statusLen).append('s').append(fieldPostamble).append('
 ');
+            
retval.append(fieldPreamble).append("%-").append(mavenLen).append('.').append(mavenLen).append('s').append(fieldPostamble).append('
 ');
+            
retval.append(fieldPreamble).append("%-").append(descriptionLen).append('.').append(descriptionLen).append('s').append(fieldPostamble).append('
 ');
+            return retval.toString();
+        } else {
+            int nameLen = Math.min(columnWidths.get(NAME_COLUMN_LABEL) + 
columnWidthIncrement, getMaxColumnWidth());
+            int descriptionLen = 
Math.min(columnWidths.get(DESCRIPTION_COLUMN_LABEL) + columnWidthIncrement, 
getMaxColumnWidth());
+
+            nameLen = Math.max(MIN_COLUMN_WIDTH, nameLen);
+            descriptionLen = Math.max(MIN_COLUMN_WIDTH, descriptionLen);
+
+            final StringBuilder retval = new 
StringBuilder(DEFAULT_FORMAT_BUFFER_LENGTH);
+            
retval.append(fieldPreamble).append("%-").append(nameLen).append('.').append(nameLen).append('s').append(fieldPostamble).append('
 ');
+            
retval.append(fieldPreamble).append("%-").append(descriptionLen).append('.').append(descriptionLen).append('s').append(fieldPostamble).append('
 ');
+            return retval.toString();
+        }
+    }
+
+    private int getMaxColumnWidth() {
+        if (verbose) {
+            return Integer.MAX_VALUE;
+        } else {
+            return MAX_COLUMN_WIDTH;
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/a6f335bc/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java
index ab96096..b8599aa 100644
--- 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java
@@ -24,7 +24,7 @@ import java.util.Map;
 import org.apache.camel.CamelContext;
 
 /**
- * List all the Camel components that are available in the JVM.
+ * List all the Camel components that are currently used/loaded in the JVM.
  */
 public class ComponentListCommand extends AbstractContextCommand {
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a6f335bc/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentCatalogList.java
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentCatalogList.java
 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentCatalogList.java
new file mode 100644
index 0000000..380f455
--- /dev/null
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentCatalogList.java
@@ -0,0 +1,35 @@
+/**
+ * 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.camel.karaf.commands;
+
+import org.apache.camel.commands.ComponentCatalogListCommand;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+
+@Command(scope = "camel", name = "component-catalog-list", description = 
"Lists all Camel components from the Camel catalog.")
+public class ComponentCatalogList extends CamelCommandSupport {
+
+    @Option(name = "--verbose", aliases = "-v", description = "Verbose output 
which shows more information",
+            required = false, multiValued = false, valueToShowInHelp = "false")
+    boolean verbose;
+
+    protected Object doExecute() throws Exception {
+        ComponentCatalogListCommand command = new 
ComponentCatalogListCommand(verbose);
+        return command.execute(camelController, System.out, System.err);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/a6f335bc/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentList.java
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentList.java
 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentList.java
index 96bc8e7..5ffeb7e 100644
--- 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentList.java
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ComponentList.java
@@ -21,7 +21,7 @@ import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.felix.gogo.commands.Option;
 
-@Command(scope = "camel", name = "component-list", description = "Lists all 
Camel components available in CamelContexts.")
+@Command(scope = "camel", name = "component-list", description = "Lists all 
Camel components that are in use in Karaf.")
 public class ComponentList extends CamelCommandSupport {
 
     @Argument(index = 0, name = "name", description = "The Camel context name 
where to look for the components", required = true, multiValued = false)

http://git-wip-us.apache.org/repos/asf/camel/blob/a6f335bc/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
 
b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
index e298a72..e18cc3f 100644
--- 
a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
+++ 
b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
@@ -233,6 +233,15 @@
         <null/>
       </completers>
     </command>
+    <command name="camel/component-catalog-list">
+      <action class="org.apache.camel.karaf.commands.ComponentCatalogList">
+        <property name="camelController" ref="camelController"/>
+      </action>
+      <completers>
+        <ref component-id="camelContextCompleter"/>
+        <null/>
+      </completers>
+    </command>
   </command-bundle>
 
   <bean id="camelContextCompleter" 
class="org.apache.camel.karaf.commands.completers.CamelContextCompleter">

Reply via email to