Author: davsclaus
Date: Sat Oct 11 09:34:49 2008
New Revision: 703680

URL: http://svn.apache.org/viewvc?rev=703680&view=rev
Log:
CAMEL-976: Added example for new handled policy for ExceptionType. Polished 
code. Added handled(Expression) DSL. Added OFF logging level.

Added:
    
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java
Modified:
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Logger.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoggingLevel.java
    
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelRedeliveryTest.java
    activemq/camel/trunk/camel-core/src/test/resources/log4j.properties

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java?rev=703680&r1=703679&r2=703680&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
 Sat Oct 11 09:34:49 2008
@@ -147,7 +147,7 @@
     /**
      * Sets the exception policy to use
      */
-    public ErrorHandlerBuilderSupport 
exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
+    public DeadLetterChannelBuilder 
exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
         setExceptionPolicyStrategy(exceptionPolicyStrategy);
         return this;
     }

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java?rev=703680&r1=703679&r2=703680&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
 Sat Oct 11 09:34:49 2008
@@ -27,10 +27,12 @@
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
+import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.builder.ErrorHandlerBuilder;
+import static org.apache.camel.builder.PredicateBuilder.toPredicate;
 import org.apache.camel.language.constant.ConstantLanguage;
 import org.apache.camel.processor.CatchProcessor;
 import org.apache.camel.processor.RedeliveryPolicy;
@@ -119,16 +121,21 @@
 
     // Fluent API
     //-------------------------------------------------------------------------
-    public ExceptionType handled(boolean cond) {
+    public ExceptionType handled(boolean handled) {
         ConstantLanguage constant = new ConstantLanguage();
-        return handled(constant.createPredicate(Boolean.toString(cond)));
+        return handled(constant.createPredicate(Boolean.toString(handled)));
     }
     
-    public ExceptionType handled(Predicate cond) {
-        setHandledPolicy(cond);
+    public ExceptionType handled(Predicate handled) {
+        setHandledPolicy(handled);
         return this;
     }
     
+    public ExceptionType handled(Expression handled) {
+        setHandledPolicy(toPredicate(handled));
+        return this;
+    }
+
     public ExceptionType backOffMultiplier(double backOffMultiplier) {
         getOrCreateRedeliveryPolicy().backOffMultiplier(backOffMultiplier);
         return this;

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java?rev=703680&r1=703679&r2=703680&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
 Sat Oct 11 09:34:49 2008
@@ -45,7 +45,6 @@
     public static final String REDELIVERED = "org.apache.camel.Redelivered";
     public static final String EXCEPTION_CAUSE_PROPERTY = 
"CamelCauseException";
 
-
     private static final transient Log LOG = 
LogFactory.getLog(DeadLetterChannel.class);
     private static final String FAILURE_HANDLED_PROPERTY = 
DeadLetterChannel.class.getName() + ".FAILURE_HANDLED";
     private Processor output;
@@ -208,9 +207,15 @@
 
     protected static void restoreExceptionOnExchange(Exchange exchange, 
Predicate handledPredicate) {
         if (handledPredicate == null || !handledPredicate.matches(exchange)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("This exchange is not handled so its marked as 
failed: " + exchange);
+            }
             // exception not handled, put exception back in the exchange
             
exchange.setException(exchange.getProperty(FAILURE_HANDLED_PROPERTY, 
Throwable.class));
         } else {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("This exchange is handled so its marked as not 
failed: " + exchange);
+            }
             exchange.setProperty(Exchange.EXCEPTION_HANDLED_PROPERTY, 
Boolean.TRUE);
         }
     }

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Logger.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Logger.java?rev=703680&r1=703679&r2=703680&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Logger.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Logger.java
 Sat Oct 11 09:34:49 2008
@@ -208,6 +208,7 @@
                 log.warn(message, exception);
             }
             break;
+        case OFF: break;
         default:
             log.error("Unknown level: " + level + " when trying to log 
exchange: " + message, exception);
         }

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoggingLevel.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoggingLevel.java?rev=703680&r1=703679&r2=703680&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoggingLevel.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/LoggingLevel.java
 Sat Oct 11 09:34:49 2008
@@ -22,5 +22,5 @@
  * @version $Revision$
  */
 public enum LoggingLevel {
-    DEBUG, ERROR, FATAL, INFO, TRACE, WARN
+    DEBUG, ERROR, FATAL, INFO, TRACE, WARN, OFF
 }

Added: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java?rev=703680&view=auto
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java
 (added)
+++ 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelHandledExampleTest.java
 Sat Oct 11 09:34:49 2008
@@ -0,0 +1,145 @@
+/**
+ * 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 java.util.Map;
+
+import org.apache.camel.Body;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Headers;
+import org.apache.camel.OutHeaders;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test to verify that handled policy is working as expected for wiki 
documentation.
+ */
+public class DeadLetterChannelHandledExampleTest 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 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);
+
+        Object out = template.requestBodyAndHeader("direct:start", "Order: 
kaboom", "customerid", "555");
+        assertEquals("Order ERROR", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                // we do special error handling for when OrderFailedException 
is thrown
+                onException(OrderFailedException.class)
+                    // we mark the exchange as handled so the caller doesn't 
receive the
+                    // OrderFailedException but whatever we want to return 
instead
+                    .handled(true)
+                    // this bean handles the error handling where we can 
customize the error
+                    // response using java code
+                    .bean(OrderService.class, "orderFailed")
+                    // and since this is an unit test we use mocks for testing
+                    .to("mock:error");
+
+                // this is just the generic error handler where we set the 
destination
+                // and the number of redeliveries we want to try
+                
errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(1));
+
+                // this is our route where we handle orders
+                from("direct:start")
+                    // this bean is our order service
+                    .bean(OrderService.class, "handleOrder")
+                    // this is the destination if the order is OK
+                    .to("mock:result");
+                // END SNIPPET: e1
+            }
+        };
+    }
+
+    // START SNIPPET: e2
+    /**
+     * Order service as a plain POJO class
+     */
+    public static class OrderService {
+
+        /**
+         * This method handle our order input and return the order
+         *
+         * @param in      the in headers
+         * @param payload the in payload
+         * @param out     the out headers
+         * @return the out payload
+         * @throws OrderFailedException is thrown if the order can not be 
processed
+         */
+        public Object handleOrder(@Headers Map in, @Body String payload, 
@OutHeaders Map out)
+            throws OrderFailedException {
+            out.put("customerid", in.get("customerid"));
+            if ("Order: kaboom".equals(payload)) {
+                throw new OrderFailedException("Can not order: kaboom");
+            } else {
+                out.put("orderid", "123");
+                return "Order OK";
+            }
+        }
+
+        /**
+         * This method creates the response to the caller if the order could 
not be processed
+         * @param in      the in headers
+         * @param payload the in payload
+         * @param out     the out headers
+         * @return the out payload
+         */
+        public Object orderFailed(@Headers Map in, @Body String payload, 
@OutHeaders Map out) {
+            out.put("customerid", in.get("customerid"));
+            out.put("orderid", "failed");
+            return "Order ERROR";
+        }
+    }
+    // END SNIPPET: e2
+
+    // START SNIPPET: e3
+    /**
+     * Exception thrown if the order can not be processed
+     */
+    public static class OrderFailedException extends Exception {
+
+        public OrderFailedException(String message) {
+            super(message);
+        }
+        
+    }
+    // END SNIPPET: e3
+
+}
\ No newline at end of file

Modified: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelRedeliveryTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelRedeliveryTest.java?rev=703680&r1=703679&r2=703680&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelRedeliveryTest.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelRedeliveryTest.java
 Sat Oct 11 09:34:49 2008
@@ -24,7 +24,7 @@
 import org.apache.camel.component.mock.MockEndpoint;
 
 /**
- * Unit test to verift that redelivery counters is working as expected.
+ * Unit test to verify that redelivery counters is working as expected.
  */
 public class DeadLetterChannelRedeliveryTest extends ContextTestSupport {
 

Modified: activemq/camel/trunk/camel-core/src/test/resources/log4j.properties
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/resources/log4j.properties?rev=703680&r1=703679&r2=703680&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/resources/log4j.properties 
(original)
+++ activemq/camel/trunk/camel-core/src/test/resources/log4j.properties Sat Oct 
11 09:34:49 2008
@@ -22,6 +22,7 @@
 
 log4j.logger.org.apache.activemq.spring=WARN
 log4j.logger.org.apache.camel=DEBUG
+log4j.logger.org.apache.camel.impl.converter=WARN
 
 # CONSOLE appender not used by default
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender


Reply via email to