This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 74fcffe CAMEL-15704: camel-csimple - Compiled simple language. 74fcffe is described below commit 74fcffe9016abb0b09b8fcae8f943167eb35138d Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Nov 27 09:03:00 2020 +0100 CAMEL-15704: camel-csimple - Compiled simple language. --- .../camel/catalog/docs/csimple-language.adoc | 14 ++++---- .../camel/language/csimple/joor/Constants.java | 31 +++++++++++++++++ .../language/csimple/joor/OriginalSimpleTest.java | 40 +++++++++------------- .../modules/languages/pages/csimple-language.adoc | 14 ++++---- .../camel/language/csimple/CSimpleSupport.java | 6 ++-- .../simple/ast/SimpleFunctionExpression.java | 8 ++++- .../csimple/CSimplePredicateParserTest.java | 6 ++-- .../org/apache/camel/util/ObjectHelperTest.java | 4 +-- .../java/org/apache/camel/util/ObjectHelper.java | 7 ++-- .../java/org/apache/camel/util/OgnlHelper.java | 4 +++ .../modules/languages/pages/csimple-language.adoc | 14 ++++---- 11 files changed, 92 insertions(+), 56 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/csimple-language.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/csimple-language.adoc index d9a5a89..3c2bffd 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/csimple-language.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/csimple-language.adoc @@ -22,7 +22,7 @@ The simple langauge is generally very lightweight and fast, however for some use then the simple language does runtime introspection and reflection calls. This has an overhead on performance, and was one of the reasons why csimple was created. -Csimple requires to be typesafe and method calls via OGNL paths requires to know the type during parsing. +The csimple language requires to be typesafe and method calls via OGNL paths requires to know the type during parsing. This means for csimple languages expressions you would need to provide the class type in the script, where as simple introspects this at runtime. @@ -41,17 +41,15 @@ There are two ways to compile csimple === Using camel-csimple-maven-plugin -You should either use the `camel-csimple-maven-plugin` to use the Maven plugin to discover all the csimple scripts from the source code, and then automatic generate source code in the `src/generated/java` folder, which then gets compiled together with all the other sources. +The `camel-csimple-maven-plugin` Maven plugin is used for discoving all the csimple scripts from the source code, and then automatic generate source code in the `src/generated/java` folder, which then gets compiled together with all the other sources. The maven plugin will do source code scanning of `.java` and `.xml` files (Java and XML DSL). -The scanner is limited to detect certain code patterns, and it may miss discovering some csimple scripts if they are being used in unusual/rare ways. +The scanner limits to detect certain code patterns, and it may miss discovering some csimple scripts if they are being used in unusual/rare ways. The runtime compilation using `camel-csimple-joor` does not have this limitation. -However the pros is that all of the csimple scripts will be compiled using the regular Java compiler and therefore everything is included out of the box as `.class` files in the application JAR file. -And no additional dependencies is needed at runtime. - -See the `camel-example-csimple` example at https://github.com/apache/camel-examples[Camel Examples] which uses the maven plugin. +However the pros is that all of the csimple scripts will be compiled using the regular Java compiler and therefore everything +is included out of the box as `.class` files in the application JAR file, and no additional dependencies is required at runtime. To use `camel-csimple-maven-plugin` you need to add it to your `pom.xml` file as shown: @@ -103,6 +101,8 @@ To use `camel-csimple-maven-plugin` you need to add it to your `pom.xml` file as And then you must also add the `build-helper-maven-plugin` Maven plugin to include `src/generated` to the list of source folders for the Java compiler, to ensure the generated source code is compiled and included in the application JAR file. +See the `camel-example-csimple` example at https://github.com/apache/camel-examples[Camel Examples] which uses the maven plugin. + === Using camel-csimple-joor The jOOR library integrates with the Java compiler and performs runtime compilation of Java code. diff --git a/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/Constants.java b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/Constants.java new file mode 100644 index 0000000..a5b894f --- /dev/null +++ b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/Constants.java @@ -0,0 +1,31 @@ +/* + * 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.language.csimple.joor; + +// CHECKSTYLE:OFF +// we want to use the code as-is as that is how end users may code +public class Constants { + + public static String BAR = "456"; + + public static class MyInnerStuff { + + public static String FOO = "123"; + + } +} +// CHECKSTYLE:ON diff --git a/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java index 42333d2..ba10ddd 100644 --- a/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java +++ b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java @@ -598,8 +598,8 @@ public class OriginalSimpleTest extends LanguageTestSupport { try { assertExpression("${date:yyyyMMdd}", "19740420"); fail("Should thrown an exception"); - } catch (IllegalArgumentException e) { - assertEquals("Command not supported for dateExpression: yyyyMMdd", e.getMessage()); + } catch (Exception e) { + assertEquals("Command not supported for dateExpression: yyyyMMdd", e.getCause().getMessage()); } } @@ -805,14 +805,14 @@ public class OriginalSimpleTest extends LanguageTestSupport { } @Test - public void testOnglOnHeadersWithBracket() throws Exception { - assertOnglOnHeadersWithSquareBrackets("order"); - assertOnglOnHeadersWithSquareBrackets("purchase.order"); - assertOnglOnHeadersWithSquareBrackets("foo.bar.qux"); - assertOnglOnHeadersWithSquareBrackets("purchase order"); + public void testOgnlOnHeadersWithBracket() throws Exception { + assertOgnlOnHeadersWithSquareBrackets("order"); + assertOgnlOnHeadersWithSquareBrackets("purchase.order"); + assertOgnlOnHeadersWithSquareBrackets("foo.bar.qux"); + assertOgnlOnHeadersWithSquareBrackets("purchase order"); } - private void assertOnglOnHeadersWithSquareBrackets(String key) { + private void assertOgnlOnHeadersWithSquareBrackets(String key) { exchange.getIn().setHeader(key, new OrderLine(123, "Camel in Action")); assertExpression("${headers[" + key + "].name}", "Camel in Action"); assertExpression("${in.headers[" + key + "].name}", "Camel in Action"); @@ -820,14 +820,14 @@ public class OriginalSimpleTest extends LanguageTestSupport { } @Test - public void testOnglOnExchangePropertiesWithBracket() throws Exception { - assertOnglOnExchangePropertiesWithBracket("order"); - assertOnglOnExchangePropertiesWithBracket("purchase.order"); - assertOnglOnExchangePropertiesWithBracket("foo.bar.qux"); - assertOnglOnExchangePropertiesWithBracket("purchase order"); + public void testOgnlOnExchangePropertiesWithBracket() throws Exception { + assertOgnlOnExchangePropertiesWithBracket("order"); + assertOgnlOnExchangePropertiesWithBracket("purchase.order"); + assertOgnlOnExchangePropertiesWithBracket("foo.bar.qux"); + assertOgnlOnExchangePropertiesWithBracket("purchase order"); } - public void assertOnglOnExchangePropertiesWithBracket(String key) throws Exception { + public void assertOgnlOnExchangePropertiesWithBracket(String key) throws Exception { exchange.setProperty(key, new OrderLine(123, "Camel in Action")); assertExpression("${exchangeProperty[" + key + "].name}", "Camel in Action"); assertExpression("${exchangeProperty[\"" + key + "\"].name}", "Camel in Action"); @@ -1548,13 +1548,6 @@ public class OriginalSimpleTest extends LanguageTestSupport { } @Test - public void testCamelContextStartRoute() throws Exception { - exchange.getIn().setBody(null); - - assertExpression("${camelContext.getRouteController().startRoute(\"foo\")}", null); - } - - @Test public void testBodyOgnlReplace() throws Exception { exchange.getIn().setBody("Kamel is a cool Kamel"); @@ -1703,8 +1696,9 @@ public class OriginalSimpleTest extends LanguageTestSupport { @Test public void testTypeConstantInnerClass() throws Exception { - assertExpression("${type:org.apache.camel.language.simple.Constants$MyInnerStuff.FOO}", 123); - assertExpression("${type:org.apache.camel.language.simple.Constants.BAR}", 456); + assertExpression("${type:org.apache.camel.language.csimple.joor.Constants$MyInnerStuff.FOO}", 123); + assertExpression("${type:org.apache.camel.language.csimple.joor.Constants.MyInnerStuff.FOO}", 123); + assertExpression("${type:org.apache.camel.language.csimple.joor.Constants.BAR}", 456); } @Test diff --git a/core/camel-core-languages/src/main/docs/modules/languages/pages/csimple-language.adoc b/core/camel-core-languages/src/main/docs/modules/languages/pages/csimple-language.adoc index d9a5a89..3c2bffd 100644 --- a/core/camel-core-languages/src/main/docs/modules/languages/pages/csimple-language.adoc +++ b/core/camel-core-languages/src/main/docs/modules/languages/pages/csimple-language.adoc @@ -22,7 +22,7 @@ The simple langauge is generally very lightweight and fast, however for some use then the simple language does runtime introspection and reflection calls. This has an overhead on performance, and was one of the reasons why csimple was created. -Csimple requires to be typesafe and method calls via OGNL paths requires to know the type during parsing. +The csimple language requires to be typesafe and method calls via OGNL paths requires to know the type during parsing. This means for csimple languages expressions you would need to provide the class type in the script, where as simple introspects this at runtime. @@ -41,17 +41,15 @@ There are two ways to compile csimple === Using camel-csimple-maven-plugin -You should either use the `camel-csimple-maven-plugin` to use the Maven plugin to discover all the csimple scripts from the source code, and then automatic generate source code in the `src/generated/java` folder, which then gets compiled together with all the other sources. +The `camel-csimple-maven-plugin` Maven plugin is used for discoving all the csimple scripts from the source code, and then automatic generate source code in the `src/generated/java` folder, which then gets compiled together with all the other sources. The maven plugin will do source code scanning of `.java` and `.xml` files (Java and XML DSL). -The scanner is limited to detect certain code patterns, and it may miss discovering some csimple scripts if they are being used in unusual/rare ways. +The scanner limits to detect certain code patterns, and it may miss discovering some csimple scripts if they are being used in unusual/rare ways. The runtime compilation using `camel-csimple-joor` does not have this limitation. -However the pros is that all of the csimple scripts will be compiled using the regular Java compiler and therefore everything is included out of the box as `.class` files in the application JAR file. -And no additional dependencies is needed at runtime. - -See the `camel-example-csimple` example at https://github.com/apache/camel-examples[Camel Examples] which uses the maven plugin. +However the pros is that all of the csimple scripts will be compiled using the regular Java compiler and therefore everything +is included out of the box as `.class` files in the application JAR file, and no additional dependencies is required at runtime. To use `camel-csimple-maven-plugin` you need to add it to your `pom.xml` file as shown: @@ -103,6 +101,8 @@ To use `camel-csimple-maven-plugin` you need to add it to your `pom.xml` file as And then you must also add the `build-helper-maven-plugin` Maven plugin to include `src/generated` to the list of source folders for the Java compiler, to ensure the generated source code is compiled and included in the application JAR file. +See the `camel-example-csimple` example at https://github.com/apache/camel-examples[Camel Examples] which uses the maven plugin. + === Using camel-csimple-joor The jOOR library integrates with the Java compiler and performs runtime compilation of Java code. diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleSupport.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleSupport.java index 9cf6e72..371cbb9 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleSupport.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleSupport.java @@ -20,6 +20,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.ExpressionEvaluationException; import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.util.ObjectHelper; /** * Base class for source code generateed csimple expressions. @@ -54,10 +55,7 @@ public abstract class CSimpleSupport implements CSimpleExpression, CSimpleMethod } catch (Exception e) { throw new ExpressionEvaluationException(this, exchange, e); } - if (out instanceof String && ((String) out).trim().isEmpty()) { - return false; - } - return camelContext.getTypeConverter().convertTo(boolean.class, exchange, out); + return ObjectHelper.evaluateValuePredicate(out); } } diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java index f4dcc52..d3c0863 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java @@ -575,7 +575,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (invalid) { throw new SimpleParserException("Valid syntax: ${camelContext.OGNL} was: " + function, token.getIndex()); } - return "camelContext" + ognlCodeMethods(remainder); + return "context" + ognlCodeMethods(remainder); } // ExceptionAs OGNL @@ -587,6 +587,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!type.endsWith(".class")) { type = type + ".class"; } + type = type.replace('$', '.'); type = type.trim(); boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder); if (type.isEmpty() || invalid) { @@ -626,6 +627,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!type.endsWith(".class")) { type = type + ".class"; } + type = type.replace('$', '.'); type = type.trim(); return "exchangePropertyAs(exchange, \"" + key + "\", " + type + ")" + ognlCodeMethods(remainder); } @@ -794,6 +796,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!type.endsWith(".class")) { type += ".class"; } + type = type.replace('$', '.'); if (field != null) { return "type(exchange, " + type + ", \"" + field + "\")"; } else { @@ -856,6 +859,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!type.endsWith(".class")) { type = type + ".class"; } + type = type.replace('$', '.'); type = type.trim(); remainder = StringHelper.after(remainder, ")"); if (ObjectHelper.isNotEmpty(remainder)) { @@ -880,6 +884,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!type.endsWith(".class")) { type = type + ".class"; } + type = type.replace('$', '.'); type = type.trim(); remainder = StringHelper.after(remainder, ")"); if (ObjectHelper.isNotEmpty(remainder)) { @@ -929,6 +934,7 @@ public class SimpleFunctionExpression extends LiteralExpression { if (!type.endsWith(".class")) { type = type + ".class"; } + type = type.replace('$', '.'); type = type.trim(); return "headerAs(message, \"" + key + "\", " + type + ")" + ognlCodeMethods(remainder); } diff --git a/core/camel-core/src/test/java/org/apache/camel/language/csimple/CSimplePredicateParserTest.java b/core/camel-core/src/test/java/org/apache/camel/language/csimple/CSimplePredicateParserTest.java index c77aeed..c16dc3c 100644 --- a/core/camel-core/src/test/java/org/apache/camel/language/csimple/CSimplePredicateParserTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/language/csimple/CSimplePredicateParserTest.java @@ -57,13 +57,13 @@ public class CSimplePredicateParserTest { code); code = parser.parsePredicate("${camelContext.getName()} == 'myCamel'"); - Assertions.assertEquals("isEqualTo(exchange, camelContext.getName(), \"myCamel\")", code); + Assertions.assertEquals("isEqualTo(exchange, context.getName(), \"myCamel\")", code); code = parser.parsePredicate("${camelContext.name} == 'myCamel'"); - Assertions.assertEquals("isEqualTo(exchange, camelContext.getName(), \"myCamel\")", code); + Assertions.assertEquals("isEqualTo(exchange, context.getName(), \"myCamel\")", code); code = parser.parsePredicate("${camelContext.inflightRepository.size()} > 0"); - Assertions.assertEquals("isGreaterThan(exchange, camelContext.getInflightRepository().size(), 0)", code); + Assertions.assertEquals("isGreaterThan(exchange, context.getInflightRepository().size(), 0)", code); } @Test diff --git a/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java index 3dad610..54b2089 100644 --- a/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java @@ -701,8 +701,8 @@ public class ObjectHelperTest { assertEquals(false, org.apache.camel.util.ObjectHelper.evaluateValuePredicate("false")); assertEquals(false, org.apache.camel.util.ObjectHelper.evaluateValuePredicate("FALSE")); assertEquals(true, org.apache.camel.util.ObjectHelper.evaluateValuePredicate("foobar")); - assertEquals(true, org.apache.camel.util.ObjectHelper.evaluateValuePredicate("")); - assertEquals(true, org.apache.camel.util.ObjectHelper.evaluateValuePredicate(" ")); + assertEquals(false, org.apache.camel.util.ObjectHelper.evaluateValuePredicate("")); + assertEquals(false, org.apache.camel.util.ObjectHelper.evaluateValuePredicate(" ")); List<String> list = new ArrayList<>(); assertEquals(false, org.apache.camel.util.ObjectHelper.evaluateValuePredicate(list)); diff --git a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java index e0354e3..618dba5 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -999,9 +999,12 @@ public final class ObjectHelper { if (value instanceof Boolean) { return (Boolean) value; } else if (value instanceof String) { - if ("true".equalsIgnoreCase((String) value)) { + String str = ((String) value).trim(); + if (str.isEmpty()) { + return false; + } else if ("true".equalsIgnoreCase(str)) { return true; - } else if ("false".equalsIgnoreCase((String) value)) { + } else if ("false".equalsIgnoreCase(str)) { return false; } } else if (value instanceof NodeList) { diff --git a/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java index c10f5d1..b10d7ae 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java @@ -56,6 +56,10 @@ public final class OgnlHelper { } public static boolean isInvalidValidOgnlExpression(String expression) { + if (expression == null) { + return false; + } + if (expression.indexOf('.') == -1 && expression.indexOf('[') == -1 && expression.indexOf(']') == -1) { return false; } diff --git a/docs/components/modules/languages/pages/csimple-language.adoc b/docs/components/modules/languages/pages/csimple-language.adoc index 821828f..653d775 100644 --- a/docs/components/modules/languages/pages/csimple-language.adoc +++ b/docs/components/modules/languages/pages/csimple-language.adoc @@ -24,7 +24,7 @@ The simple langauge is generally very lightweight and fast, however for some use then the simple language does runtime introspection and reflection calls. This has an overhead on performance, and was one of the reasons why csimple was created. -Csimple requires to be typesafe and method calls via OGNL paths requires to know the type during parsing. +The csimple language requires to be typesafe and method calls via OGNL paths requires to know the type during parsing. This means for csimple languages expressions you would need to provide the class type in the script, where as simple introspects this at runtime. @@ -43,17 +43,15 @@ There are two ways to compile csimple === Using camel-csimple-maven-plugin -You should either use the `camel-csimple-maven-plugin` to use the Maven plugin to discover all the csimple scripts from the source code, and then automatic generate source code in the `src/generated/java` folder, which then gets compiled together with all the other sources. +The `camel-csimple-maven-plugin` Maven plugin is used for discoving all the csimple scripts from the source code, and then automatic generate source code in the `src/generated/java` folder, which then gets compiled together with all the other sources. The maven plugin will do source code scanning of `.java` and `.xml` files (Java and XML DSL). -The scanner is limited to detect certain code patterns, and it may miss discovering some csimple scripts if they are being used in unusual/rare ways. +The scanner limits to detect certain code patterns, and it may miss discovering some csimple scripts if they are being used in unusual/rare ways. The runtime compilation using `camel-csimple-joor` does not have this limitation. -However the pros is that all of the csimple scripts will be compiled using the regular Java compiler and therefore everything is included out of the box as `.class` files in the application JAR file. -And no additional dependencies is needed at runtime. - -See the `camel-example-csimple` example at https://github.com/apache/camel-examples[Camel Examples] which uses the maven plugin. +However the pros is that all of the csimple scripts will be compiled using the regular Java compiler and therefore everything +is included out of the box as `.class` files in the application JAR file, and no additional dependencies is required at runtime. To use `camel-csimple-maven-plugin` you need to add it to your `pom.xml` file as shown: @@ -105,6 +103,8 @@ To use `camel-csimple-maven-plugin` you need to add it to your `pom.xml` file as And then you must also add the `build-helper-maven-plugin` Maven plugin to include `src/generated` to the list of source folders for the Java compiler, to ensure the generated source code is compiled and included in the application JAR file. +See the `camel-example-csimple` example at https://github.com/apache/camel-examples[Camel Examples] which uses the maven plugin. + === Using camel-csimple-joor The jOOR library integrates with the Java compiler and performs runtime compilation of Java code.