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 2012/03/04 13:57:14 UTC

svn commit: r1296794 - in /camel/branches/camel-2.9.x: ./ camel-core/src/main/java/org/apache/camel/component/bean/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/test/java/org/apache/camel/impl/

Author: davsclaus
Date: Sun Mar  4 12:57:13 2012
New Revision: 1296794

URL: http://svn.apache.org/viewvc?rev=1296794&view=rev
Log:
CAMEL-4994: @Consume now pre validates method is valid.

Modified:
    camel/branches/camel-2.9.x/   (props changed)
    camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
    camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
    camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
    camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sun Mar  4 12:57:13 2012
@@ -1 +1 @@
-/camel/trunk:1243046,1243057,1243234,1244518,1244644,1244859,1244861,1244864,1244870,1244872,1245021,1291555,1291727,1291848,1291864,1292114,1292384,1292725,1292760,1292767,1293079,1293268,1293288,1293330,1293590,1293828,1293852,1293855,1294130,1294482,1294502,1294533,1294588,1294639,1294709,1294909,1294976,1295073,1295108,1295120,1296653
+/camel/trunk:1243046,1243057,1243234,1244518,1244644,1244859,1244861,1244864,1244870,1244872,1245021,1291555,1291727,1291848,1291864,1292114,1292384,1292725,1292760,1292767,1293079,1293268,1293288,1293330,1293590,1293828,1293852,1293855,1294130,1294482,1294502,1294533,1294588,1294639,1294709,1294909,1294976,1295073,1295108,1295120,1296653,1296790

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

Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=1296794&r1=1296793&r2=1296794&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Sun Mar  4 12:57:13 2012
@@ -93,12 +93,28 @@ public class BeanInfo {
         this(camelContext, type, createParameterMappingStrategy(camelContext));
     }
 
+    public BeanInfo(CamelContext camelContext, Method explicitMethod) {
+        this(camelContext, explicitMethod.getDeclaringClass(), explicitMethod, createParameterMappingStrategy(camelContext));
+    }
+
     public BeanInfo(CamelContext camelContext, Class<?> type, ParameterMappingStrategy strategy) {
+        this(camelContext, type, null, strategy);
+    }
+
+    public BeanInfo(CamelContext camelContext, Class<?> type, Method explicitMethod, ParameterMappingStrategy strategy) {
         this.camelContext = camelContext;
         this.type = type;
         this.strategy = strategy;
 
-        introspect(getType());
+        if (explicitMethod != null) {
+            // must be a valid method
+            if (!isValidMethod(type, explicitMethod)) {
+                throw new IllegalArgumentException("The method " + explicitMethod + " is not valid (for example the method must be public)");
+            }
+            introspect(getType(), explicitMethod);
+        } else {
+            introspect(getType());
+        }
 
         // if there are only 1 method with 1 operation then select it as a default/fallback method
         MethodInfo method = null;
@@ -139,18 +155,29 @@ public class BeanInfo {
         return answer;
     }
 
-    public MethodInvocation createInvocation(Method method, Object pojo, Exchange exchange) {
-        MethodInfo methodInfo = introspect(type, method);
-        if (methodInfo != null) {
-            return methodInfo.createMethodInvocation(pojo, exchange);
-        }
-        return null;
+    public MethodInvocation createInvocation(Object pojo, Exchange exchange)
+        throws AmbiguousMethodCallException, MethodNotFoundException {
+        return createInvocation(pojo, exchange, null);
     }
 
     @SuppressWarnings("unchecked")
-    public MethodInvocation createInvocation(Object pojo, Exchange exchange)
+    private MethodInvocation createInvocation(Object pojo, Exchange exchange, Method explicitMethod)
         throws AmbiguousMethodCallException, MethodNotFoundException {
         MethodInfo methodInfo = null;
+        
+        // find the explicit method to invoke
+        if (explicitMethod != null) {
+            Iterator<List<MethodInfo>> it = operations.values().iterator();
+            while (it.hasNext()) {
+                List<MethodInfo> infos = it.next();
+                for (MethodInfo info : infos) {
+                    if (explicitMethod.equals(info.getMethod())) {
+                        return info.createMethodInvocation(pojo, exchange);
+                    }
+                }
+            }
+            throw new MethodNotFoundException(exchange, pojo, explicitMethod.getName());
+        }
 
         String methodName = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class);
         if (methodName != null) {
@@ -213,7 +240,7 @@ public class BeanInfo {
      *
      * @param clazz the class
      */
-    protected void introspect(Class<?> clazz) {
+    private 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);
@@ -253,7 +280,7 @@ public class BeanInfo {
      * @param method the method
      * @return the method info, is newer <tt>null</tt>
      */
-    protected MethodInfo introspect(Class<?> clazz, Method method) {
+    private MethodInfo introspect(Class<?> clazz, Method method) {
         LOG.trace("Introspecting class: {}, method: {}", clazz, method);
         String opName = method.getName();
 
@@ -265,7 +292,6 @@ public class BeanInfo {
         MethodInfo existingMethodInfo = overridesExistingMethod(methodInfo);
         if (existingMethodInfo != null) {
             LOG.trace("This method is already overridden in a subclass, so the method from the sub class is preferred: {}", existingMethodInfo);
-
             return existingMethodInfo;
         }
 

Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java?rev=1296794&r1=1296793&r2=1296794&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java (original)
+++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java Sun Mar  4 12:57:13 2012
@@ -139,30 +139,26 @@ public class BeanProcessor extends Servi
         }
 
         MethodInvocation invocation;
-        if (methodObject != null) {
-            invocation = beanInfo.createInvocation(methodObject, bean, exchange);
-        } else {
-            // set explicit method name to invoke as a header, which is how BeanInfo can detect it
-            if (explicitMethodName != null) {
-                in.setHeader(Exchange.BEAN_METHOD_NAME, explicitMethodName);
-            }
-            try {
-                invocation = beanInfo.createInvocation(bean, exchange);
-            } catch (Throwable e) {
-                exchange.setException(e);
-                callback.done(true);
-                return true;
-            }
+        // set explicit method name to invoke as a header, which is how BeanInfo can detect it
+        if (explicitMethodName != null) {
+            in.setHeader(Exchange.BEAN_METHOD_NAME, explicitMethodName);
+        }
+        try {
+            invocation = beanInfo.createInvocation(bean, exchange);
+        } catch (Throwable e) {
+            exchange.setException(e);
+            callback.done(true);
+            return true;
+        } finally {
+            // must remove headers as they were provisional
+            in.removeHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY);
+            in.removeHeader(Exchange.BEAN_METHOD_NAME);
         }
         if (invocation == null) {
             throw new IllegalStateException("No method invocation could be created, no matching method could be found on: " + bean);
         }
 
-        // remove headers as they should not be propagated
-        in.removeHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY);
-        in.removeHeader(Exchange.BEAN_METHOD_NAME);
-
-        Object value = null;
+        Object value;
         try {
             AtomicBoolean sync = new AtomicBoolean(true);
             value = invocation.proceed(callback, sync);
@@ -215,14 +211,6 @@ public class BeanProcessor extends Servi
     // Properties
     // -----------------------------------------------------------------------
 
-    public Method getMethodObject() {
-        return methodObject;
-    }
-
-    public void setMethodObject(Method methodObject) {
-        this.methodObject = methodObject;
-    }
-
     public String getMethod() {
         return method;
     }

Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java?rev=1296794&r1=1296793&r2=1296794&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java (original)
+++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java Sun Mar  4 12:57:13 2012
@@ -32,6 +32,7 @@ import org.apache.camel.Producer;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.ProxyInstantiationException;
 import org.apache.camel.Service;
+import org.apache.camel.component.bean.BeanInfo;
 import org.apache.camel.component.bean.BeanProcessor;
 import org.apache.camel.component.bean.ProxyHelper;
 import org.apache.camel.processor.UnitOfWorkProcessor;
@@ -124,8 +125,8 @@ public class CamelPostProcessorHelper im
      * message exchange is received
      */
     protected Processor createConsumerProcessor(final Object pojo, final Method method, final Endpoint endpoint) {
-        BeanProcessor answer = new BeanProcessor(pojo, getCamelContext());
-        answer.setMethodObject(method);
+        BeanInfo info = new BeanInfo(getCamelContext(), method);
+        BeanProcessor answer = new BeanProcessor(pojo, info);
         // must ensure the consumer is being executed in an unit of work so synchronization callbacks etc is invoked
         return new UnitOfWorkProcessor(answer);
     }

Modified: camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java?rev=1296794&r1=1296793&r2=1296794&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java (original)
+++ camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java Sun Mar  4 12:57:13 2012
@@ -29,6 +29,7 @@ import org.apache.camel.Produce;
 import org.apache.camel.Producer;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.support.SynchronizationAdapter;
 import org.apache.camel.util.ObjectHelper;
@@ -74,6 +75,21 @@ public class CamelPostProcessorHelperTes
         assertMockEndpointsSatisfied();
     }
 
+    public void testConsumePrivate() throws Exception {
+        CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
+
+        MyPrivateConsumeBean my = new MyPrivateConsumeBean();
+        Method method = my.getClass().getDeclaredMethod("consumeSomethingPrivate", String.class);
+        try {
+            helper.consumerInjection(method, my, "foo");
+            fail("Should have thrown exception");
+        } catch (RuntimeCamelException e) {
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertTrue(iae.getMessage().startsWith("The method private void"));
+            assertTrue(iae.getMessage().endsWith("(for example the method must be public)"));
+        }
+    }
+
     public void testConsumeSynchronization() throws Exception {
         CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
 
@@ -456,4 +472,13 @@ public class CamelPostProcessorHelperTes
 
     }
 
+    public class MyPrivateConsumeBean {
+
+        @Consume(uri = "seda:foo")
+        private void consumeSomethingPrivate(String body) {
+            assertEquals("Hello World", body);
+            template.sendBody("mock:result", body);
+        }
+    }
+
 }