camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: Allow to add custom component/dataformat to catalog with provided json schema
Date Thu, 29 Sep 2016 12:33:09 GMT
Repository: camel
Updated Branches:
  refs/heads/master 281bac41e -> 9210dec59


Allow to add custom component/dataformat to catalog with provided json schema


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

Branch: refs/heads/master
Commit: 9210dec5982e9a2c55c71e1f03c37ac1002556da
Parents: 281bac4
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Sep 29 14:32:58 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Sep 29 14:32:58 2016 +0200

----------------------------------------------------------------------
 .../org/apache/camel/catalog/CamelCatalog.java  | 18 +++++
 .../camel/catalog/DefaultCamelCatalog.java      | 74 +++++++++++++-------
 .../apache/camel/catalog/CamelCatalogTest.java  | 39 +++++++++--
 3 files changed, 103 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9210dec5/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
index 40c88c9..5ee8714 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
@@ -69,6 +69,15 @@ public interface CamelCatalog {
     void addComponent(String name, String className);
 
     /**
+     * Adds a 3rd party component to this catalog.
+     *
+     * @param name       the component name
+     * @param className  the fully qualified class name for the component class
+     * @param jsonSchema the component JSon schema
+     */
+    void addComponent(String name, String className, String jsonSchema);
+
+    /**
      * Adds a 3rd party data format to this catalog.
      *
      * @param name      the data format name
@@ -77,6 +86,15 @@ public interface CamelCatalog {
     void addDataFormat(String name, String className);
 
     /**
+     * Adds a 3rd party data format to this catalog.
+     *
+     * @param name      the data format name
+     * @param className the fully qualified class name for the data format class
+     * @param jsonSchema the data format JSon schema
+     */
+    void addDataFormat(String name, String className, String jsonSchema);
+
+    /**
      * The version of this Camel Catalog
      */
     String getCatalogVersion();

http://git-wip-us.apache.org/repos/asf/camel/blob/9210dec5/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
index f6c8334..6f723b4 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
@@ -90,7 +90,9 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
     // 3rd party components/data-formats
     private final Map<String, String> extraComponents = new HashMap<String, String>();
+    private final Map<String, String> extraComponentsJSonSchema = new HashMap<String,
String>();
     private final Map<String, String> extraDataFormats = new HashMap<String, String>();
+    private final Map<String, String> extraDataFormatsJSonSchema = new HashMap<String,
String>();
 
     // cache of operation -> result
     private final Map<String, Object> cache = new HashMap<String, Object>();
@@ -153,6 +155,14 @@ public class DefaultCamelCatalog implements CamelCatalog {
     }
 
     @Override
+    public void addComponent(String name, String className, String jsonSchema) {
+        addComponent(name, className);
+        if (jsonSchema != null) {
+            extraComponentsJSonSchema.put(name, jsonSchema);
+        }
+    }
+
+    @Override
     public void addDataFormat(String name, String className) {
         extraDataFormats.put(name, className);
         // invalidate the cache
@@ -162,6 +172,14 @@ public class DefaultCamelCatalog implements CamelCatalog {
     }
 
     @Override
+    public void addDataFormat(String name, String className, String jsonSchema) {
+        addDataFormat(name, className);
+        if (jsonSchema != null) {
+            extraDataFormatsJSonSchema.put(name, jsonSchema);
+        }
+    }
+
+    @Override
     public String getCatalogVersion() {
         return version.getVersion();
     }
@@ -467,18 +485,22 @@ public class DefaultCamelCatalog implements CamelCatalog {
                     // ignore
                 }
             } else {
-                // its maybe a third party so try load it
-                String className = extraComponents.get(name);
-                if (className != null) {
-                    String packageName = className.substring(0, className.lastIndexOf('.'));
-                    packageName = packageName.replace('.', '/');
-                    String path = packageName + "/" + name + ".json";
-                    is = versionManager.getResourceAsStream(path);
-                    if (is != null) {
-                        try {
-                            answer = CatalogHelper.loadText(is);
-                        } catch (IOException e) {
-                            // ignore
+                // its maybe a third party so try to see if we have the json schema already
+                answer = extraComponentsJSonSchema.get(name);
+                if (answer == null) {
+                    // or if we can load it from the classpath
+                    String className = extraComponents.get(name);
+                    if (className != null) {
+                        String packageName = className.substring(0, className.lastIndexOf('.'));
+                        packageName = packageName.replace('.', '/');
+                        String path = packageName + "/" + name + ".json";
+                        is = versionManager.getResourceAsStream(path);
+                        if (is != null) {
+                            try {
+                                answer = CatalogHelper.loadText(is);
+                            } catch (IOException e) {
+                                // ignore
+                            }
                         }
                     }
                 }
@@ -509,18 +531,22 @@ public class DefaultCamelCatalog implements CamelCatalog {
                     // ignore
                 }
             } else {
-                // its maybe a third party so try load it
-                String className = extraDataFormats.get(name);
-                if (className != null) {
-                    String packageName = className.substring(0, className.lastIndexOf('.'));
-                    packageName = packageName.replace('.', '/');
-                    String path = packageName + "/" + name + ".json";
-                    is = versionManager.getResourceAsStream(path);
-                    if (is != null) {
-                        try {
-                            answer = CatalogHelper.loadText(is);
-                        } catch (IOException e) {
-                            // ignore
+                // its maybe a third party so try to see if we have the json schema already
+                answer = extraDataFormatsJSonSchema.get(name);
+                if (answer == null) {
+                    // or if we can load it from the classpath
+                    String className = extraDataFormats.get(name);
+                    if (className != null) {
+                        String packageName = className.substring(0, className.lastIndexOf('.'));
+                        packageName = packageName.replace('.', '/');
+                        String path = packageName + "/" + name + ".json";
+                        is = versionManager.getResourceAsStream(path);
+                        if (is != null) {
+                            try {
+                                answer = CatalogHelper.loadText(is);
+                            } catch (IOException e) {
+                                // ignore
+                            }
                         }
                     }
                 }

http://git-wip-us.apache.org/repos/asf/camel/blob/9210dec5/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
index 21a1527..39fe6e4 100644
--- a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
+++ b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.catalog;
 
+import java.io.FileInputStream;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -713,8 +714,6 @@ public class CamelCatalogTest {
 
     @Test
     public void testAddComponent() throws Exception {
-        assertFalse(catalog.findComponentNames().contains("dummy"));
-
         catalog.addComponent("dummy", "org.foo.camel.DummyComponent");
 
         assertTrue(catalog.findComponentNames().contains("dummy"));
@@ -729,9 +728,24 @@ public class CamelCatalogTest {
     }
 
     @Test
-    public void testAddDataFormat() throws Exception {
-        assertFalse(catalog.findDataFormatNames().contains("dummyformat"));
+    public void testAddComponentWithJson() throws Exception {
+        String json = loadText(new FileInputStream("src/test/resources/org/foo/camel/dummy.json"));
+        assertNotNull(json);
+        catalog.addComponent("dummy", "org.foo.camel.DummyComponent", json);
+
+        assertTrue(catalog.findComponentNames().contains("dummy"));
+
+        json = catalog.componentJSonSchema("dummy");
+        assertNotNull(json);
 
+        // validate we can parse the json
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode tree = mapper.readTree(json);
+        assertNotNull(tree);
+    }
+
+    @Test
+    public void testAddDataFormat() throws Exception {
         catalog.addDataFormat("dummyformat", "org.foo.camel.DummyDataFormat");
 
         assertTrue(catalog.findDataFormatNames().contains("dummyformat"));
@@ -746,6 +760,23 @@ public class CamelCatalogTest {
     }
 
     @Test
+    public void testAddDataFormatWithJSon() throws Exception {
+        String json = loadText(new FileInputStream("src/test/resources/org/foo/camel/dummyformat.json"));
+        assertNotNull(json);
+        catalog.addDataFormat("dummyformat", "org.foo.camel.DummyDataFormat", json);
+
+        assertTrue(catalog.findDataFormatNames().contains("dummyformat"));
+
+        json = catalog.dataFormatJSonSchema("dummyformat");
+        assertNotNull(json);
+
+        // validate we can parse the json
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode tree = mapper.readTree(json);
+        assertNotNull(tree);
+    }
+
+    @Test
     public void testSimpleExpression() throws Exception {
         SimpleValidationResult result = catalog.validateSimpleExpression("${body}");
         assertTrue(result.isSuccess());


Mime
View raw message