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 2021/05/10 09:24:55 UTC
[camel] 02/02: CAMEL-16596: ScriptingLanguage SPI.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch CAMEL-16593
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 50f24819d1310f45eee36f58ca6978fcab65baaf
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon May 10 11:23:48 2021 +0200
CAMEL-16596: ScriptingLanguage SPI.
---
.../camel-kamelet/src/test/resources/mybar.groovy | 4 ++--
components/camel-kamelet/src/test/resources/mybar.joor | 4 ++--
.../main/java/org/apache/camel/impl/DefaultModel.java | 3 ++-
.../org/apache/camel/model/templateBeanFactory.json | 2 +-
docs/user-manual/modules/ROOT/pages/route-template.adoc | 17 +++++++++++++++++
5 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/components/camel-kamelet/src/test/resources/mybar.groovy b/components/camel-kamelet/src/test/resources/mybar.groovy
index a62ab64..68354995 100644
--- a/components/camel-kamelet/src/test/resources/mybar.groovy
+++ b/components/camel-kamelet/src/test/resources/mybar.groovy
@@ -17,6 +17,6 @@
import org.apache.camel.component.kamelet.MyInjectBar
def bean = new MyInjectBar()
-// context is RouteTemplateContext
-bean.bar = context.getProperty('bar')
+// rtc is RouteTemplateContext
+bean.bar = rtc.getProperty('bar')
return bean
diff --git a/components/camel-kamelet/src/test/resources/mybar.joor b/components/camel-kamelet/src/test/resources/mybar.joor
index be7a484..eeaae06 100644
--- a/components/camel-kamelet/src/test/resources/mybar.joor
+++ b/components/camel-kamelet/src/test/resources/mybar.joor
@@ -1,4 +1,4 @@
var bean = new org.apache.camel.component.kamelet.MyInjectBar();
-// context is RouteTemplateContext
-bean.setBar(context.getProperty('bar', String.class));
+// rtc is RouteTemplateContext
+bean.setBar(rtc.getProperty('bar', String.class));
return bean;
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
index 8266c1a..c6e7c6d 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
@@ -348,7 +348,8 @@ public class DefaultModel implements Model {
// scripting language should be evaluated with route template context as binding
routeTemplateContext.bind(b.getName(), clazz, () -> {
Map<String, Object> bindings = new HashMap<>();
- bindings.put("context", routeTemplateContext);
+ // use rtx as the short-hand name, as context would imply its CamelContext
+ bindings.put("rtc", routeTemplateContext);
return slan.evaluate(script, bindings, clazz);
});
} else {
diff --git a/core/camel-core-model/src/generated/resources/org/apache/camel/model/templateBeanFactory.json b/core/camel-core-model/src/generated/resources/org/apache/camel/model/templateBeanFactory.json
index 4bd12f5..538a54b 100644
--- a/core/camel-core-model/src/generated/resources/org/apache/camel/model/templateBeanFactory.json
+++ b/core/camel-core-model/src/generated/resources/org/apache/camel/model/templateBeanFactory.json
@@ -11,7 +11,7 @@
"output": false
},
"properties": {
- "language": { "kind": "attribute", "displayName": "Language", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "bean", "groovy", "joor", "language", "mvel", "ognl", "spel" ], "deprecated": false, "autowired": false, "secret": false, "description": "The language to use for creating the bean (such as groovy, joor)" },
+ "language": { "kind": "attribute", "displayName": "Language", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "bean", "groovy", "joor", "language", "mvel", "ognl" ], "deprecated": false, "autowired": false, "secret": false, "description": "The language to use for creating the bean (such as groovy, joor)" },
"script": { "kind": "value", "displayName": "Script", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The script to execute that creates the bean. If the script use the prefix resource: such as resource:classpath:com\/foo\/myscript.groovy, resource:file:\/var\/myscript.groovy, then its loaded from the external resource." }
}
}
diff --git a/docs/user-manual/modules/ROOT/pages/route-template.adoc b/docs/user-manual/modules/ROOT/pages/route-template.adoc
index 1606390..66cee9c 100644
--- a/docs/user-manual/modules/ROOT/pages/route-template.adoc
+++ b/docs/user-manual/modules/ROOT/pages/route-template.adoc
@@ -319,6 +319,23 @@ The languages supported are:
| ognl | To use OGNL template script to create the bean.
|===
+Camel will bind `RouteTemplateContext` as the root object with name `rtc` when evaluating the script.
+This means you can get access to all the information from `RouteTemplateContext` and `CamelContext` via `rtc`.
+
+This is what we have done in the scripts in the previous examples where we get hold of a template parameter with:
+
+[source,groovy]
+----
+ rtc.getProperty('region', String.class)
+----
+
+To get access to `CamelContext` you can do:
+
+[source,groovy]
+----
+ var cn = rtc.getCamelContext().getName()
+----
+
The most powerful languages to use are groovy and joor. The other languages are limited in flexibility
as they are not complete programming languages, but are more suited for templating needs.