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/01/09 15:24:20 UTC
[camel] branch master updated: CAMEL-14383: Bean language add
support for specying the FQN classname via type: prefix which makes it
easier to do from simple/string based expressions.
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 1aa2320 CAMEL-14383: Bean language add support for specying the FQN classname via type: prefix which makes it easier to do from simple/string based expressions.
1aa2320 is described below
commit 1aa2320a70ef11ea931a0fe4df02c911d0473e00
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jan 9 15:22:43 2020 +0100
CAMEL-14383: Bean language add support for specying the FQN classname via type: prefix which makes it easier to do from simple/string based expressions.
---
.../apache/camel/language/bean/BeanExpression.java | 28 +++++++++++++-
core/camel-base/src/main/docs/simple-language.adoc | 3 +-
.../camel/component/bean/BeanStaticMethodTest.java | 45 ++++++++++++++++++++++
3 files changed, 74 insertions(+), 2 deletions(-)
diff --git a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
index 1219fe2..516c131 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
@@ -25,6 +25,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Expression;
import org.apache.camel.ExpressionIllegalSyntaxException;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Predicate;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.component.bean.BeanExpressionProcessor;
@@ -126,6 +127,16 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
}
public Object evaluate(Exchange exchange) {
+ if (bean == null && type == null && beanName != null && beanName.startsWith("type:")) {
+ // its a reference to a fqn class so load the class and use type instead
+ String fqn = beanName.substring(5);
+ try {
+ type = exchange.getContext().getClassResolver().resolveMandatoryClass(fqn);
+ beanName = null;
+ } catch (ClassNotFoundException e) {
+ throw new NoSuchBeanException(beanName, e);
+ }
+ }
// if the bean holder doesn't exist then create it using the context from the exchange
if (beanHolder == null) {
@@ -188,7 +199,18 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
// lets see if we can do additional validation that the bean has valid method during creation of the expression
Object target = bean;
if (bean == null && type == null && beanName != null) {
- target = CamelContextHelper.mandatoryLookup(camelContext, beanName);
+ if (beanName.startsWith("type:")) {
+ // its a reference to a fqn class so load the class and use type instead
+ String fqn = beanName.substring(5);
+ try {
+ type = camelContext.getClassResolver().resolveMandatoryClass(fqn);
+ beanName = null;
+ } catch (ClassNotFoundException e) {
+ throw new NoSuchBeanException(beanName, e);
+ }
+ } else {
+ target = CamelContextHelper.mandatoryLookup(camelContext, beanName);
+ }
}
validateHasMethod(camelContext, target, type, method);
@@ -253,6 +275,10 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
if (bean != null) {
holder = new ConstantBeanHolder(bean, context);
} else if (beanName != null) {
+ // it may refer to a type such as when used with bean language
+ if (context.getRegistry().lookupByName(beanName) == null) {
+
+ }
holder = new RegistryBean(context, beanName);
} else if (type != null) {
holder = new ConstantTypeBeanHolder(type, context);
diff --git a/core/camel-base/src/main/docs/simple-language.adoc b/core/camel-base/src/main/docs/simple-language.adoc
index 96e2fee..bc21e37 100644
--- a/core/camel-base/src/main/docs/simple-language.adoc
+++ b/core/camel-base/src/main/docs/simple-language.adoc
@@ -177,7 +177,8 @@ Command accepts offsets such as: *now-24h* or *in.header.xxx+1h* or even *now+1h
|bean:_bean expression_ |Object |Invoking a bean expression using the xref:components::bean-component.adoc[Bean] language.
Specifying a method name you must use dot as separator. We also support
the ?method=methodname syntax that is used by the xref:components::bean-component.adoc[Bean]
-component.
+component. Camel will by default lookup a bean by the given name. However if you need to refer
+to a bean class then you can prefix with type, such as `bean:type:fqnClassName`.
|`properties:key:default` |String |Lookup a property with the given key. If the key does
not exists or has no value, then an optional default value can be
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanStaticMethodTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanStaticMethodTest.java
new file mode 100644
index 0000000..33631dc
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanStaticMethodTest.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.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class BeanStaticMethodTest extends ContextTestSupport {
+
+ @Test
+ public void testBeanStaticMethod() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .setBody().simple("The host is ${bean:type:org.apache.camel.util.InetAddressUtil?method=getLocalHostNameSafe}")
+ .to("mock:result");
+ }
+ };
+ }
+}