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 2011/09/22 12:51:00 UTC

svn commit: r1174050 - in /camel/branches/camel-2.8.x: ./ camel-core/src/main/java/org/apache/camel/component/bean/ camel-core/src/main/java/org/apache/camel/language/bean/ camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/a...

Author: davsclaus
Date: Thu Sep 22 10:50:59 2011
New Revision: 1174050

URL: http://svn.apache.org/viewvc?rev=1174050&view=rev
Log:
Merged revisions 1174047 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk


Added:
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanTypeHolder.java
      - copied unchanged from r1174047, camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanTypeHolder.java
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
      - copied unchanged from r1174047, camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
    camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/RouteMethodCallStaticTest.java
      - copied unchanged from r1174047, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/RouteMethodCallStaticTest.java
    camel/branches/camel-2.8.x/components/camel-spring/src/test/java/org/apache/camel/component/bean/SpringRouteMethodCallStaticTest.java
      - copied unchanged from r1174047, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/bean/SpringRouteMethodCallStaticTest.java
    camel/branches/camel-2.8.x/components/camel-spring/src/test/resources/org/apache/camel/component/bean/SpringRouteMethodCallStaticTest.xml
      - copied unchanged from r1174047, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/bean/SpringRouteMethodCallStaticTest.xml
Modified:
    camel/branches/camel-2.8.x/   (props changed)
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java
    camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/MyStaticClass.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Sep 22 10:50:59 2011
@@ -1 +1 @@
-/camel/trunk:1173732,1173958
+/camel/trunk:1173732,1173958,1174047

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java?rev=1174050&r1=1174049&r2=1174050&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java Thu Sep 22 10:50:59 2011
@@ -19,6 +19,7 @@ package org.apache.camel.component.bean;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Processor;
 import org.apache.camel.util.CamelContextHelper;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * A constant (singleton) bean implementation of {@link BeanHolder}
@@ -31,6 +32,9 @@ public class ConstantBeanHolder implemen
     private final Processor processor;
 
     public ConstantBeanHolder(Object bean, BeanInfo beanInfo) {
+        ObjectHelper.notNull(bean, "bean");
+        ObjectHelper.notNull(beanInfo, "beanInfo");
+
         this.bean = bean;
         this.beanInfo = beanInfo;
         this.processor = CamelContextHelper.convertTo(beanInfo.getCamelContext(), Processor.class, bean);

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java?rev=1174050&r1=1174049&r2=1174050&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java Thu Sep 22 10:50:59 2011
@@ -36,7 +36,7 @@ public class MethodNotFoundException ext
         this.bean = pojo;
     }
 
-    public MethodNotFoundException(Exchange exchange, Class type, String methodName, boolean isStaticMethod) {
+    public MethodNotFoundException(Exchange exchange, Class<?> type, String methodName, boolean isStaticMethod) {
         super((isStaticMethod ? "Static method" : "Method") + " with name: " + methodName + " not found on class: " + ObjectHelper.name(type), exchange);
         this.methodName = methodName;
         this.bean = null;
@@ -48,6 +48,12 @@ public class MethodNotFoundException ext
         this.bean = pojo;
     }
 
+    public MethodNotFoundException(Class<?> type, String methodName, Throwable cause) {
+        super("Method with name: " + methodName + " not found on class: " + ObjectHelper.className(type), null, cause);
+        this.methodName = methodName;
+        this.bean = null;
+    }
+
     public String getMethodName() {
         return methodName;
     }

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java?rev=1174050&r1=1174049&r2=1174050&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java Thu Sep 22 10:50:59 2011
@@ -28,6 +28,7 @@ import org.apache.camel.Processor;
 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.ConstantTypeBeanHolder;
 import org.apache.camel.component.bean.RegistryBean;
 import org.apache.camel.util.KeyValueHolder;
 import org.apache.camel.util.ObjectHelper;
@@ -40,9 +41,10 @@ import org.apache.camel.util.StringHelpe
  * @version 
  */
 public class BeanExpression implements Expression, Predicate {
+    private Object bean;
     private String beanName;
+    private Class<?> type;
     private String method;
-    private Object bean;
 
     public BeanExpression(Object bean, String method) {
         this.bean = bean;
@@ -54,18 +56,39 @@ public class BeanExpression implements E
         this.method = method;
     }
 
+    public BeanExpression(Class<?> type, String method) {
+        this.type = type;
+        this.method = method;
+    }
+
     @Override
     public String toString() {
-        return "BeanExpression[bean:" + (bean == null ? beanName : bean) + " method: " + method + "]";
+        StringBuilder sb = new StringBuilder("BeanExpression[");
+        if (bean != null) {
+            sb.append(bean.toString());
+        } else if (beanName != null) {
+            sb.append(beanName);
+        } else if (type != null) {
+            sb.append(ObjectHelper.className(type));
+        }
+        if (method != null) {
+            sb.append(" method: ").append(method);
+        }
+        sb.append("]");
+        return sb.toString();
     }
 
     public Object evaluate(Exchange exchange) {
         // either use registry lookup or a constant bean
         BeanHolder holder;
-        if (bean == null) {
+        if (bean != null) {
+            holder = new ConstantBeanHolder(bean, exchange.getContext());
+        } else if (beanName != null) {
             holder = new RegistryBean(exchange.getContext(), beanName);
+        } else if (type != null) {
+            holder = new ConstantTypeBeanHolder(type, exchange.getContext());
         } else {
-            holder = new ConstantBeanHolder(bean, exchange.getContext());
+            throw new IllegalArgumentException("Either bean, beanName or type should be set on " + this);
         }
 
         // invoking the bean can either be the easy way or using OGNL
@@ -78,8 +101,7 @@ public class BeanExpression implements E
 
         if (OgnlHelper.isValidOgnlExpression(method)) {
             // okay the method is an ognl expression
-            Object beanToCall = holder.getBean();
-            OgnlInvokeProcessor ognl = new OgnlInvokeProcessor(beanToCall, method);
+            OgnlInvokeProcessor ognl = new OgnlInvokeProcessor(holder, method);
             try {
                 ognl.process(exchange);
                 return ognl.getResult();
@@ -159,15 +181,15 @@ public class BeanExpression implements E
      */
     private final class OgnlInvokeProcessor implements Processor {
 
-        private final Object bean;
         private final String ognl;
+        private final BeanHolder beanHolder;
         private Object result;
 
-        public OgnlInvokeProcessor(Object bean, String ognl) {
-            this.bean = bean;
+        public OgnlInvokeProcessor(BeanHolder beanHolder, String ognl) {
+            this.beanHolder = beanHolder;
             this.ognl = ognl;
             // we must start with having bean as the result
-            this.result = bean;
+            this.result = beanHolder.getBean();
         }
 
         public void process(Exchange exchange) throws Exception {
@@ -180,7 +202,11 @@ public class BeanExpression implements E
             String ognlPath = "";
 
             // loop and invoke each method
-            Object beanToCall = bean;
+            Object beanToCall = beanHolder.getBean();
+            // there must be a bean to call with, we currently does not support OGNL expressions on using purely static methods
+            if (beanToCall == null) {
+                throw new IllegalArgumentException("Bean instance is null. OGNL bean expressions requires bean instances.");
+            }
 
             // Split ognl except when this is not a Map, Array
             // and we would like to keep the dots within the key name

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java?rev=1174050&r1=1174049&r2=1174050&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java Thu Sep 22 10:50:59 2011
@@ -23,11 +23,11 @@ import javax.xml.bind.annotation.XmlRoot
 import javax.xml.bind.annotation.XmlTransient;
 
 import org.apache.camel.Processor;
-import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.bean.BeanHolder;
 import org.apache.camel.component.bean.BeanInfo;
 import org.apache.camel.component.bean.BeanProcessor;
 import org.apache.camel.component.bean.ConstantBeanHolder;
+import org.apache.camel.component.bean.ConstantTypeBeanHolder;
 import org.apache.camel.component.bean.MethodNotFoundException;
 import org.apache.camel.component.bean.RegistryBean;
 import org.apache.camel.spi.Required;
@@ -153,10 +153,10 @@ public class BeanDefinition extends NoOu
     public Processor createProcessor(RouteContext routeContext) {
         BeanProcessor answer;
         Class<?> clazz = bean != null ? bean.getClass() : null;
-        BeanInfo beanInfo = null;
+        BeanHolder beanHolder;
 
         if (ObjectHelper.isNotEmpty(ref)) {
-            RegistryBean beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
+            beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
             // bean holder will check if the bean exists
             bean = beanHolder.getBean();
             answer = new BeanProcessor(beanHolder);
@@ -182,33 +182,30 @@ public class BeanDefinition extends NoOu
                     + ". We suppose you want to refer to a bean instance by its id instead. Please use beanRef.");
             }
 
-            // notice bean may be null if its a static class
-            beanInfo = new BeanInfo(routeContext.getCamelContext(), clazz);
-            answer = new BeanProcessor(new ConstantBeanHolder(bean, beanInfo));
+            // the holder should either be bean or type based
+            beanHolder = bean != null ? new ConstantBeanHolder(bean, routeContext.getCamelContext()) : new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext());
+            answer = new BeanProcessor(beanHolder);
         }
+
+        // check for method exists
         if (method != null) {
             answer.setMethod(method);
 
             // check there is a method with the given name, and leverage BeanInfo for that
-            if (bean != null) {
-                clazz = bean.getClass();
-            }
-            if (beanInfo == null) {
-                beanInfo = new BeanInfo(routeContext.getCamelContext(), clazz);
-            }
-
+            BeanInfo beanInfo = beanHolder.getBeanInfo();
             if (bean != null) {
                 // there is a bean instance, so check for any methods
                 if (!beanInfo.hasMethod(method)) {
                     throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, bean, method));
                 }
-            } else {
+            } else if (clazz != null) {
                 // there is no bean instance, so check for static methods only
                 if (!beanInfo.hasStaticMethod(method)) {
                     throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, clazz, method, true));
                 }
             }
         }
+
         return answer;
     }
 
@@ -228,4 +225,5 @@ public class BeanDefinition extends NoOu
             return "";
         }
     }
+
 }

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java?rev=1174050&r1=1174049&r2=1174050&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java Thu Sep 22 10:50:59 2011
@@ -31,6 +31,7 @@ import org.apache.camel.component.bean.B
 import org.apache.camel.component.bean.MethodNotFoundException;
 import org.apache.camel.component.bean.RegistryBean;
 import org.apache.camel.language.bean.BeanExpression;
+import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.OgnlHelper;
 
@@ -154,8 +155,13 @@ public class MethodCallExpression extend
         }
 
         if (beanType != null) {
-            instance = camelContext.getInjector().newInstance(beanType);
-            answer = new BeanExpression(instance, getMethod());
+            // create a bean if there is a default public no-arg constructor
+            if (ObjectHelper.hasDefaultPublicNoArgConstructor(beanType)) {
+                instance = camelContext.getInjector().newInstance(beanType);
+                answer = new BeanExpression(instance, getMethod());
+            } else {
+                answer = new BeanExpression(beanType, getMethod());
+            }
         } else if (instance != null) {
             answer = new BeanExpression(instance, getMethod());
         } else {
@@ -167,7 +173,7 @@ public class MethodCallExpression extend
             answer = new BeanExpression(ref, getMethod());
         }
 
-        validateHasMethod(camelContext, instance, getMethod());
+        validateHasMethod(camelContext, instance, beanType, getMethod());
         return answer;
     }
 
@@ -183,14 +189,19 @@ public class MethodCallExpression extend
      *
      * @param context  camel context
      * @param bean     the bean instance
+     * @param type     the bean type
      * @param method   the method, can be <tt>null</tt> if no method name provided
      * @throws org.apache.camel.RuntimeCamelException is thrown if bean does not have the method
      */
-    protected void validateHasMethod(CamelContext context, Object bean, String method) {
+    protected void validateHasMethod(CamelContext context, Object bean, Class<?> type, String method) {
         if (method == null) {
             return;
         }
 
+        if (bean == null && type == null) {
+            throw new IllegalArgumentException("Either bean or type should be provided on " + this);
+        }
+
         // do not try to validate ognl methods
         if (OgnlHelper.isValidOgnlExpression(method)) {
             return;
@@ -199,12 +210,20 @@ public class MethodCallExpression extend
         // if invalid OGNL then fail
         if (OgnlHelper.isInvalidValidOgnlExpression(method)) {
             ExpressionIllegalSyntaxException cause = new ExpressionIllegalSyntaxException(method);
-            throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(bean, method, cause));
+            throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(bean != null ? bean : type, method, cause));
         }
 
-        BeanInfo info = new BeanInfo(context, bean.getClass());
-        if (!info.hasMethod(method)) {
-            throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, bean, method));
+        if (bean != null) {
+            BeanInfo info = new BeanInfo(context, bean.getClass());
+            if (!info.hasMethod(method)) {
+                throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, bean, method));
+            }
+        } else {
+            BeanInfo info = new BeanInfo(context, type);
+            // must be a static method as we do not have a bean instance to invoke
+            if (!info.hasStaticMethod(method)) {
+                throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, type, method, true));
+            }
         }
     }
 

Modified: camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/MyStaticClass.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/MyStaticClass.java?rev=1174050&r1=1174049&r2=1174050&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/MyStaticClass.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/MyStaticClass.java Thu Sep 22 10:50:59 2011
@@ -31,6 +31,10 @@ public final class MyStaticClass {
         return null;
     }
 
+    public static boolean isCamel(String body) {
+        return body.contains("Camel");
+    }
+
     public void doSomething() {
         // noop
     }