camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/7] camel git commit: CAMEL-7999: Add label to @UriEndpoint so we can group endpoints.
Date Fri, 14 Nov 2014 12:02:18 GMT
Repository: camel
Updated Branches:
  refs/heads/master 0906ef82a -> 8133ab7d8


CAMEL-7999: Add label to @UriEndpoint so we can group endpoints.


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

Branch: refs/heads/master
Commit: 6cf45ca2b6ece6e3ce2f8e42c9c4d4273af1a8b0
Parents: 1590a35
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Fri Nov 14 11:13:56 2014 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Nov 14 13:02:05 2014 +0100

----------------------------------------------------------------------
 .../camel/catalog/CamelComponentCatalog.java    |   8 ++
 .../catalog/DefaultCamelComponentCatalog.java   |  27 +++++
 .../camel/commands/AbstractCamelController.java |   6 +
 .../apache/camel/commands/CamelController.java  |   9 ++
 .../camel/commands/CatalogLabelListCommand.java | 109 +++++++++++++++++++
 .../catalog/CamelComponentCatalogTest.java      |  14 +++
 .../camel/karaf/commands/CatalogLabelList.java  |  30 +++++
 .../OSGI-INF/blueprint/camel-commands.xml       |   9 +-
 8 files changed, 208 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
index 766ff39..7f23733 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
@@ -17,6 +17,7 @@
 package org.apache.camel.catalog;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * Catalog of all the Camel components from this Apache Camel release.
@@ -40,4 +41,11 @@ public interface CamelComponentCatalog {
      * @return component details in JSon
      */
     String componentJSonSchema(String name);
+
+    /**
+     * Find all the unique label names all the components are using.
+     *
+     * @return a set of all the labels.
+     */
+    Set<String> findLabels();
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
index b5ee6dd..4c0f542 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
@@ -24,6 +24,9 @@ import java.io.LineNumberReader;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 import java.util.regex.PatternSyntaxException;
 
 /**
@@ -95,6 +98,30 @@ public class DefaultCamelComponentCatalog implements CamelComponentCatalog
{
         return null;
     }
 
+    @Override
+    public Set<String> findLabels() {
+        SortedSet<String> answer = new TreeSet<String>();
+
+        List<String> names = findComponentNames();
+        for (String name : names) {
+            String json = componentJSonSchema(name);
+            if (json != null) {
+                List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("component",
json, false);
+                for (Map<String, String> row : rows) {
+                    if (row.containsKey("label")) {
+                        String label = row.get("label");
+                        String[] parts = label.split(",");
+                        for (String part : parts) {
+                            answer.add(part);
+                        }
+                    }
+                }
+            }
+        }
+
+        return answer;
+    }
+
     /**
      * Loads the entire stream into memory as a String and returns it.
      * <p/>

http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/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 f3965ae..8179bc7 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
@@ -24,6 +24,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
@@ -345,4 +346,9 @@ public abstract class AbstractCamelController implements CamelController
{
 
         return answer;
     }
+
+    @Override
+    public Set<String> listLabelCatalog() throws Exception {
+        return catalog.findLabels();
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/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 ad05893..e3d8333 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
@@ -18,6 +18,7 @@ package org.apache.camel.commands;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
@@ -134,4 +135,12 @@ public interface CamelController {
      */
     List<Map<String, String>> listComponentsCatalog(String filter) throws Exception;
 
+    /**
+     * Lists all the labels from the Camel components catalog
+     *
+     * @return a sorted set with all the label names
+     * @throws java.lang.Exception is thrown if error loading resources to gather component
information
+     */
+    Set<String> listLabelCatalog() throws Exception;
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CatalogLabelListCommand.java
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CatalogLabelListCommand.java
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CatalogLabelListCommand.java
new file mode 100644
index 0000000..d47283a
--- /dev/null
+++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CatalogLabelListCommand.java
@@ -0,0 +1,109 @@
+/**
+ * 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.Map;
+import java.util.Set;
+
+/**
+ * From the Camel catalog lists all the components.
+ */
+public class CatalogLabelListCommand extends AbstractCamelCommand {
+
+    private static final String LABEL_COLUMN_LABEL = "Label";
+
+    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;
+
+    public CatalogLabelListCommand() {
+    }
+
+    @Override
+    public Object execute(CamelController camelController, PrintStream out, PrintStream err)
throws Exception {
+        Set<String> labels = camelController.listLabelCatalog();
+
+        if (labels.isEmpty()) {
+            return null;
+        }
+
+        final Map<String, Integer> columnWidths = computeColumnWidths(labels);
+        final String headerFormat = buildFormatString(columnWidths, true);
+        final String rowFormat = buildFormatString(columnWidths, false);
+
+        out.println(String.format(headerFormat, LABEL_COLUMN_LABEL));
+        out.println(String.format(headerFormat, "-----"));
+        for (String label : labels) {
+            out.println(String.format(rowFormat, label));
+        }
+
+        return null;
+    }
+
+    private Map<String, Integer> computeColumnWidths(final Iterable<String> labels)
throws Exception {
+        if (labels == null) {
+            return null;
+        } else {
+            // some of the options is optional so we need to start from 1
+            int maxLabelLen = LABEL_COLUMN_LABEL.length();
+
+            for (String label : labels) {
+                maxLabelLen = Math.max(maxLabelLen, label.length());
+            }
+
+            final Map<String, Integer> retval = new Hashtable<String, Integer>(1);
+            retval.put(LABEL_COLUMN_LABEL, maxLabelLen);
+
+            return retval;
+        }
+    }
+
+    private String buildFormatString(Map<String, Integer> columnWidths, boolean isHeader)
{
+        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;
+
+        int labelLen = Math.min(columnWidths.get(LABEL_COLUMN_LABEL) + columnWidthIncrement,
getMaxColumnWidth());
+        labelLen = Math.max(MIN_COLUMN_WIDTH, labelLen);
+
+        final StringBuilder retval = new StringBuilder(DEFAULT_FORMAT_BUFFER_LENGTH);
+        retval.append(fieldPreamble).append("%-").append(labelLen).append('.').append(labelLen).append('s').append(fieldPostamble).append('
');
+        return retval.toString();
+    }
+
+    private int getMaxColumnWidth() {
+        return MAX_COLUMN_WIDTH;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
b/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
index f9bb0db..d6b8067 100644
--- a/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
+++ b/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/catalog/CamelComponentCatalogTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.commands.catalog;
 
 import java.util.List;
+import java.util.Set;
 
 import org.apache.camel.catalog.CamelComponentCatalog;
 import org.apache.camel.catalog.DefaultCamelComponentCatalog;
@@ -107,4 +108,17 @@ public class CamelComponentCatalogTest {
         assertTrue("Should find ftp component", json.contains("ftp"));
     }
 
+    @Test
+    public void testLabels() {
+        CamelComponentCatalog catalog = new DefaultCamelComponentCatalog();
+        Set<String> labels = catalog.findLabels();
+
+        assertNotNull(labels);
+
+        assertTrue("Should find labels", labels.size() > 0);
+        assertTrue("Should find core label", labels.contains("core"));
+        assertTrue("Should find testing label", labels.contains("testing"));
+        assertTrue("Should find rest label", labels.contains("rest"));
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CatalogLabelList.java
----------------------------------------------------------------------
diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CatalogLabelList.java
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CatalogLabelList.java
new file mode 100644
index 0000000..2989ae4
--- /dev/null
+++ b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CatalogLabelList.java
@@ -0,0 +1,30 @@
+/**
+ * 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.CatalogLabelListCommand;
+import org.apache.felix.gogo.commands.Command;
+
+@Command(scope = "camel", name = "catalog-label-list", description = "Lists all Camel component
labels from the Camel catalog.")
+public class CatalogLabelList extends CamelCommandSupport {
+
+    protected Object doExecute() throws Exception {
+        CatalogLabelListCommand command = new CatalogLabelListCommand();
+        return command.execute(camelController, System.out, System.err);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6cf45ca2/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 0e87fe6..d0496fb 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
@@ -237,10 +237,11 @@
       <action class="org.apache.camel.karaf.commands.CatalogComponentList">
         <property name="camelController" ref="camelController"/>
       </action>
-      <completers>
-        <ref component-id="camelContextCompleter"/>
-        <null/>
-      </completers>
+    </command>
+    <command name="camel/catalog-label-list">
+      <action class="org.apache.camel.karaf.commands.CatalogLabelList">
+        <property name="camelController" ref="camelController"/>
+      </action>
     </command>
   </command-bundle>
 


Mime
View raw message