CAMEL-10434: Camel catalog support different runtimes to provide their 
supported list of components etc.


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

Branch: refs/heads/camel-2.18.x
Commit: 4c256b4279dc9fa7127ea1f29d3f9f7c475e3f86
Parents: fc27052
Author: Claus Ibsen <davscl...@apache.org>
Authored: Mon Nov 7 14:21:05 2016 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Mon Nov 7 16:46:20 2016 +0100

----------------------------------------------------------------------
 platforms/catalog-provider-karaf/pom.xml        |   2 +-
 .../catalog/karaf/KarafRuntimeProvider.java     | 117 ++++++++++++++++++-
 .../catalog/karaf/KarafRuntimeProviderTest.java |  93 +++++++++++++++
 3 files changed, 208 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4c256b42/platforms/catalog-provider-karaf/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/catalog-provider-karaf/pom.xml 
b/platforms/catalog-provider-karaf/pom.xml
index 9aafd73..6923faa 100644
--- a/platforms/catalog-provider-karaf/pom.xml
+++ b/platforms/catalog-provider-karaf/pom.xml
@@ -89,7 +89,7 @@
               <goal>copy-resources</goal>
             </goals>
             <configuration>
-              <outputDirectory>${basedir}/target/resources</outputDirectory>
+              
<outputDirectory>${basedir}/target/classes/org/apache/camel/catalog/karaf</outputDirectory>
               <resources>
                 <resource>
                   <directory>../karaf/features/target/classes</directory>

http://git-wip-us.apache.org/repos/asf/camel/blob/4c256b42/platforms/catalog-provider-karaf/src/main/java/org/apache/camel/catalog/karaf/KarafRuntimeProvider.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog-provider-karaf/src/main/java/org/apache/camel/catalog/karaf/KarafRuntimeProvider.java
 
b/platforms/catalog-provider-karaf/src/main/java/org/apache/camel/catalog/karaf/KarafRuntimeProvider.java
index 071d6e9..9c8ac17 100644
--- 
a/platforms/catalog-provider-karaf/src/main/java/org/apache/camel/catalog/karaf/KarafRuntimeProvider.java
+++ 
b/platforms/catalog-provider-karaf/src/main/java/org/apache/camel/catalog/karaf/KarafRuntimeProvider.java
@@ -16,14 +16,30 @@
  */
 package org.apache.camel.catalog.karaf;
 
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultRuntimeProvider;
+import org.apache.camel.catalog.JSonSchemaHelper;
 import org.apache.camel.catalog.RuntimeProvider;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import static org.w3c.dom.Node.ELEMENT_NODE;
 
 public class KarafRuntimeProvider implements RuntimeProvider {
 
+    private static final String FEATURES = 
"org/apache/camel/catalog/karaf/features.xml";
     private CamelCatalog camelCatalog;
+    private DefaultRuntimeProvider defaultProvider = new 
DefaultRuntimeProvider();
+
+    private Map<String, List<Map<String, String>>> rowsCache = new HashMap<>();
 
     @Override
     public CamelCatalog getCamelCatalog() {
@@ -33,6 +49,7 @@ public class KarafRuntimeProvider implements RuntimeProvider {
     @Override
     public void setCamelCatalog(CamelCatalog camelCatalog) {
         this.camelCatalog = camelCatalog;
+        this.defaultProvider.setCamelCatalog(camelCatalog);
     }
 
     @Override
@@ -42,17 +59,111 @@ public class KarafRuntimeProvider implements 
RuntimeProvider {
 
     @Override
     public List<String> findComponentNames() {
-        // parse the karaf features xml file
-        return null;
+        // find the component name from all the default components
+        List<String> allNames = defaultProvider.findComponentNames();
+
+        List<String> answer = new ArrayList<>();
+
+        // filter out to only include what's in the karaf features file
+        InputStream is = 
camelCatalog.getVersionManager().getResourceAsStream(FEATURES);
+        if (is != null) {
+            try {
+                DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
+                dbf.setIgnoringComments(true);
+                dbf.setIgnoringElementContentWhitespace(true);
+                dbf.setNamespaceAware(false);
+                dbf.setValidating(false);
+                dbf.setXIncludeAware(false);
+                Document dom = dbf.newDocumentBuilder().parse(is);
+                NodeList children = dom.getElementsByTagName("features");
+
+                for (int i = 0; i < children.getLength(); i++) {
+                    Node child = children.item(i);
+                    if (child.getNodeType() == ELEMENT_NODE) {
+                        NodeList children2 = child.getChildNodes();
+                        for (int j = 0; j < children2.getLength(); j++) {
+                            Node child2 = children2.item(j);
+                            if ("feature".equals(child2.getNodeName())) {
+                                // the name attribute is the maven artifact id 
of the component
+                                String artifactId = 
child2.getAttributes().getNamedItem("name").getTextContent();
+                                if (artifactId != null && 
artifactId.startsWith("camel-")) {
+                                    // find the component name based on the 
artifact id
+                                    String componentName = 
componentNameFromArtifactId(artifactId, allNames);
+                                    if (componentName != null) {
+                                        answer.add(componentName);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                // ignore
+            }
+        }
+
+        System.out.println("Total components " + allNames.size() + " karaf 
supports " + answer.size());
+
+        // clear temporary cache
+        rowsCache.clear();
+
+        return answer;
     }
 
     @Override
     public List<String> findDataFormatNames() {
-        return null;
+        // karaf support all data formats
+        return defaultProvider.findDataFormatNames();
     }
 
     @Override
     public List<String> findLanguageNames() {
+        // karaf support all languages
+        return defaultProvider.findLanguageNames();
+    }
+
+    private String componentNameFromArtifactId(String artifactId, List<String> 
allNames) {
+        // try a quick shortcut that is faster
+        String quick = artifactId.startsWith("camel-") ? 
artifactId.substring(6) : null;
+        if (quick != null) {
+            String json = camelCatalog.componentJSonSchema(quick);
+            if (json != null) {
+                List<Map<String, String>> rows = rowsCache.get(quick);
+                if (rows == null) {
+                    rows = JSonSchemaHelper.parseJsonSchema("component", json, 
false);
+                    rowsCache.put(quick, rows);
+                }
+                String componentArtifactId = getArtifactId(rows);
+                if (artifactId.equals(componentArtifactId)) {
+                    return quick;
+                }
+            }
+        }
+
+        for (String name : allNames) {
+            String json = camelCatalog.componentJSonSchema(name);
+            if (json != null) {
+                List<Map<String, String>> rows = rowsCache.get(quick);
+                if (rows == null) {
+                    rows = JSonSchemaHelper.parseJsonSchema("component", json, 
false);
+                    rowsCache.put(quick, rows);
+                }
+                String componentArtifactId = getArtifactId(rows);
+                if (artifactId.equals(componentArtifactId)) {
+                    return name;
+                }
+            }
+        }
         return null;
     }
+
+    public static String getArtifactId(List<Map<String, String>> rows) {
+        for (Map<String, String> row : rows) {
+            if (row.get("artifactId") != null) {
+                return row.get("artifactId");
+            }
+        }
+        return null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/4c256b42/platforms/catalog-provider-karaf/src/test/java/org/apache/camel/catalog/karaf/KarafRuntimeProviderTest.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog-provider-karaf/src/test/java/org/apache/camel/catalog/karaf/KarafRuntimeProviderTest.java
 
b/platforms/catalog-provider-karaf/src/test/java/org/apache/camel/catalog/karaf/KarafRuntimeProviderTest.java
new file mode 100644
index 0000000..9d83d52
--- /dev/null
+++ 
b/platforms/catalog-provider-karaf/src/test/java/org/apache/camel/catalog/karaf/KarafRuntimeProviderTest.java
@@ -0,0 +1,93 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.catalog.karaf;
+
+import java.util.List;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class KarafRuntimeProviderTest {
+
+    static CamelCatalog catalog;
+
+    @BeforeClass
+    public static void createCamelCatalog() {
+        catalog = new DefaultCamelCatalog();
+        catalog.setRuntimeProvider(new KarafRuntimeProvider());
+    }
+
+    @Test
+    public void testGetVersion() throws Exception {
+        String version = catalog.getCatalogVersion();
+        assertNotNull(version);
+
+        String loaded = catalog.getLoadedVersion();
+        assertNotNull(loaded);
+        assertEquals(version, loaded);
+    }
+
+    @Test
+    public void testProviderName() throws Exception {
+        assertEquals("karaf", catalog.getRuntimeProvider().getProviderName());
+    }
+
+    @Test
+    public void testFindComponentNames() throws Exception {
+        List<String> names = catalog.findComponentNames();
+
+        assertNotNull(names);
+        assertFalse(names.isEmpty());
+
+        assertTrue(names.contains("ftp"));
+        assertTrue(names.contains("paxlogging"));
+        // camel-docker does not work in Karaf
+        assertFalse(names.contains("docker"));
+    }
+
+    @Test
+    public void testFindDataFormatNames() throws Exception {
+        List<String> names = catalog.findDataFormatNames();
+
+        assertNotNull(names);
+        assertFalse(names.isEmpty());
+
+        assertTrue(names.contains("bindy-csv"));
+        assertTrue(names.contains("zip"));
+        assertTrue(names.contains("zipfile"));
+    }
+
+    @Test
+    public void testFindLanguageNames() throws Exception {
+        List<String> names = catalog.findLanguageNames();
+
+        assertNotNull(names);
+        assertFalse(names.isEmpty());
+
+        assertTrue(names.contains("simple"));
+        assertTrue(names.contains("spel"));
+        assertTrue(names.contains("xpath"));
+    }
+
+}

Reply via email to