You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/08/16 11:29:32 UTC

svn commit: r566604 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/util/ components/camel-spring/src/main/java/org/apache/camel/spring/

Author: jstrachan
Date: Thu Aug 16 02:29:31 2007
New Revision: 566604

URL: http://svn.apache.org/viewvc?view=rev&rev=566604
Log:
initial cut of an implementation of CAMEL-108, just needs a test case

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?view=diff&rev=566604&r1=566603&r2=566604
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Thu Aug 16 02:29:31 2007
@@ -365,4 +365,15 @@
     public static String getDefaultCharacterSet() {
         return Charset.defaultCharset().name();
     }
+
+    /**
+     * Returns the Java Bean property name of the given method, if it is a setter
+     */
+    public static String getPropertyName(Method method) {
+        String propertyName = method.getName();
+        if (propertyName.startsWith("set") && method.getParameterTypes().length == 1) {
+            propertyName = propertyName.substring(3, 4).toLowerCase() + propertyName.substring(4);
+        }
+        return propertyName;
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java?view=diff&rev=566604&r1=566603&r2=566604
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java Thu Aug 16 02:29:31 2007
@@ -18,6 +18,8 @@
 
 import junit.framework.TestCase;
 
+import java.lang.reflect.Method;
+
 /**
  * @version $Revision$
  */
@@ -26,5 +28,16 @@
         assertEquals(ObjectHelper.removeStartingCharacters("foo", '/'), "foo");
         assertEquals(ObjectHelper.removeStartingCharacters("/foo", '/'), "foo");
         assertEquals(ObjectHelper.removeStartingCharacters("//foo", '/'), "foo");
+    }
+
+    public void testGetPropertyName() throws Exception {
+        Method method = getClass().getMethod("setCheese", String.class);
+        assertNotNull("should have found a method!", method);
+
+        String name = ObjectHelper.getPropertyName(method);
+        assertEquals("Property name", "cheese", name);
+    }
+
+    public void setCheese(String cheese) {
     }
 }

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java?view=diff&rev=566604&r1=566603&r2=566604
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java Thu Aug 16 02:29:31 2007
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.spring;
 
+import static org.apache.camel.util.ObjectHelper.isNullOrBlank;
+
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
@@ -115,7 +117,7 @@
             public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                 EndpointInject annotation = field.getAnnotation(EndpointInject.class);
                 if (annotation != null) {
-                    ReflectionUtils.setField(field, bean, getEndpointInjectionValue(annotation, field.getType()));
+                    ReflectionUtils.setField(field, bean, getEndpointInjectionValue(annotation, field.getType(), field.getName()));
                 }
             }
         });
@@ -139,7 +141,8 @@
                 if (parameterTypes.length != 1) {
                     LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
                 } else {
-                    Object value = getEndpointInjectionValue(annoation, parameterTypes[0]);
+                    String propertyName = ObjectHelper.getPropertyName(method);
+                    Object value = getEndpointInjectionValue(annoation, parameterTypes[0], propertyName);
                     ObjectHelper.invokeMethod(method, bean, value);
                 }
             }
@@ -177,14 +180,15 @@
             LOG.info("Creating a consumer for: " + annotation);
 
             // lets bind this method to a listener
-            Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name());
+            String injectionPointName = method.getName();
+            Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name(), injectionPointName);
             if (endpoint != null) {
                 try {
                     Processor processor = createConsumerProcessor(bean, method, endpoint);
                     LOG.info("Created processor: " + processor);
                     Consumer consumer = endpoint.createConsumer(processor);
                     consumer.start();
-                    addConsumer(consumer);
+                    onConsumerAdded(consumer);
                 } catch (Exception e) {
                     LOG.warn(e);
                     throw new RuntimeCamelException(e);
@@ -203,16 +207,15 @@
         return answer;
     }
 
-    protected void addConsumer(Consumer consumer) {
+    protected void onConsumerAdded(Consumer consumer) {
         LOG.debug("Adding consumer: " + consumer);
-        // consumers.add(consumer);
     }
 
     /**
      * Creates the value for the injection point for the given annotation
      */
-    protected Object getEndpointInjectionValue(EndpointInject annotation, Class<?> type) {
-        Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name());
+    protected Object getEndpointInjectionValue(EndpointInject annotation, Class<?> type, String injectionPointName) {
+        Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name(), injectionPointName);
         if (endpoint != null) {
             if (type.isInstance(endpoint)) {
                 return endpoint;
@@ -229,18 +232,17 @@
         return null;
     }
 
-    protected Endpoint getEndpointInjection(String uri, String name) {
+    protected Endpoint getEndpointInjection(String uri, String name, String injectionPointName) {
         Endpoint endpoint = null;
         if (isNotNullAndNonEmpty(uri)) {
             endpoint = camelContext.getEndpoint(uri);
         } else {
-            if (isNotNullAndNonEmpty(name)) {
-                endpoint = (Endpoint)applicationContext.getBean(name);
-                if (endpoint == null) {
-                    throw new NoSuchBeanDefinitionException(name);
-                }
-            } else {
-                LOG.warn("No uri or name specified on @EndpointInject annotation!");
+            if (isNullOrBlank(name)) {
+                name = injectionPointName;
+            }
+            endpoint = (Endpoint) applicationContext.getBean(name);
+            if (endpoint == null) {
+                throw new NoSuchBeanDefinitionException(name);
             }
         }
         return endpoint;