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 2010/01/16 13:49:39 UTC
svn commit: r899932 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/builder/
main/java/org/apache/camel/model/language/
test/java/org/apache/camel/component/bean/issues/
Author: davsclaus
Date: Sat Jan 16 12:49:38 2010
New Revision: 899932
URL: http://svn.apache.org/viewvc?rev=899932&view=rev
Log:
CAMEL-2368: Builder using bean now accepts a bean instance. Method call expression as well.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanNonRegistryTest.java
- copied, changed from r899913, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanLanguageNonRegistryTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanRegistryTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java?rev=899932&r1=899931&r2=899932&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java Sat Jan 16 12:49:38 2010
@@ -36,26 +36,34 @@
/**
* Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
- * value builder
+ * value builder.
+ * <p/>
+ * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
*
- * @param beanRef reference to bean to lookup in the Registry
+ * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
* @return the builder
*/
- public static ValueBuilder bean(String beanRef) {
- Expression expression = ExpressionBuilder.beanExpression(beanRef);
- return new ValueBuilder(expression);
+ public static ValueBuilder bean(final Object beanOrBeanRef) {
+ return bean(beanOrBeanRef, null);
}
/**
* Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
- * value builder
+ * value builder.
+ * <p/>
+ * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
*
- * @param beanRef reference to bean to lookup in the Registry
- * @param method name of method to invoke
+ * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
+ * @param method the method name
* @return the builder
*/
- public static ValueBuilder bean(String beanRef, String method) {
- Expression expression = ExpressionBuilder.beanExpression(beanRef, method);
+ public static ValueBuilder bean(Object beanOrBeanRef, String method) {
+ Expression expression;
+ if (beanOrBeanRef instanceof String) {
+ expression = ExpressionBuilder.beanExpression((String) beanOrBeanRef, method);
+ } else {
+ expression = ExpressionBuilder.beanExpression(beanOrBeanRef, method);
+ }
return new ValueBuilder(expression);
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java?rev=899932&r1=899931&r2=899932&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java Sat Jan 16 12:49:38 2010
@@ -131,24 +131,28 @@
/**
* Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
* value builder
+ * <p/>
+ * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
*
- * @param beanRef reference to bean to lookup in the Registry
+ * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
* @return the builder
*/
- public ValueBuilder bean(String beanRef) {
- return Builder.bean(beanRef, null);
+ public ValueBuilder bean(Object beanOrBeanRef) {
+ return bean(beanOrBeanRef, null);
}
/**
* Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a>
* value builder
+ * <p/>
+ * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
*
- * @param beanRef reference to bean to lookup in the Registry
+ * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
* @param method name of method to invoke
* @return the builder
*/
- public ValueBuilder bean(String beanRef, String method) {
- return Builder.bean(beanRef, method);
+ public ValueBuilder bean(Object beanOrBeanRef, String method) {
+ return Builder.bean(beanOrBeanRef, method);
}
/**
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=899932&r1=899931&r2=899932&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Sat Jan 16 12:49:38 2010
@@ -928,6 +928,10 @@
return BeanLanguage.bean(beanType, methodName);
}
+ public static Expression beanExpression(final Object bean, final String methodName) {
+ return BeanLanguage.bean(bean, methodName);
+ }
+
public static Expression beanExpression(final String beanRef, final String methodName) {
String expression = methodName != null ? beanRef + "." + methodName : beanRef;
return beanExpression(expression);
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java?rev=899932&r1=899931&r2=899932&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java Sat Jan 16 12:49:38 2010
@@ -179,6 +179,21 @@
* href="http://camel.apache.org/bean-language.html>bean language</a>
* which basically means the bean is invoked to determine the expression
* value.
+ *
+ * @param instance the instance of the bean
+ * @return the builder to continue processing the DSL
+ */
+ public T method(Object instance) {
+ MethodCallExpression expression = new MethodCallExpression(instance);
+ setExpressionType(expression);
+ return result;
+ }
+
+ /**
+ * Evaluates an expression using the <a
+ * href="http://camel.apache.org/bean-language.html>bean language</a>
+ * which basically means the bean is invoked to determine the expression
+ * value.
*
* @param beanType the Class of the bean which we want to invoke
* @return the builder to continue processing the DSL
@@ -210,6 +225,22 @@
* href="http://camel.apache.org/bean-language.html>bean language</a>
* which basically means the bean is invoked to determine the expression
* value.
+ *
+ * @param instance the instance of the bean
+ * @param method the name of the method to invoke on the bean
+ * @return the builder to continue processing the DSL
+ */
+ public T method(Object instance, String method) {
+ MethodCallExpression expression = new MethodCallExpression(instance, method);
+ setExpressionType(expression);
+ return result;
+ }
+
+ /**
+ * Evaluates an expression using the <a
+ * href="http://camel.apache.org/bean-language.html>bean language</a>
+ * which basically means the bean is invoked to determine the expression
+ * value.
*
* @param beanType the Class of the bean which we want to invoke
* @param method the name of the method to invoke on the bean
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java?rev=899932&r1=899931&r2=899932&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java Sat Jan 16 12:49:38 2010
@@ -44,6 +44,8 @@
@XmlTransient
// we don't need to support the beanType class in Spring
private Class<?> beanType;
+ @XmlTransient
+ private Object instance;
public MethodCallExpression() {
@@ -58,6 +60,17 @@
this.method = method;
}
+ public MethodCallExpression(Object instance) {
+ super(instance.getClass().getName());
+ this.instance = instance;
+ }
+
+ public MethodCallExpression(Object instance, String method) {
+ super(instance.getClass().getName());
+ this.instance = instance;
+ this.method = method;
+ }
+
public MethodCallExpression(Class<?> type) {
super(type.toString());
this.beanType = type;
@@ -86,8 +99,10 @@
public Expression createExpression(RouteContext routeContext) {
if (beanType != null) {
return new BeanExpression(ObjectHelper.newInstance(beanType), getMethod());
+ } else if (instance != null) {
+ return new BeanExpression(instance, getMethod());
} else {
- return new BeanExpression(beanName(), getMethod());
+ return new BeanExpression(beanName(), getMethod());
}
}
@@ -96,6 +111,8 @@
public Predicate createPredicate(RouteContext routeContext) {
if (beanType != null) {
return new BeanExpression(ObjectHelper.newInstance(beanType), getMethod());
+ } else if (instance != null) {
+ return new BeanExpression(instance, getMethod());
} else {
return new BeanExpression(beanName(), getMethod());
}
@@ -104,6 +121,8 @@
protected String beanName() {
if (bean != null) {
return bean;
+ } else if (instance != null) {
+ return ObjectHelper.className(instance);
}
return getExpression();
}
Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanNonRegistryTest.java (from r899913, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanLanguageNonRegistryTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanNonRegistryTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanNonRegistryTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanLanguageNonRegistryTest.java&r1=899913&r2=899932&rev=899932&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanLanguageNonRegistryTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanNonRegistryTest.java Sat Jan 16 12:49:38 2010
@@ -18,14 +18,25 @@
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.language.bean.BeanLanguage;
/**
* @version $Revision$
*/
-public class FilterBeanLanguageNonRegistryTest extends ContextTestSupport {
+public class FilterBeanNonRegistryTest extends ContextTestSupport {
public void testBeanLanguageExp() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ MyBean myBean = new MyBean();
+
+ from("direct:start")
+ .filter().expression(bean(myBean, "isGoldCustomer"))
+ .to("mock:result");
+ }
+ });
+ context.start();
+
getMockEndpoint("mock:result").expectedBodiesReceived("Camel");
template.sendBody("direct:start", "Hello World");
@@ -35,18 +46,31 @@
assertMockEndpointsSatisfied();
}
- @Override
- protected RouteBuilder createRouteBuilder() throws Exception {
- return new RouteBuilder() {
+ public void testMethodCallExp() throws Exception {
+ context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
MyBean myBean = new MyBean();
from("direct:start")
- .filter().expression(BeanLanguage.bean(myBean, "isGoldCustomer"))
+ .filter().method(myBean)
.to("mock:result");
}
- };
+ });
+ context.start();
+
+ getMockEndpoint("mock:result").expectedBodiesReceived("Camel");
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start", "Camel");
+ template.sendBody("direct:start", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
}
public class MyBean {
@@ -55,4 +79,4 @@
return "Camel".equals(name);
}
}
-}
+}
\ No newline at end of file
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanRegistryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanRegistryTest.java?rev=899932&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanRegistryTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanRegistryTest.java Sat Jan 16 12:49:38 2010
@@ -0,0 +1,86 @@
+/**
+ * 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.issues;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * @version $Revision$
+ */
+public class FilterBeanRegistryTest extends ContextTestSupport {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("foo", new MyBean());
+ return jndi;
+ }
+
+ public void testMethodCallExp() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .filter().method("foo", "isGoldCustomer")
+ .to("mock:result");
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:result").expectedBodiesReceived("Camel");
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start", "Camel");
+ template.sendBody("direct:start", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testBeanLanguageExp() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .filter().expression(bean("foo", "isGoldCustomer"))
+ .to("mock:result");
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:result").expectedBodiesReceived("Camel");
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start", "Camel");
+ template.sendBody("direct:start", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ public class MyBean {
+
+ public boolean isGoldCustomer(String name) {
+ return "Camel".equals(name);
+ }
+ }
+}
\ No newline at end of file
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanRegistryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/FilterBeanRegistryTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date