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 2010/01/29 09:00:32 UTC

svn commit: r904408 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/bean/ components/camel-spring/ components/camel-spring/src/test/java/org/apache/camel/spring/aop/ components/camel-spring/src/test/resources/org/apache/camel/spr...

Author: davsclaus
Date: Fri Jan 29 08:00:32 2010
New Revision: 904408

URL: http://svn.apache.org/viewvc?rev=904408&view=rev
Log:
CAMEL-2384: Bean parameter binding using Camel annotations now works with CGLIB enhanced classes.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/ExceptionInterceptor.java   (with props)
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/MyCoolAopBean.java   (with props)
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.java   (with props)
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml
      - copied, changed from r903943, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
    camel/trunk/components/camel-spring/pom.xml

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=904408&r1=904407&r2=904408&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Fri Jan 29 08:00:32 2010
@@ -59,6 +59,7 @@
  */
 public class BeanInfo {
     private static final transient Log LOG = LogFactory.getLog(BeanInfo.class);
+    private static final String CGLIB_CLASS_SEPARATOR = "$$";
     private static final List<Method> EXCLUDED_METHODS = new ArrayList<Method>();
     private final CamelContext camelContext;
     private final Class<?> type;
@@ -87,8 +88,6 @@
                 EXCLUDED_METHODS.addAll(Arrays.asList(Object.class.getMethods()));
                 // exclude all java.lang.reflect.Proxy methods as we dont want to invoke them
                 EXCLUDED_METHODS.addAll(Arrays.asList(Proxy.class.getMethods()));
-
-                // TODO: AOP proxies have additional methods - well known methods should be added to EXCLUDE_METHODS
             }
         }
 
@@ -179,6 +178,9 @@
      * @param clazz the class
      */
     protected void introspect(Class<?> clazz) {
+        // get the target clazz as it could potentially have been enhanced by CGLIB etc.
+        clazz = getTargetClass(clazz);
+
         if (LOG.isTraceEnabled()) {
             LOG.trace("Introspecting class: " + clazz);
         }
@@ -662,4 +664,11 @@
         }
     }
 
+    private static Class<?> getTargetClass(Class<?> clazz) {
+        if (clazz.getName().indexOf(CGLIB_CLASS_SEPARATOR) != -1) {
+            return clazz.getSuperclass();
+        };
+        return clazz;
+    }
+
 }

Modified: camel/trunk/components/camel-spring/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/pom.xml?rev=904408&r1=904407&r2=904408&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/pom.xml (original)
+++ camel/trunk/components/camel-spring/pom.xml Fri Jan 29 08:00:32 2010
@@ -116,6 +116,32 @@
       <scope>test</scope>
     </dependency>
 
+      <!-- for testing Spring AOP at class level -->
+      <dependency>
+        <groupId>org.aspectj</groupId>
+        <artifactId>aspectjrt</artifactId>
+        <version>1.6.2</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.aspectj</groupId>
+        <artifactId>aspectjweaver</artifactId>
+        <version>1.6.2</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>cglib</groupId>
+        <artifactId>cglib</artifactId>
+        <version>2.2</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>asm</groupId>
+        <artifactId>asm</artifactId>
+        <version>3.1</version>
+        <scope>test</scope>
+      </dependency>
+
   </dependencies>
 
   <repositories>

Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/ExceptionInterceptor.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/ExceptionInterceptor.java?rev=904408&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/ExceptionInterceptor.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/ExceptionInterceptor.java Fri Jan 29 08:00:32 2010
@@ -0,0 +1,33 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.aop;
+
+/**
+ * @version $Revision$
+ */
+public class ExceptionInterceptor {
+
+    private Exception e;
+
+    public void logException(Exception e) {
+        this.e = e;
+    }
+
+    public Exception getE() {
+        return e;
+    }
+}

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/ExceptionInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/ExceptionInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/MyCoolAopBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/MyCoolAopBean.java?rev=904408&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/MyCoolAopBean.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/MyCoolAopBean.java Fri Jan 29 08:00:32 2010
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.aop;
+
+import java.util.Map;
+
+import org.apache.camel.Body;
+import org.apache.camel.Header;
+import org.apache.camel.Headers;
+
+/**
+ * @version $Revision$
+ */
+public class MyCoolAopBean {
+
+    public String hello(@Body String body, @Header("foo") String foo, @Headers Map headers) {
+        String s = body.replaceFirst("Hello", "Bye");
+
+        if (!foo.equals("ABC")) {
+            throw new IllegalArgumentException("Foo has not expected value ABC but " + foo);
+        }
+
+        headers.put("foo", 123);
+        return s;
+    }
+    
+}

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/MyCoolAopBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/MyCoolAopBean.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.java?rev=904408&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.java Fri Jan 29 08:00:32 2010
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.aop;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Spring AOP will proxy a bean at class level which is also a bean that Camel should invoke
+ * using its bean component. The test should test that Camel bean binding annotations works.
+ *
+ * @version $Revision$
+ */
+public class SpringAopClassLevelCamelBeanTest extends SpringTestSupport {
+
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml");
+    }
+
+    public void testSpringAopOk() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Bye World");
+        mock.expectedHeaderReceived("foo", 123);
+
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "ABC");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testSpringAopException() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(0);
+
+        try {
+            template.sendBodyAndHeader("direct:start", "Hello World", "foo", "Damn");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+        }
+
+        assertMockEndpointsSatisfied();
+
+        ExceptionInterceptor ei = context.getRegistry().lookup("exceptionInterceptor", ExceptionInterceptor.class);
+        IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, ei.getE());
+        assertEquals("Foo has not expected value ABC but Damn", iae.getMessage());
+    }
+
+}

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml (from r903943, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml&r1=903943&r2=904408&rev=904408&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml Fri Jan 29 08:00:32 2010
@@ -17,20 +17,38 @@
 -->
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
+       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-  <!-- START SNIPPET: e1 -->
-  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
-    <route>
-      <from uri="direct:start"/>
-      <to uri="myBean"/>
-      <to uri="mock:results"/>
-    </route>
-  </camelContext>
+    <context:component-scan base-package="org.apache.camel.spring.aop"/>
+
+    <aop:config>
+        <aop:pointcut id="servicePointcut" expression="execution(* org.apache.camel.spring.aop.*Bean+.*(..))"/>
+
+        <aop:aspect id="exceptionAspect" ref="exceptionInterceptor">
+            <aop:after-throwing
+                    method="logException"
+                    pointcut-ref="servicePointcut"
+                    throwing="e"/>
+        </aop:aspect>
+    </aop:config>
+
+    <bean id="exceptionInterceptor" class="org.apache.camel.spring.aop.ExceptionInterceptor"/>
+
+    <bean id="myBean" class="org.apache.camel.spring.aop.MyCoolAopBean"/>
+
+    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+        <route>
+            <from uri="direct:start"/>
+            <to uri="myBean"/>
+            <to uri="mock:result"/>
+        </route>
+    </camelContext>
 
-  <bean id="myBean" class="org.apache.camel.spring.bind.ExampleBean"/>
-  <!-- END SNIPPET: e1 -->
 </beans>