camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-10703: camel-catalog - Add api for validating any Camel languge
Date Fri, 13 Jan 2017 11:59:20 GMT
Repository: camel
Updated Branches:
  refs/heads/master 73907c022 -> a9cf098fd


CAMEL-10703: camel-catalog - Add api for validating any Camel languge


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

Branch: refs/heads/master
Commit: a9cf098fdce05609a91d2ca79713a3f81b025d43
Parents: 73907c0
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Fri Jan 13 12:59:10 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Jan 13 12:59:10 2017 +0100

----------------------------------------------------------------------
 .../org/apache/camel/catalog/CamelCatalog.java  | 32 ++++++++-
 .../camel/catalog/DefaultCamelCatalog.java      | 75 +++++++++++++++++++-
 .../camel/catalog/LanguageValidationResult.java | 65 +++++++++++++++++
 .../camel/catalog/SimpleValidationResult.java   | 45 ++----------
 .../apache/camel/catalog/CamelCatalogTest.java  | 31 ++++++--
 5 files changed, 202 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a9cf098f/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 ff70e58..a83b886 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
@@ -359,7 +359,9 @@ public interface CamelCatalog {
      *
      * @param simple  the simple expression
      * @return validation result
+     * @deprecated use {@link #validateSimpleExpression(ClassLoader, String)}
      */
+    @Deprecated
     SimpleValidationResult validateSimpleExpression(String simple);
 
     /**
@@ -367,7 +369,7 @@ public interface CamelCatalog {
      * <p/>
      * <b>Important:</b> This requires having <tt>camel-core</tt>
on the classpath
      *
-     * @param classLoader a custom classloader to use for loading the simple language from
the classpath
+     * @param classLoader a custom classloader to use for loading the language from the classpath,
or <tt>null</tt> for using default classloader
      * @param simple  the simple expression
      * @return validation result
      */
@@ -380,7 +382,9 @@ public interface CamelCatalog {
      *
      * @param simple  the simple predicate
      * @return validation result
+     * @deprecated use {@link #validateSimplePredicate(ClassLoader, String)}
      */
+    @Deprecated
     SimpleValidationResult validateSimplePredicate(String simple);
 
     /**
@@ -388,13 +392,37 @@ public interface CamelCatalog {
      * <p/>
      * <b>Important:</b> This requires having <tt>camel-core</tt>
on the classpath
      *
-     * @param classLoader a custom classloader to use for loading the simple language from
the classpath
+     * @param classLoader a custom classloader to use for loading the language from the classpath,
or <tt>null</tt> for using default classloader
      * @param simple  the simple predicate
      * @return validation result
      */
     SimpleValidationResult validateSimplePredicate(ClassLoader classLoader, String simple);
 
     /**
+     * Parses and validates the language as a predicate
+     * <p/>
+     * <b>Important:</b> This requires having <tt>camel-core</tt>
and the language dependencies on the classpath
+     *
+     * @param classLoader a custom classloader to use for loading the language from the classpath,
or <tt>null</tt> for using default classloader
+     * @param language the name of the language
+     * @param text  the predicate text
+     * @return validation result
+     */
+    LanguageValidationResult validateLanguagePredicate(ClassLoader classLoader, String language,
String text);
+
+    /**
+     * Parses and validates the language as an expression
+     * <p/>
+     * <b>Important:</b> This requires having <tt>camel-core</tt>
and the language dependencies on the classpath
+     *
+     * @param classLoader a custom classloader to use for loading the language from the classpath,
or <tt>null</tt> for using default classloader
+     * @param language the name of the language
+     * @param text  the expression text
+     * @return validation result
+     */
+    LanguageValidationResult validateLanguageExpression(ClassLoader classLoader, String language,
String text);
+
+    /**
      * Returns the component name from the given endpoint uri
      *
      * @param uri  the endpoint uri

http://git-wip-us.apache.org/repos/asf/camel/blob/a9cf098f/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 435d60b..e30616f 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
@@ -1797,7 +1797,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
     @Override
     public SimpleValidationResult validateSimpleExpression(String simple) {
-        return doValidateSimple(DefaultCamelCatalog.class.getClassLoader(), simple, false);
+        return doValidateSimple(null, simple, false);
     }
 
     @Override
@@ -1807,7 +1807,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
     @Override
     public SimpleValidationResult validateSimplePredicate(String simple) {
-        return doValidateSimple(DefaultCamelCatalog.class.getClassLoader(), simple, true);
+        return doValidateSimple(null, simple, true);
     }
 
     @Override
@@ -1816,6 +1816,10 @@ public class DefaultCamelCatalog implements CamelCatalog {
     }
 
     private SimpleValidationResult doValidateSimple(ClassLoader classLoader, String simple,
boolean predicate) {
+        if (classLoader == null) {
+            classLoader = DefaultCamelCatalog.class.getClassLoader();
+        }
+
         SimpleValidationResult answer = new SimpleValidationResult(simple);
 
         Object instance = null;
@@ -1890,6 +1894,73 @@ public class DefaultCamelCatalog implements CamelCatalog {
         return answer;
     }
 
+    @Override
+    public LanguageValidationResult validateLanguagePredicate(ClassLoader classLoader, String
language, String text) {
+        return doValidateLanguage(classLoader, language, text, true);
+    }
+
+    @Override
+    public LanguageValidationResult validateLanguageExpression(ClassLoader classLoader, String
language, String text) {
+        return doValidateLanguage(classLoader, language, text, false);
+    }
+
+    private LanguageValidationResult doValidateLanguage(ClassLoader classLoader, String language,
String text, boolean predicate) {
+        if (classLoader == null) {
+            classLoader = DefaultCamelCatalog.class.getClassLoader();
+        }
+
+        SimpleValidationResult answer = new SimpleValidationResult(text);
+
+        String json = languageJSonSchema(language);
+        if (json == null) {
+            answer.setError("Unknown language " + language);
+            return answer;
+        }
+
+        List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("language",
json, false);
+        String className = null;
+        for (Map<String, String> row : rows) {
+            if (row.containsKey("javaType")) {
+                className = row.get("javaType");
+            }
+        }
+
+        if (className == null) {
+            answer.setError("Cannot find javaType for language " + language);
+            return answer;
+        }
+
+        Object instance = null;
+        Class clazz = null;
+        try {
+            clazz = classLoader.loadClass(className);
+            instance = clazz.newInstance();
+        } catch (Exception e) {
+            // ignore
+        }
+
+        if (clazz != null && instance != null) {
+            Throwable cause = null;
+            try {
+                if (predicate) {
+                    instance.getClass().getMethod("createPredicate", String.class).invoke(instance,
text);
+                } else {
+                    instance.getClass().getMethod("createExpression", String.class).invoke(instance,
text);
+                }
+            } catch (InvocationTargetException e) {
+                cause = e.getTargetException();
+            } catch (Exception e) {
+                cause = e;
+            }
+
+            if (cause != null) {
+                answer.setError(cause.getMessage());
+            }
+        }
+
+        return answer;
+    }
+
     /**
      * Special logic for log endpoints to deal when showAll=true
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/a9cf098f/platforms/catalog/src/main/java/org/apache/camel/catalog/LanguageValidationResult.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/LanguageValidationResult.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/LanguageValidationResult.java
new file mode 100644
index 0000000..4a977eb
--- /dev/null
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/LanguageValidationResult.java
@@ -0,0 +1,65 @@
+/**
+ * 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.catalog;
+
+import java.io.Serializable;
+
+/**
+ * Validation result of parsing a language expression or predicate
+ */
+public class LanguageValidationResult implements Serializable {
+    private final String text;
+    private String error;
+    private String shortError;
+    private int index;
+
+    public LanguageValidationResult(String text) {
+        this.text = text;
+    }
+
+    public String getText() {
+        return text;
+    }
+
+    public boolean isSuccess() {
+        return error == null;
+    }
+
+    public void setError(String error) {
+        this.error = error;
+    }
+
+    public String getError() {
+        return error;
+    }
+
+    public String getShortError() {
+        return shortError;
+    }
+
+    public void setShortError(String shortError) {
+        this.shortError = shortError;
+    }
+
+    public int getIndex() {
+        return index;
+    }
+
+    public void setIndex(int index) {
+        this.index = index;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/a9cf098f/platforms/catalog/src/main/java/org/apache/camel/catalog/SimpleValidationResult.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/SimpleValidationResult.java
b/platforms/catalog/src/main/java/org/apache/camel/catalog/SimpleValidationResult.java
index 5032758..5c86f23 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/SimpleValidationResult.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/SimpleValidationResult.java
@@ -16,48 +16,17 @@
  */
 package org.apache.camel.catalog;
 
-import java.io.Serializable;
-
-public class SimpleValidationResult implements Serializable {
-
-    private final String simple;
-    private String error;
-    private String shortError;
-    private int index;
+/**
+ * To be backwards compatible, but favor using {@link LanguageValidationResult} instead.
+ */
+public class SimpleValidationResult extends LanguageValidationResult {
 
-    public SimpleValidationResult(String simple) {
-        this.simple = simple;
+    public SimpleValidationResult(String text) {
+        super(text);
     }
 
     public String getSimple() {
-        return simple;
+        return getText();
     }
 
-    public boolean isSuccess() {
-        return error == null;
-    }
-
-    public void setError(String error) {
-        this.error = error;
-    }
-
-    public String getError() {
-        return error;
-    }
-
-    public String getShortError() {
-        return shortError;
-    }
-
-    public void setShortError(String shortError) {
-        this.shortError = shortError;
-    }
-
-    public int getIndex() {
-        return index;
-    }
-
-    public void setIndex(int index) {
-        this.index = index;
-    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9cf098f/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 e08e668..0682026 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
@@ -793,11 +793,11 @@ public class CamelCatalogTest {
 
     @Test
     public void testSimpleExpression() throws Exception {
-        SimpleValidationResult result = catalog.validateSimpleExpression("${body}");
+        SimpleValidationResult result = catalog.validateSimpleExpression(null, "${body}");
         assertTrue(result.isSuccess());
         assertEquals("${body}", result.getSimple());
 
-        result = catalog.validateSimpleExpression("${body");
+        result = catalog.validateSimpleExpression(null, "${body");
         assertFalse(result.isSuccess());
         assertEquals("${body", result.getSimple());
         LOG.info(result.getError());
@@ -808,11 +808,11 @@ public class CamelCatalogTest {
 
     @Test
     public void testSimplePredicate() throws Exception {
-        SimpleValidationResult result = catalog.validateSimplePredicate("${body} == 'abc'");
+        SimpleValidationResult result = catalog.validateSimplePredicate(null, "${body} ==
'abc'");
         assertTrue(result.isSuccess());
         assertEquals("${body} == 'abc'", result.getSimple());
 
-        result = catalog.validateSimplePredicate("${body} > ${header.size");
+        result = catalog.validateSimplePredicate(null, "${body} > ${header.size");
         assertFalse(result.isSuccess());
         assertEquals("${body} > ${header.size", result.getSimple());
         LOG.info(result.getError());
@@ -822,6 +822,29 @@ public class CamelCatalogTest {
     }
 
     @Test
+    public void testValidateLanguage() throws Exception {
+        LanguageValidationResult result = catalog.validateLanguageExpression(null, "simple",
"${body}");
+        assertTrue(result.isSuccess());
+        assertEquals("${body}", result.getText());
+
+        result = catalog.validateLanguageExpression(null, "header", "foo");
+        assertTrue(result.isSuccess());
+        assertEquals("foo", result.getText());
+
+        result = catalog.validateLanguagePredicate(null, "simple", "${body} > 10");
+        assertTrue(result.isSuccess());
+        assertEquals("${body} > 10", result.getText());
+
+        result = catalog.validateLanguagePredicate(null, "header", "bar");
+        assertTrue(result.isSuccess());
+        assertEquals("bar", result.getText());
+
+        result = catalog.validateLanguagePredicate(null, "foobar", "bar");
+        assertFalse(result.isSuccess());
+        assertEquals("Unknown language foobar", result.getError());
+    }
+
+    @Test
     public void testSpringCamelContext() throws Exception {
         String json = catalog.modelJSonSchema("camelContext");
         assertNotNull(json);


Mime
View raw message