You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/12/11 12:12:54 UTC
[camel] branch master updated: CAMEL-15704: camel-csimple -
Compiled simple language. Fixed bean language resolution issue
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 a8d7b5d CAMEL-15704: camel-csimple - Compiled simple language. Fixed bean language resolution issue
a8d7b5d is described below
commit a8d7b5d237650ffcd2493cdd8fea7586dbda52ba
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Dec 11 13:11:42 2020 +0100
CAMEL-15704: camel-csimple - Compiled simple language. Fixed bean language resolution issue
---
.../language/csimple/joor/CSimpleBeanTest.java | 50 ++++++++++++++++++++++
.../camel/language/csimple/joor/FooBean.java | 24 +++++++++++
.../language/csimple/CSimpleCodeGenerator.java | 15 ++++++-
.../camel/language/csimple/CSimpleHelper.java | 11 +----
.../camel/language/csimple/CSimpleLanguage.java | 8 ++--
.../simple/ast/SimpleFunctionExpression.java | 6 +--
6 files changed, 96 insertions(+), 18 deletions(-)
diff --git a/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/CSimpleBeanTest.java b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/CSimpleBeanTest.java
new file mode 100644
index 0000000..30e2b66
--- /dev/null
+++ b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/CSimpleBeanTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class CSimpleBeanTest extends CamelTestSupport {
+
+ @BindToRegistry
+ private FooBean foo = new FooBean();
+
+ @Test
+ public void testCSimpleBean() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hi World. Camel rocks!", "Hi Scott. Camel rocks!");
+
+ template.sendBody("direct:start", "World");
+ template.sendBody("direct:start", "Scott");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .transform().csimple("${bean:foo?method=doSomething}")
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/FooBean.java b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/FooBean.java
new file mode 100644
index 0000000..7d0e4e4
--- /dev/null
+++ b/components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/FooBean.java
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+public class FooBean {
+
+ public String doSomething(String name) {
+ return "Hi " + name + ". Camel rocks!";
+ }
+}
diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleCodeGenerator.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleCodeGenerator.java
index 0d05a12..605f417 100644
--- a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleCodeGenerator.java
+++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleCodeGenerator.java
@@ -83,6 +83,7 @@ public class CSimpleCodeGenerator {
sb.append("\n");
sb.append("import org.apache.camel.*;\n");
sb.append("import org.apache.camel.util.*;\n");
+ sb.append("import org.apache.camel.spi.*;\n");
sb.append("import static org.apache.camel.language.csimple.CSimpleHelper.*;\n");
sb.append("\n");
// custom imports
@@ -96,7 +97,8 @@ public class CSimpleCodeGenerator {
sb.append("\n");
sb.append("public class ").append(name).append(" extends org.apache.camel.language.csimple.CSimpleSupport {\n");
sb.append("\n");
-
+ sb.append(" Language bean;\n");
+ sb.append("\n");
sb.append(" public ").append(name).append("() {\n");
sb.append(" }\n");
sb.append("\n");
@@ -147,6 +149,17 @@ public class CSimpleCodeGenerator {
}
sb.append("\n");
sb.append(" }\n");
+
+ // only resolve bean language if we use it as camel-bean must then be on the classpath
+ if (script.contains("bean(exchange, bean,")) {
+ sb.append("\n");
+ sb.append(" @Override\n");
+ sb.append(" public void init(CamelContext context) {\n");
+ sb.append(" bean = context.resolveLanguage(\"bean\");\n");
+ sb.append(" }\n");
+ sb.append("\n");
+ }
+
sb.append("}\n");
sb.append("\n");
diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java
index 502ea04..68fa57a 100644
--- a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java
+++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java
@@ -67,7 +67,6 @@ public final class CSimpleHelper {
private static final Pattern OFFSET_PATTERN = Pattern.compile("([+-])([^+-]+)");
private static ExchangeFormatter exchangeFormatter;
- private static Language beanLanguage;
private CSimpleHelper() {
}
@@ -408,7 +407,7 @@ public final class CSimpleHelper {
return ObjectHelper.lookupConstantFieldValue(type, field);
}
- public static Object bean(Exchange exchange, String ref, String method, Object scope) {
+ public static Object bean(Exchange exchange, Language bean, String ref, String method, Object scope) {
Class<?> type = null;
if (ref != null && ref.startsWith("type:")) {
try {
@@ -419,7 +418,6 @@ public final class CSimpleHelper {
}
}
- Language bean = getOrCreateBeanLanguage(exchange.getContext());
Object[] properties = new Object[5];
properties[2] = type;
properties[3] = ref;
@@ -430,13 +428,6 @@ public final class CSimpleHelper {
return exp.evaluate(exchange, Object.class);
}
- private static Language getOrCreateBeanLanguage(CamelContext camelContext) {
- if (beanLanguage == null) {
- beanLanguage = camelContext.resolveLanguage("bean");
- }
- return beanLanguage;
- }
-
public static Object increment(Exchange exchange, Object number) {
Number num = exchange.getContext().getTypeConverter().tryConvertTo(Number.class, exchange, number);
if (num instanceof Integer) {
diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleLanguage.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleLanguage.java
index 705d0bc..5b4709e 100644
--- a/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleLanguage.java
+++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleLanguage.java
@@ -68,11 +68,8 @@ public class CSimpleLanguage extends LanguageSupport implements StaticService {
/**
* For 100% pre-compiled use cases
- *
- * @param compiled the compiled
*/
- private CSimpleLanguage(
- Map<String, CSimpleExpression> compiledPredicates,
+ private CSimpleLanguage(Map<String, CSimpleExpression> compiledPredicates,
Map<String, CSimpleExpression> compiledExpressions) {
this.compiledPredicates = compiledPredicates;
this.compiledExpressions = compiledExpressions;
@@ -140,6 +137,7 @@ public class CSimpleLanguage extends LanguageSupport implements StaticService {
if (answer == null && compilationSupport != null) {
CSimpleExpression exp = compilationSupport.compilePredicate(getCamelContext(), expression);
if (exp != null) {
+ exp.init(getCamelContext());
compiledPredicates.put(text, exp);
answer = exp;
}
@@ -174,6 +172,7 @@ public class CSimpleLanguage extends LanguageSupport implements StaticService {
if (answer == null && compilationSupport != null) {
CSimpleExpression exp = compilationSupport.compileExpression(getCamelContext(), expression);
if (exp != null) {
+ exp.init(getCamelContext());
compiledExpressions.put(text, exp);
answer = exp;
}
@@ -295,6 +294,7 @@ public class CSimpleLanguage extends LanguageSupport implements StaticService {
Class<CSimpleExpression> clazz
= ecc.getClassResolver().resolveMandatoryClass(fqn, CSimpleExpression.class);
CSimpleExpression ce = clazz.getConstructor().newInstance();
+ ce.init(getCamelContext());
if (ce.isPredicate()) {
compiledPredicates.put(ce.getText(), ce);
} else {
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 af823d8..8fb4156 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
@@ -707,11 +707,11 @@ public class SimpleFunctionExpression extends LiteralExpression {
}
ref = ref.trim();
if (method != null && scope != null) {
- return "bean(exchange, \"" + ref + "\", \"" + method.toString() + "\", \"" + scope.toString() + "\")";
+ return "bean(exchange, bean, \"" + ref + "\", \"" + method.toString() + "\", \"" + scope.toString() + "\")";
} else if (method != null) {
- return "bean(exchange, \"" + ref + "\", \"" + method.toString() + "\", null)";
+ return "bean(exchange, bean, \"" + ref + "\", \"" + method.toString() + "\", null)";
} else {
- return "bean(exchange, \"" + ref + "\", null, null)";
+ return "bean(exchange, bean, \"" + ref + "\", null, null)";
}
}