camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/7] camel git commit: Camel catalog - Add api to validate simple language
Date Fri, 01 Jan 2016 10:23:53 GMT
Camel catalog - Add api to validate simple language


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

Branch: refs/heads/master
Commit: 635ab04bc75624859849a3dd76b93463efb29f8c
Parents: 0b82cfb
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Dec 31 17:29:11 2015 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Jan 1 11:22:34 2016 +0100

----------------------------------------------------------------------
 platforms/catalog/pom.xml                       |  7 +++
 .../org/apache/camel/catalog/CamelCatalog.java  | 20 +++++++++
 .../camel/catalog/DefaultCamelCatalog.java      | 40 +++++++++++++++++
 .../camel/catalog/SimpleValidationResult.java   | 45 ++++++++++++++++++++
 .../apache/camel/catalog/CamelCatalogTest.java  | 26 +++++++++++
 5 files changed, 138 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/635ab04b/platforms/catalog/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/catalog/pom.xml b/platforms/catalog/pom.xml
index 94321d2..bfd5019 100644
--- a/platforms/catalog/pom.xml
+++ b/platforms/catalog/pom.xml
@@ -47,6 +47,13 @@
       <scope>test</scope>
     </dependency>
 
+    <!-- for testing simple language parser -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>

http://git-wip-us.apache.org/repos/asf/camel/blob/635ab04b/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 5fedead..4d3f5e4 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
@@ -199,6 +199,26 @@ public interface CamelCatalog {
     EndpointValidationResult validateEndpointProperties(String uri);
 
     /**
+     * Parses and validates the simple expression.
+     * <p/>
+     * <b>Important:</b> This requires having <tt>camel-core</tt>
on the classpath
+     *
+     * @param simple  the simple expression
+     * @return validation result
+     */
+    SimpleValidationResult validateSimpleExpression(String simple);
+
+    /**
+     * Parses and validates the simple predicate
+     * <p/>
+     * <b>Important:</b> This requires having <tt>camel-core</tt>
on the classpath
+     *
+     * @param simple  the simple predicate
+     * @return validation result
+     */
+    SimpleValidationResult validateSimplePredicate(String simple);
+
+    /**
      * Returns the component name from the given endpoint uri
      *
      * @param uri  the endpoint uri

http://git-wip-us.apache.org/repos/asf/camel/blob/635ab04b/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 3cde7ba..607a7e5 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
@@ -19,6 +19,7 @@ package org.apache.camel.catalog;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
@@ -1200,6 +1201,45 @@ public class DefaultCamelCatalog implements CamelCatalog {
         return sb.toString();
     }
 
+    @Override
+    public SimpleValidationResult validateSimpleExpression(String simple) {
+        return doValidateSimple(simple, false);
+    }
+
+    @Override
+    public SimpleValidationResult validateSimplePredicate(String simple) {
+        return doValidateSimple(simple, true);
+    }
+
+    private SimpleValidationResult doValidateSimple(String simple, boolean predicate) {
+        SimpleValidationResult answer = new SimpleValidationResult(simple);
+
+        Object instance = null;
+        Class clazz = null;
+        try {
+            clazz = DefaultCamelCatalog.class.getClassLoader().loadClass("org.apache.camel.language.simple.SimpleLanguage");
+            instance = clazz.newInstance();
+        } catch (Exception e) {
+            // ignore
+        }
+
+        if (clazz != null && instance != null) {
+            try {
+                if (predicate) {
+                    instance.getClass().getMethod("createPredicate", String.class).invoke(instance,
simple);
+                } else {
+                    instance.getClass().getMethod("createExpression", String.class).invoke(instance,
simple);
+                }
+            } catch (InvocationTargetException e) {
+                answer.setError(e.getTargetException().getMessage());
+            } catch (Exception e) {
+                answer.setError(e.getMessage());
+            }
+        }
+
+        return answer;
+    }
+
     /**
      * Special logic for log endpoints to deal when showAll=true
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/635ab04b/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
new file mode 100644
index 0000000..cd11ef0
--- /dev/null
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/SimpleValidationResult.java
@@ -0,0 +1,45 @@
+/**
+ * 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;
+
+public class SimpleValidationResult implements Serializable {
+
+    private final String simple;
+    private String error;
+
+    public SimpleValidationResult(String simple) {
+        this.simple = simple;
+    }
+
+    public String getSimple() {
+        return simple;
+    }
+
+    public boolean isSuccess() {
+        return error == null;
+    }
+
+    public void setError(String error) {
+        this.error = error;
+    }
+
+    public String getError() {
+        return error;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/635ab04b/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 0bd8d3b..259272b 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
@@ -566,4 +566,30 @@ public class CamelCatalogTest {
         assertNotNull(tree);
     }
 
+    @Test
+    public void testSimpleExpression() throws Exception {
+        SimpleValidationResult result = catalog.validateSimpleExpression("${body}");
+        assertTrue(result.isSuccess());
+        assertEquals("${body}", result.getSimple());
+
+        result = catalog.validateSimpleExpression("${body");
+        assertFalse(result.isSuccess());
+        assertEquals("${body", result.getSimple());
+        LOG.info(result.getError());
+        assertTrue(result.getError().startsWith("expected symbol functionEnd but was eol
at location 5"));
+    }
+
+    @Test
+    public void testSimplePredicate() throws Exception {
+        SimpleValidationResult result = catalog.validateSimplePredicate("${body} == 'abc'");
+        assertTrue(result.isSuccess());
+        assertEquals("${body} == 'abc'", result.getSimple());
+
+        result = catalog.validateSimplePredicate("${body} > ${header.size");
+        assertFalse(result.isSuccess());
+        assertEquals("${body} > ${header.size", result.getSimple());
+        LOG.info(result.getError());
+        assertTrue(result.getError().startsWith("expected symbol functionEnd but was eol
at location 22"));
+    }
+
 }


Mime
View raw message