camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1173269 - 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/model/ camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/cam...
Date Tue, 20 Sep 2011 17:29:14 GMT
Author: dkulp
Date: Tue Sep 20 17:29:13 2011
New Revision: 1173269

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

........
  r1172503 | davsclaus | 2011-09-19 03:54:04 -0400 (Mon, 19 Sep 2011) | 1 line
  
  CAMEL-4386: Bean component can now invoke static methods without the need for creating a
new bean instance.
........

Added:
    camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvokeStaticTest.java
      - copied unchanged from r1172503, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvokeStaticTest.java
    camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/component/bean/MyStaticClass.java
      - copied unchanged from r1172503, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyStaticClass.java
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/BeanInfo.java
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.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/model/BeanDefinition.java
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java

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/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=1173269&r1=1173268&r2=1173269&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
(original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
Tue Sep 20 17:29:13 2011
@@ -196,6 +196,7 @@ public class BeanInfo {
     protected void introspect(Class<?> clazz) {
         // get the target clazz as it could potentially have been enhanced by CGLIB etc.
         clazz = getTargetClass(clazz);
+        ObjectHelper.notNull(clazz, "clazz", this);
 
         LOG.trace("Introspecting class: {}", clazz);
 
@@ -773,6 +774,28 @@ public class BeanInfo {
     }
 
     /**
+     * Do we have a static method with the given name.
+     * <p/>
+     * Shorthand method names for getters is supported, so you can pass in eg 'name' and
Camel
+     * will can find the real 'getName' method instead.
+     *
+     * @param methodName the method name
+     * @return <tt>true</tt> if we have such a static method.
+     */
+    public boolean hasStaticMethod(String methodName) {
+        List<MethodInfo> methods = getOperations(methodName);
+        if (methods == null || methods.isEmpty()) {
+            return false;
+        }
+        for (MethodInfo method : methods) {
+            if (method.isStaticMethod()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Gets the list of methods sorted by A..Z method name.
      *
      * @return the methods.

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java?rev=1173269&r1=1173268&r2=1173269&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
(original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
Tue Sep 20 17:29:13 2011
@@ -21,6 +21,7 @@ import java.lang.reflect.AccessibleObjec
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -325,6 +326,10 @@ public class MethodInfo {
         return method.getReturnType().getName().equals("void");
     }
 
+    public boolean isStaticMethod() {
+        return Modifier.isStatic(method.getModifiers());
+    }
+
     protected Object invoke(Method mth, Object pojo, Object[] arguments, Exchange exchange)
throws IllegalAccessException, InvocationTargetException {
         return mth.invoke(pojo, arguments);
     }

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=1173269&r1=1173268&r2=1173269&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
Tue Sep 20 17:29:13 2011
@@ -37,6 +37,12 @@ public class MethodNotFoundException ext
         this.bean = pojo;
     }
 
+    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;
+    }
+
     public MethodNotFoundException(Object pojo, String methodName, Throwable cause) {
         super("Method with name: " + methodName + " not found on bean: " + pojo, null, cause);
         this.methodName = methodName;

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=1173269&r1=1173268&r2=1173269&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
Tue Sep 20 17:29:13 2011
@@ -23,8 +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.MethodNotFoundException;
 import org.apache.camel.component.bean.RegistryBean;
 import org.apache.camel.spi.Required;
@@ -149,6 +152,9 @@ public class BeanDefinition extends NoOu
     @Override
     public Processor createProcessor(RouteContext routeContext) {
         BeanProcessor answer;
+        Class<?> clazz = bean != null ? bean.getClass() : null;
+        BeanInfo beanInfo = null;
+
         if (ObjectHelper.isNotEmpty(ref)) {
             RegistryBean beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
             // bean holder will check if the bean exists
@@ -157,15 +163,17 @@ public class BeanDefinition extends NoOu
         } else {
             if (bean == null) {
                 ObjectHelper.notNull(beanType, "bean, ref or beanType", this);
-                Class<?> clazz;
                 try {
                     clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(beanType);
                 } catch (ClassNotFoundException e) {
                     throw ObjectHelper.wrapRuntimeCamelException(e);
                 }
-                bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), clazz);
+                // create a bean if there is a default public no-arg constructor
+                if (ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
+                    bean = CamelContextHelper.newInstance(routeContext.getCamelContext(),
clazz);
+                    ObjectHelper.notNull(bean, "bean", this);
+                }
             }
-            ObjectHelper.notNull(bean, "bean", this);
 
             // validate the bean type is not from java so you by mistake think its a reference
             // to a bean name but the String is being invoke instead
@@ -173,15 +181,32 @@ public class BeanDefinition extends NoOu
                 throw new IllegalArgumentException("The bean instance is a java.lang.String
type: " + bean
                     + ". We suppose you want to refer to a bean instance by its id instead.
Please use beanRef.");
             }
-            answer = new BeanProcessor(bean, routeContext.getCamelContext());
+
+            // notice bean may be null if its a static class
+            beanInfo = new BeanInfo(routeContext.getCamelContext(), clazz);
+            answer = new BeanProcessor(new ConstantBeanHolder(bean, beanInfo));
         }
         if (method != null) {
             answer.setMethod(method);
 
             // check there is a method with the given name, and leverage BeanInfo for that
-            BeanInfo info = new BeanInfo(routeContext.getCamelContext(), bean.getClass());
-            if (!info.hasMethod(method)) {
-                throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null,
bean, method));
+            if (bean != null) {
+                clazz = bean.getClass();
+            }
+            if (beanInfo == null) {
+                beanInfo = new BeanInfo(routeContext.getCamelContext(), clazz);
+            }
+
+            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 {
+                // 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;

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1173269&r1=1173268&r2=1173269&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
(original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
Tue Sep 20 17:29:13 2011
@@ -22,9 +22,11 @@ import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.net.URL;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.charset.Charset;
@@ -1081,6 +1083,19 @@ public final class ObjectHelper {
     }
 
     /**
+     * Does the given class have a default public no-arg constructor.
+     */
+    public static boolean hasDefaultPublicNoArgConstructor(Class type) {
+        // getConstructors() returns only public constructors
+        for (Constructor ctr : type.getConstructors()) {
+            if (ctr.getParameterTypes().length == 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Returns true if the given name is a valid java identifier
      */
     public static boolean isJavaIdentifier(String name) {

Modified: camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java?rev=1173269&r1=1173268&r2=1173269&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
(original)
+++ camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
Tue Sep 20 17:29:13 2011
@@ -27,6 +27,7 @@ import java.util.List;
 import junit.framework.TestCase;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
+import org.apache.camel.component.bean.MyStaticClass;
 import org.apache.camel.impl.DefaultMessage;
 
 /**
@@ -299,4 +300,9 @@ public class ObjectHelperTest extends Te
         assertEquals(null, ObjectHelper.lookupConstantFieldValue(null, "FILE_NAME"));
     }
 
+    public void testHasDefaultPublicNoArgConstructor() {
+        assertTrue(ObjectHelper.hasDefaultPublicNoArgConstructor(ObjectHelperTest.class));
+        assertFalse(ObjectHelper.hasDefaultPublicNoArgConstructor(MyStaticClass.class));
+    }
+
 }



Mime
View raw message