You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/09/21 16:45:00 UTC

svn commit: r1388534 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/ test/java/org/apache/camel/processor/

Author: ningjiang
Date: Fri Sep 21 14:45:00 2012
New Revision: 1388534

URL: http://svn.apache.org/viewvc?rev=1388534&view=rev
Log:
CAMEL-5633 Camel bean method should be able to take out the annotation from interface

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListInterfaceAnnotationTest.java
      - copied, changed from r1388366, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java?rev=1388534&r1=1388533&r2=1388534&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java Fri Sep 21 14:45:00 2012
@@ -25,8 +25,12 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -113,18 +117,20 @@ public class MethodInfo {
         this.hasCustomAnnotation = hasCustomAnnotation;
         this.hasHandlerAnnotation = hasHandlerAnnotation;
         this.parametersExpression = createParametersExpression();
+        
+        Map<Class<?>, Annotation> collectedMethodAnnotation = collectMethodAnnotations(type, method);
 
         Pattern oneway = findOneWayAnnotation(method);
         if (oneway != null) {
             pattern = oneway.value();
         }
         
-        if (method.getAnnotation(org.apache.camel.RoutingSlip.class) != null
-                && matchContext(method.getAnnotation(org.apache.camel.RoutingSlip.class).context())) {
-            org.apache.camel.RoutingSlip annotation = method.getAnnotation(org.apache.camel.RoutingSlip.class);
+        org.apache.camel.RoutingSlip routingSlipAnnotation = 
+            (org.apache.camel.RoutingSlip)collectedMethodAnnotation.get(org.apache.camel.RoutingSlip.class);
+        if (routingSlipAnnotation != null && matchContext(routingSlipAnnotation.context())) {
             routingSlip = new RoutingSlip(camelContext);
-            routingSlip.setDelimiter(annotation.delimiter());
-            routingSlip.setIgnoreInvalidEndpoints(annotation.ignoreInvalidEndpoints());
+            routingSlip.setDelimiter(routingSlipAnnotation.delimiter());
+            routingSlip.setIgnoreInvalidEndpoints(routingSlipAnnotation.ignoreInvalidEndpoints());
             // add created routingSlip as a service so we have its lifecycle managed
             try {
                 camelContext.addService(routingSlip);
@@ -133,12 +139,13 @@ public class MethodInfo {
             }
         }
 
-        if (method.getAnnotation(org.apache.camel.DynamicRouter.class) != null
-                && matchContext(method.getAnnotation(org.apache.camel.DynamicRouter.class).context())) {
-            org.apache.camel.DynamicRouter annotation = method.getAnnotation(org.apache.camel.DynamicRouter.class);
+        org.apache.camel.DynamicRouter dynamicRouterAnnotation = 
+            (org.apache.camel.DynamicRouter)collectedMethodAnnotation.get(org.apache.camel.DynamicRouter.class);
+        if (dynamicRouterAnnotation != null
+                && matchContext(dynamicRouterAnnotation.context())) {
             dynamicRouter = new DynamicRouter(camelContext);
-            dynamicRouter.setDelimiter(annotation.delimiter());
-            dynamicRouter.setIgnoreInvalidEndpoints(annotation.ignoreInvalidEndpoints());
+            dynamicRouter.setDelimiter(dynamicRouterAnnotation.delimiter());
+            dynamicRouter.setIgnoreInvalidEndpoints(dynamicRouterAnnotation.ignoreInvalidEndpoints());
             // add created dynamicRouter as a service so we have its lifecycle managed
             try {
                 camelContext.addService(dynamicRouter);
@@ -147,37 +154,36 @@ public class MethodInfo {
             }
         }
 
-        if (method.getAnnotation(org.apache.camel.RecipientList.class) != null
-                && matchContext(method.getAnnotation(org.apache.camel.RecipientList.class).context())) {
-
-            org.apache.camel.RecipientList annotation = method.getAnnotation(org.apache.camel.RecipientList.class);
-
-            recipientList = new RecipientList(camelContext, annotation.delimiter());
-            recipientList.setStopOnException(annotation.stopOnException());
-            recipientList.setIgnoreInvalidEndpoints(annotation.ignoreInvalidEndpoints());
-            recipientList.setParallelProcessing(annotation.parallelProcessing());
-            recipientList.setStreaming(annotation.streaming());
-            recipientList.setTimeout(annotation.timeout());
-            recipientList.setShareUnitOfWork(annotation.shareUnitOfWork());
+        org.apache.camel.RecipientList recipientListAnnotation = 
+            (org.apache.camel.RecipientList)collectedMethodAnnotation.get(org.apache.camel.RecipientList.class);
+        if (recipientListAnnotation != null
+                && matchContext(recipientListAnnotation.context())) {
+            recipientList = new RecipientList(camelContext, recipientListAnnotation.delimiter());
+            recipientList.setStopOnException(recipientListAnnotation.stopOnException());
+            recipientList.setIgnoreInvalidEndpoints(recipientListAnnotation.ignoreInvalidEndpoints());
+            recipientList.setParallelProcessing(recipientListAnnotation.parallelProcessing());
+            recipientList.setStreaming(recipientListAnnotation.streaming());
+            recipientList.setTimeout(recipientListAnnotation.timeout());
+            recipientList.setShareUnitOfWork(recipientListAnnotation.shareUnitOfWork());
 
-            if (ObjectHelper.isNotEmpty(annotation.executorServiceRef())) {
-                ExecutorService executor = camelContext.getExecutorServiceManager().newDefaultThreadPool(this, annotation.executorServiceRef());
+            if (ObjectHelper.isNotEmpty(recipientListAnnotation.executorServiceRef())) {
+                ExecutorService executor = camelContext.getExecutorServiceManager().newDefaultThreadPool(this, recipientListAnnotation.executorServiceRef());
                 recipientList.setExecutorService(executor);
             }
 
-            if (annotation.parallelProcessing() && recipientList.getExecutorService() == null) {
+            if (recipientListAnnotation.parallelProcessing() && recipientList.getExecutorService() == null) {
                 // we are running in parallel so we need a thread pool
                 ExecutorService executor = camelContext.getExecutorServiceManager().newDefaultThreadPool(this, "@RecipientList");
                 recipientList.setExecutorService(executor);
             }
 
-            if (ObjectHelper.isNotEmpty(annotation.strategyRef())) {
-                AggregationStrategy strategy = CamelContextHelper.mandatoryLookup(camelContext, annotation.strategyRef(), AggregationStrategy.class);
+            if (ObjectHelper.isNotEmpty(recipientListAnnotation.strategyRef())) {
+                AggregationStrategy strategy = CamelContextHelper.mandatoryLookup(camelContext, recipientListAnnotation.strategyRef(), AggregationStrategy.class);
                 recipientList.setAggregationStrategy(strategy);
             }
 
-            if (ObjectHelper.isNotEmpty(annotation.onPrepareRef())) {
-                Processor onPrepare = CamelContextHelper.mandatoryLookup(camelContext, annotation.onPrepareRef(), Processor.class);
+            if (ObjectHelper.isNotEmpty(recipientListAnnotation.onPrepareRef())) {
+                Processor onPrepare = CamelContextHelper.mandatoryLookup(camelContext, recipientListAnnotation.onPrepareRef(), Processor.class);
                 recipientList.setOnPrepare(onPrepare);
             }
 
@@ -190,6 +196,32 @@ public class MethodInfo {
         }
     }
 
+    private Map<Class<?>, Annotation> collectMethodAnnotations(Class<?> c, Method method) {
+        Map<Class<?>, Annotation> annotations = new HashMap<Class<?>, Annotation>();
+        collectMethodAnnotations(c, method, annotations);
+        return annotations;
+    }
+    
+    private void collectMethodAnnotations(Class<?> c, Method method, Map<Class<?>, Annotation> annotations) {
+        for (Class<?> i : c.getInterfaces()) {
+            collectMethodAnnotations(i, method, annotations);
+        }
+        if (!c.isInterface() && c.getSuperclass() != null) {
+            collectMethodAnnotations(c.getSuperclass(), method, annotations);
+        }
+        // make sure the sub class can override the definition
+        try {
+            Annotation[] ma = c.getDeclaredMethod(method.getName(), method.getParameterTypes()).getAnnotations();
+            for (Annotation a : ma) {
+                annotations.put(a.annotationType(), a);
+            }
+        } catch (SecurityException e) {
+            // do nothing here
+        } catch (NoSuchMethodException e) {
+            // do nothing here
+        }
+    }
+
     /**
      * Does the given context match this camel context
      */

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListInterfaceAnnotationTest.java (from r1388366, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListInterfaceAnnotationTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListInterfaceAnnotationTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java&r1=1388366&r2=1388534&rev=1388534&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListInterfaceAnnotationTest.java Fri Sep 21 14:45:00 2012
@@ -20,54 +20,27 @@ import java.util.concurrent.atomic.Atomi
 
 import javax.naming.Context;
 
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.util.jndi.JndiContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @version 
- */
-public class BeanRecipientListTest extends ContextTestSupport {
-    private static final transient Logger LOG = LoggerFactory.getLogger(BeanRecipientListTest.class);
-    protected MyBean myBean = new MyBean();
-
-    public void testSendMessage() throws Exception {
-        final String expectedBody = "Wibble";
-
-        getMockEndpoint("mock:a").expectedBodiesReceived(expectedBody);
-        getMockEndpoint("mock:b").expectedBodiesReceived(expectedBody);
-
-        template.sendBody("direct:in", expectedBody);
-
-        assertMockEndpointsSatisfied();
-    }
 
+public class BeanRecipientListInterfaceAnnotationTest extends BeanRecipientListTest {
     @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        Object lookedUpBean = context.getRegistry().lookup("myBean");
-        assertSame("Lookup of 'myBean' should return same object!", myBean, lookedUpBean);
+    protected void checkBean() throws Exception {
+        // do nothing here
     }
 
     @Override
     protected Context createJndiContext() throws Exception {
         JndiContext answer = new JndiContext();
-        answer.bind("myBean", myBean);
+        answer.bind("myBean", new MyBean());
         return answer;
     }
-
-    protected RouteBuilder createRouteBuilder() {
-        return new RouteBuilder() {
-            public void configure() {
-                from("direct:in").beanRef("myBean", "route");
-            }
-        };
+    
+    interface Route {
+        @org.apache.camel.RecipientList
+        String[] route(String body);
     }
-
-    public static class MyBean {
+    
+    public static class MyBean implements Route {
         private static AtomicInteger counter = new AtomicInteger(0);
         private int id;
 
@@ -79,11 +52,11 @@ public class BeanRecipientListTest exten
         public String toString() {
             return "MyBean:" + id;
         }
-
-        @org.apache.camel.RecipientList
+        
         public String[] route(String body) {
-            LOG.debug("Called " + this + " with body: " + body);
             return new String[] {"mock:a", "mock:b"};
         }
     }
+    
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java?rev=1388534&r1=1388533&r2=1388534&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java Fri Sep 21 14:45:00 2012
@@ -47,7 +47,10 @@ public class BeanRecipientListTest exten
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-
+        checkBean();
+    }
+    
+    protected void checkBean() throws Exception {
         Object lookedUpBean = context.getRegistry().lookup("myBean");
         assertSame("Lookup of 'myBean' should return same object!", myBean, lookedUpBean);
     }