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 2009/03/04 15:20:14 UTC

svn commit: r750017 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/util/ components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/ components/camel-spring/src/test/resources/org/apache/camel/spring/processor/one...

Author: davsclaus
Date: Wed Mar  4 14:20:14 2009
New Revision: 750017

URL: http://svn.apache.org/viewvc?rev=750017&view=rev
Log:
CAMEL-1418: Normalizes class names before loading to avoid \n or other chars by Spring DSL configuration with xml tags on newlines or hidden spaces etc.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionNotNormalizedClassNameTest.java
      - copied, changed from r749920, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionSubRouteTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionNotNormalizedClassNameTest.xml
      - copied, changed from r749920, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionSubRouteTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=750017&r1=750016&r2=750017&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Wed Mar  4 14:20:14 2009
@@ -505,6 +505,9 @@
      * @return the class or null if it could not be loaded
      */
     public static Class<?> loadClass(String name, ClassLoader loader) {
+        // must clean the name so its pure java name, eg remoing \n or whatever people can do in the Spring XML
+        name = normalizeClassName(name);
+
         // try context class loader first
         Class clazz = doLoadClass(name, Thread.currentThread().getContextClassLoader());
         if (clazz == null) {
@@ -876,4 +879,23 @@
         }
     }
 
+    /**
+     * Cleans the string to pure java identifier so we can use it for loading class names.
+     * <p/>
+     * Especially from Sping DSL people can have \n \t or other characters that otherwise
+     * would result in ClassNotFoundException
+     *
+     * @param name the class name
+     * @return normalized classname that can be load by a class loader.
+     */
+    public static String normalizeClassName(String name) {
+        StringBuffer sb = new StringBuffer(name.length());
+        for (char ch : name.toCharArray()) {
+            if (ch == '.' || Character.isJavaIdentifierPart(ch)) {
+                sb.append(ch);
+            }
+        }
+        return sb.toString();
+    }
+
 }

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionNotNormalizedClassNameTest.java (from r749920, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionSubRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionNotNormalizedClassNameTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionNotNormalizedClassNameTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionSubRouteTest.java&r1=749920&r2=750017&rev=750017&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionSubRouteTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/onexception/SpringOnExceptionNotNormalizedClassNameTest.java Wed Mar  4 14:20:14 2009
@@ -24,28 +24,13 @@
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
 
 /**
- * Unit test for onException with the spring DSL.
+ * Unit test for onException with expresion classname with a trailing \n the spring DSL.
  */
-public class SpringOnExceptionSubRouteTest extends ContextTestSupport {
-
-    public void testOrderOk() throws Exception {
-        MockEndpoint result = getMockEndpoint("mock:result");
-        result.expectedBodiesReceived("Order OK");
-        result.expectedHeaderReceived("orderid", "123");
-
-        MockEndpoint error = getMockEndpoint("mock:error");
-        error.expectedMessageCount(0);
-
-        Object out = template.requestBodyAndHeader("direct:start", "Order: MacBook Pro", "customerid", "444");
-        assertEquals("Order OK", out);
-
-        assertMockEndpointsSatisfied();
-    }
+public class SpringOnExceptionNotNormalizedClassNameTest extends ContextTestSupport {
 
     public void testOrderError() throws Exception {
         MockEndpoint error = getMockEndpoint("mock:error");
         error.expectedBodiesReceived("Order ERROR");
-        error.expectedHeaderReceived("orderid", "failed");
 
         MockEndpoint result = getMockEndpoint("mock:result");
         result.expectedMessageCount(0);
@@ -56,24 +41,7 @@
         assertMockEndpointsSatisfied();
     }
 
-    public void testOrderErrorWithNoExceptionClause() throws Exception {
-        MockEndpoint error = getMockEndpoint("mock:error");
-        error.expectedMessageCount(0);
-
-        MockEndpoint result = getMockEndpoint("mock:result");
-        result.expectedMessageCount(0);
-
-        try {
-            template.requestBodyAndHeader("direct:start_with_no_handler", "Order: kaboom", "customerid", "555");
-            fail("Should throw an Exception");
-        } catch (Exception e) {
-            assertEquals("Cannot order: kaboom", e.getCause().getMessage());
-        }        
-
-        assertMockEndpointsSatisfied();
-    }    
-    
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "/org/apache/camel/spring/processor/onexception/onExceptionSubRouteTest.xml");
+        return createSpringCamelContext(this, "/org/apache/camel/spring/processor/onexception/onExceptionNotNormalizedClassNameTest.xml");
     }
 }
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionNotNormalizedClassNameTest.xml (from r749920, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionSubRouteTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionNotNormalizedClassNameTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionNotNormalizedClassNameTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionSubRouteTest.xml&r1=749920&r2=750017&rev=750017&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionSubRouteTest.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/onexception/onExceptionNotNormalizedClassNameTest.xml Wed Mar  4 14:20:14 2009
@@ -22,37 +22,24 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <!-- START SNIPPET: e1 -->
-
-    <!-- this is our POJO bean with our business logic defined as a plain spring bean -->
     <bean id="orderService" class="org.apache.camel.spring.processor.onexception.OrderService" />
 
-    <!-- this is the camel context where we define the routes -->
     <camelContext xmlns="http://camel.apache.org/schema/spring">
 
-      <route>
-        <from uri="direct:start" />
         <onException>
-          <exception>org.apache.camel.spring.processor.onexception.OrderFailedException</exception>
-          <redeliveryPolicy maximumRedeliveries="1" />
-          <handled>
-            <constant>true</constant>
-          </handled>
-          <bean ref="orderService" method="orderFailed" />
-          <to uri="mock:error" />
+            <exception>   org.apache.camel.spring.processor.onexception.OrderFailedException
+            </exception>
+            <handled><constant>true</constant></handled>
+            <transform><constant>Order ERROR</constant></transform>
+            <to uri="mock:error"/>
         </onException>
-        <bean ref="orderService" method="handleOrder" />
-        <to uri="mock:result" />
-      </route>
-
-      <!-- The exception clause specified in the first route will not be used in this route -->
-      <route>
-        <from uri="direct:start_with_no_handler" />
-        <bean ref="orderService" method="handleOrder" />
-        <to uri="mock:result" />
-      </route>
+
+        <route>
+            <from uri="direct:start"/>
+            <bean ref="orderService" method="handleOrder"/>
+            <to uri="mock:result"/>
+        </route>
 
     </camelContext>
-    <!-- END SNIPPET: e1 -->
 
 </beans>