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 2008/08/23 17:56:57 UTC
svn commit: r688350 - in /activemq/camel/trunk/camel-core/src:
main/java/org/apache/camel/component/bean/
main/java/org/apache/camel/language/bean/
test/java/org/apache/camel/language/
Author: davsclaus
Date: Sat Aug 23 08:56:57 2008
New Revision: 688350
URL: http://svn.apache.org/viewvc?rev=688350&view=rev
Log:
CAMEL-844: BeanLanguage now supports invoking bean that isn't in the regiistry. So you can invoke beans just by the typename (= classname). Also improved the debug/trace logging a bit.
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/BeanTest.java
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java?rev=688350&r1=688349&r2=688350&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java Sat Aug 23 08:56:57 2008
@@ -68,10 +68,6 @@
}
public void process(Exchange exchange) throws Exception {
- if (LOG.isDebugEnabled()) {
- LOG.debug(">>>> invoking method for: " + exchange);
- }
-
Object bean = beanHolder.getBean();
exchange.setProperty("org.apache.camel.bean.BeanHolder", beanHolder);
@@ -119,13 +115,13 @@
if (exchange.getPattern().isOutCapable()) {
// force out creating if not already created (as its lazy)
if (LOG.isDebugEnabled()) {
- LOG.debug("Setting bean invocation value on OUT message: " + value);
+ LOG.debug("Setting bean invocation result on the OUT message: " + value);
}
exchange.getOut(true).setBody(value);
} else {
// if not out then set it on the in
if (LOG.isDebugEnabled()) {
- LOG.debug("Setting bean invocation value on IN message: " + value);
+ LOG.debug("Setting bean invocation result on the IN message: " + value);
}
exchange.getIn().setBody(value);
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java?rev=688350&r1=688349&r2=688350&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java Sat Aug 23 08:56:57 2008
@@ -31,6 +31,7 @@
import org.apache.camel.Pattern;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
+import static org.apache.camel.util.ObjectHelper.asString;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -79,6 +80,9 @@
}
public Object proceed() throws Throwable {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace(">>>> invoking: " + method + " on bean: " + pojo + " with arguments: " + asString(arguments) + " for exchange: " + messageExchange);
+ }
return invoke(method, pojo, arguments, messageExchange);
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java?rev=688350&r1=688349&r2=688350&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java Sat Aug 23 08:56:57 2008
@@ -18,7 +18,9 @@
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
+import org.apache.camel.component.bean.BeanHolder;
import org.apache.camel.component.bean.BeanProcessor;
+import org.apache.camel.component.bean.ConstantBeanHolder;
import org.apache.camel.component.bean.RegistryBean;
import org.apache.camel.impl.ExpressionSupport;
@@ -30,6 +32,12 @@
public class BeanExpression<E extends Exchange> extends ExpressionSupport<E> {
private String beanName;
private String method;
+ private Object bean;
+
+ public BeanExpression(Object bean, String method) {
+ this.bean = bean;
+ this.method = method;
+ }
public BeanExpression(String beanName, String method) {
this.beanName = beanName;
@@ -38,7 +46,7 @@
@Override
public String toString() {
- return "BeanExpression[bean: " + beanName + " method: " + method + "]";
+ return "BeanExpression[bean:" + (bean == null ? beanName : bean) + " method: " + method + "]";
}
protected String assertionFailureMessage(E exchange) {
@@ -46,7 +54,15 @@
}
public Object evaluate(E exchange) {
- BeanProcessor processor = new BeanProcessor(new RegistryBean(exchange.getContext(), beanName));
+ // either use registry lookup or a constant bean
+ BeanHolder holder;
+ if (bean == null) {
+ holder = new RegistryBean(exchange.getContext(), beanName);
+ } else {
+ holder = new ConstantBeanHolder(bean, exchange.getContext());
+ }
+
+ BeanProcessor processor = new BeanProcessor(holder);
if (method != null) {
processor.setMethod(method);
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanLanguage.java?rev=688350&r1=688349&r2=688350&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanLanguage.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanLanguage.java Sat Aug 23 08:56:57 2008
@@ -32,16 +32,49 @@
* then the method is invoked to evaluate the expression using the
* <a href="http://activemq.apache.org/camel/bean-integration.html">bean integration</a> to bind the
* {@link Exchange} to the method arguments.
+ * <p/>
+ * As of Camel 1.5 the bean language also supports invoking a provided bean by
+ * its classname or the bean itself.
*
* @version $Revision$
*/
public class BeanLanguage implements Language {
+ /**
+ * Creates the expression based on the string syntax.
+ *
+ * @param expression the string syntax
+ * @return the expression
+ */
public static Expression bean(String expression) {
BeanLanguage language = new BeanLanguage();
return language.createExpression(expression);
}
+ /**
+ * Creates the expression for invoking the bean type.
+ *
+ * @param beanType the bean type to invoke
+ * @param method optional name of method to invoke for instance to avoid ambiguity
+ * @return the expression
+ */
+ public static Expression bean(Class beanType, String method) {
+ Object bean = ObjectHelper.newInstance(beanType);
+ return bean(bean, method);
+ }
+
+ /**
+ * Creates the expression for invoking the bean type.
+ *
+ * @param bean the bean to invoke
+ * @param method optional name of method to invoke for instance to avoid ambiguity
+ * @return the expression
+ */
+ public static Expression bean(Object bean, String method) {
+ BeanLanguage language = new BeanLanguage();
+ return language.createExpression(bean, method);
+ }
+
public Predicate<Exchange> createPredicate(String expression) {
return PredicateBuilder.toPredicate(createExpression(expression));
}
@@ -59,4 +92,9 @@
return new BeanExpression(beanName, method);
}
+ public Expression<Exchange> createExpression(Object bean, String method) {
+ ObjectHelper.notNull(bean, "bean");
+ return new BeanExpression(bean, method);
+ }
+
}
\ No newline at end of file
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/BeanTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/BeanTest.java?rev=688350&r1=688349&r2=688350&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/BeanTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/BeanTest.java Sat Aug 23 08:56:57 2008
@@ -19,11 +19,11 @@
import javax.naming.Context;
import org.apache.camel.Exchange;
-import org.apache.camel.ExchangePattern;
+import org.apache.camel.Expression;
import org.apache.camel.Header;
import org.apache.camel.LanguageTestSupport;
import org.apache.camel.Message;
-import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.language.bean.BeanLanguage;
/**
* @version $Revision$
@@ -38,6 +38,31 @@
assertPredicate("foo.isFooHeaderAbc");
}
+ public void testBeanTypeExpression() throws Exception {
+ Expression exp = BeanLanguage.bean(MyUser.class, null);
+ Exchange exchange = createExchangeWithBody("Claus");
+
+ Object result = exp.evaluate(exchange);
+ assertEquals("Hello Claus", result);
+ }
+
+ public void testBeanTypeAndMethodExpression() throws Exception {
+ Expression exp = BeanLanguage.bean(MyUser.class, "hello");
+ Exchange exchange = createExchangeWithBody("Claus");
+
+ Object result = exp.evaluate(exchange);
+ assertEquals("Hello Claus", result);
+ }
+
+ public void testBeanInstanceAndMethodExpression() throws Exception {
+ MyUser user = new MyUser();
+ Expression exp = BeanLanguage.bean(user, "hello");
+ Exchange exchange = createExchangeWithBody("Claus");
+
+ Object result = exp.evaluate(exchange);
+ assertEquals("Hello Claus", result);
+ }
+
protected String getLanguageName() {
return "bean";
}
@@ -59,4 +84,10 @@
return "abc".equals(foo);
}
}
+
+ public static class MyUser {
+ public String hello(String name) {
+ return "Hello " + name;
+ }
+ }
}
\ No newline at end of file