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/05/18 09:06:31 UTC

svn commit: r775844 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/processor/ camel-core/src/main/resources/org/apache/camel/model/ camel-core/src/test/java/org/apache/camel/model/ camel-co...

Author: davsclaus
Date: Mon May 18 07:06:21 2009
New Revision: 775844

URL: http://svn.apache.org/viewvc?rev=775844&view=rev
Log:
CAMEL-1557: throwFault removed. Added throwException instead.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowExceptionDefinition.java
      - copied, changed from r775808, camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java
      - copied, changed from r775808, camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionTest.java   (with props)
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrowExceptionTest.java
      - copied, changed from r775808, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringThrowExceptionTest.xml
      - copied, changed from r775808, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.xml
Removed:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
    camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
    camel/trunk/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java?rev=775844&r1=775843&r2=775844&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ConvertBodyDefinition.java Mon May 18 07:06:21 2009
@@ -29,7 +29,6 @@
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.processor.ConvertBodyProcessor;
 import org.apache.camel.spi.RouteContext;
-import org.apache.camel.util.ObjectHelper;
 
 /**
  * Represents an XML <convertBodyTo/> element
@@ -74,6 +73,13 @@
 
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
+        if (getTypeClass() == null) {
+            this.typeClass = routeContext.getCamelContext().getClassResolver().resolveClass(getType());
+            if (getTypeClass() == null) {
+                throw new RuntimeCamelException("Cannot load the class with the class name: " + getType());
+            }
+        }
+
         return new ConvertBodyProcessor(getTypeClass(), getCharset());
     }
 
@@ -83,16 +89,16 @@
         return Collections.EMPTY_LIST;
     }
 
-    protected Class createTypeClass() {
-        return ObjectHelper.loadClass(getType(), getClass().getClassLoader());
+    public String getType() {
+        return type;
     }
 
     public void setType(String type) {
         this.type = type;
     }
 
-    public String getType() {
-        return type;
+    public Class getTypeClass() {
+        return typeClass;
     }
 
     public void setTypeClass(Class typeClass) {
@@ -107,15 +113,4 @@
         this.charset = charset;
     }
 
-    public Class getTypeClass() {
-        if (typeClass == null) {
-            Class clazz = createTypeClass();
-            if (clazz == null) {
-                throw new RuntimeCamelException("Cannot load the class with the class name: " + getType());
-            }
-            setTypeClass(clazz);
-        }
-        return typeClass;
-    }
-    
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=775844&r1=775843&r2=775844&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Mon May 18 07:06:21 2009
@@ -1225,32 +1225,20 @@
     }
 
     /**
-     * Creates a fault message based on the given throwable.
+     * Sets the exception on the {@link org.apache.camel.Exchange}
      *
-     * @param fault   the fault
+     * @param exception the exception to throw
      * @return the builder
-     * @deprecated should be renamed to throw Exception
      */
     @SuppressWarnings("unchecked")
-    public Type throwFault(Throwable fault) {
-        ThrowFaultDefinition answer = new ThrowFaultDefinition();
-        answer.setFault(fault);
+    public Type throwException(Exception exception) {
+        ThrowExceptionDefinition answer = new ThrowExceptionDefinition();
+        answer.setException(exception);
         addOutput(answer);
         return (Type) this;
     }
 
     /**
-     * Creates a fault message based on the given message.
-     *
-     * @param message  the fault message
-     * @return the builder
-     * @deprecated should be renamed to throw Exception
-     */
-    public Type throwFault(String message) {
-        return throwFault(new CamelException(message));
-    }
-
-    /**
      * Marks the exchange for rollback only.
      * <p/>
      * This is done by setting a {@link org.apache.camel.RollbackExchangeException} on the Exchange

Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowExceptionDefinition.java (from r775808, camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultDefinition.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowExceptionDefinition.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowExceptionDefinition.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultDefinition.java&r1=775808&r2=775844&rev=775844&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowFaultDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ThrowExceptionDefinition.java Mon May 18 07:06:21 2009
@@ -18,78 +18,49 @@
 
 import java.util.Collections;
 import java.util.List;
-
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
-import org.apache.camel.CamelException;
 import org.apache.camel.Processor;
-import org.apache.camel.processor.ThrowFaultProcessor;
+import org.apache.camel.processor.ThrowExceptionProcessor;
 import org.apache.camel.spi.RouteContext;
+import org.apache.camel.util.ObjectHelper;
 
 /**
- * Represents an XML &lt;throwFault/&gt; element
- * @deprecated should be renamed to throwException and not be FAULT based
+ * Represents an XML &lt;throwException/&gt; element
  */
-@XmlRootElement(name = "throwFault")
+@XmlRootElement(name = "throwException")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class ThrowFaultDefinition extends ProcessorDefinition<ThrowFaultDefinition> {
-    @XmlTransient
-    private Throwable fault;
+public class ThrowExceptionDefinition extends ProcessorDefinition<ThrowExceptionDefinition> {
+    @XmlAttribute(name = "ref", required = false)
+    private String ref;
     @XmlTransient
-    private Processor processor;
-    @XmlAttribute (required = true)
-    private String faultRef;
+    private Exception exception;
 
-    public ThrowFaultDefinition() {
+    public ThrowExceptionDefinition() {
     }
 
     @Override
     public String getShortName() {
-        return "throwFault";
+        return "throwException";
     }
 
     @Override
     public String toString() {
-        if (faultRef != null) {
-            return "ThrowFault[ref: " + faultRef + "]";
-        } else {
-            return "ThrowFault[" + fault.getClass().getCanonicalName();
-        }
-    }
-
-    public void setFault(Throwable fault) {
-        this.fault = fault;
-    }
-
-    public Throwable getFault() {
-        return fault;
-    }
-
-    public void setFaultRef(String ref) {
-        this.faultRef = ref;
-    }
-
-    public String getFaultRef() {
-        return faultRef;
+        return "ThrowException[" + (exception != null ? exception.getClass().getCanonicalName() : "ref: " + ref) + "]";
     }
 
     @Override
     public Processor createProcessor(RouteContext routeContext) {
-        if (processor == null) {
-            if (fault == null) {
-                fault = routeContext.lookup(faultRef, Throwable.class);
-                if (fault == null) {
-                    // can't find the fault instance, create a new one
-                    fault = new CamelException(faultRef);
-                }
-            }
-            processor = new ThrowFaultProcessor(fault);
+        if (ref != null && exception == null) {
+            this.exception = routeContext.getCamelContext().getRegistry().lookup(ref, Exception.class);
         }
-        return processor;
+
+        ObjectHelper.notNull(exception, "exception or ref", this);
+        return new ThrowExceptionProcessor(exception);
     }
 
     @Override
@@ -97,4 +68,12 @@
     public List<ProcessorDefinition> getOutputs() {
         return Collections.EMPTY_LIST;
     }
-}
+
+    public Exception getException() {
+        return exception;
+    }
+
+    public void setException(Exception exception) {
+        this.exception = exception;
+    }
+}
\ No newline at end of file

Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java (from r775808, camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java&r1=775808&r2=775844&rev=775844&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowFaultProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java Mon May 18 07:06:21 2009
@@ -14,30 +14,26 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.camel.processor;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.Message;
 import org.apache.camel.Processor;
 
 /**
- * The processor which implements the ThrowFault DSL
+ * The processor which implements the ThrowException DSL
  */
-public class ThrowFaultProcessor implements Processor {
-    private final Throwable fault;
+public class ThrowExceptionProcessor implements Processor {
+    private final Exception exception;
 
-    public ThrowFaultProcessor(Throwable fault) {
-        this.fault = fault;
+    public ThrowExceptionProcessor(Exception exception) {
+        this.exception = exception;
     }
 
     /**
-     * Set the fault message in the exchange
-     * @see org.apache.camel.Processor#process(org.apache.camel.Exchange)
+     * Set the exception in the exchange
      */
     public void process(Exchange exchange) throws Exception {
-        Message message = exchange.getFault();
-        message.setBody(fault);
+        exchange.setException(exception);
     }
 
-}
+}
\ No newline at end of file

Modified: camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index?rev=775844&r1=775843&r2=775844&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index (original)
+++ camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index Mon May 18 07:06:21 2009
@@ -63,7 +63,7 @@
 SplitDefinition
 StopDefinition
 ThrottleDefinition
-ThrowFaultDefinition
+ThrowExceptionDefinition
 ToDefinition
 TransactedDefinition
 TransformDefinition

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java?rev=775844&r1=775843&r2=775844&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java Mon May 18 07:06:21 2009
@@ -138,7 +138,6 @@
         assertFrom(route, "seda:a");
         ConvertBodyDefinition node = assertOneProcessorInstanceOf(ConvertBodyDefinition.class, route);
         assertEquals("java.lang.Integer", node.getType());
-        assertEquals(Integer.class, node.getTypeClass());
     }
 
     public void testParseRoutingSlipXml() throws Exception {

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionTest.java?rev=775844&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionTest.java Mon May 18 07:06:21 2009
@@ -0,0 +1,55 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version $Revision$
+ */
+public class ThrowExceptionTest extends ContextTestSupport {
+
+    public void testThrowException() throws Exception {
+        getMockEndpoint("mock:start").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:start", "Hello World");
+            fail("Should have thrown an exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertEquals("Forced", e.getCause().getMessage());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("mock:start")
+                        .throwException(new IllegalArgumentException("Forced"))
+                        .to("mock:result");
+            }
+        };
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrowExceptionTest.java (from r775808, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrowExceptionTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrowExceptionTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.java&r1=775808&r2=775844&rev=775844&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrowExceptionTest.java Mon May 18 07:06:21 2009
@@ -17,16 +17,16 @@
 package org.apache.camel.spring.processor;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.processor.OnCompletionAndInterceptAndOnExceptionGlobalTest;
+import org.apache.camel.processor.ThrowExceptionTest;
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
-
+                                  
 /**
  * @version $Revision$
  */
-public class SpringOnCompletionAndInterceptAndOnExceptionGlobalTest extends OnCompletionAndInterceptAndOnExceptionGlobalTest {
+public class SpringThrowExceptionTest extends ThrowExceptionTest {
 
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringThrowExceptionTest.xml");
     }
 
 }
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringThrowExceptionTest.xml (from r775808, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringThrowExceptionTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringThrowExceptionTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.xml&r1=775808&r2=775844&rev=775844&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringOnCompletionAndInterceptAndOnExceptionGlobalTest.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringThrowExceptionTest.xml Mon May 18 07:06:21 2009
@@ -22,27 +22,15 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <bean id="myProcessor" class="org.apache.camel.processor.OnCompletionTest$MyProcessor"/>
+    <bean id="myException" class="java.lang.IllegalArgumentException">
+        <constructor-arg index="0" value="Forced"/>
+    </bean>
 
     <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
-
-        <onException>
-            <exception>java.lang.Exception</exception>
-            <to uri="mock:exception"/>
-        </onException>
-
-        <onCompletion>
-            <to uri="log:global"/>
-            <to uri="mock:sync"/>
-        </onCompletion>
-
-        <intercept>
-            <to uri="mock:intercept"/>
-        </intercept>
-
         <route>
             <from uri="direct:start"/>
-            <process ref="myProcessor"/>
+            <to uri="mock:start"/>
+            <throwException ref="myException"/>
             <to uri="mock:result"/>
         </route>
     </camelContext>