camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r563428 - in /activemq/camel/trunk/camel-core/src/main: java/org/apache/camel/model/ java/org/apache/camel/processor/ resources/org/apache/camel/model/
Date Tue, 07 Aug 2007 08:50:44 GMT
Author: jstrachan
Date: Tue Aug  7 01:50:43 2007
New Revision: 563428

URL: http://svn.apache.org/viewvc?view=rev&rev=563428
Log:
added initial implementation of the try/catch/finally DSL

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchType.java
      - copied, changed from r563410, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyType.java
      - copied, changed from r563410, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryType.java
      - copied, changed from r563410, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java
Modified:
    activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchType.java
(from r563410, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenType.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchType.java?view=diff&rev=563428&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenType.java&r1=563410&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchType.java&r2=563428
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchType.java Tue
Aug  7 01:50:43 2007
@@ -17,36 +17,100 @@
  */
 package org.apache.camel.model;
 
-import org.apache.camel.Predicate;
+import org.apache.camel.Processor;
 import org.apache.camel.impl.RouteContext;
-import org.apache.camel.model.language.ExpressionType;
-import org.apache.camel.processor.FilterProcessor;
+import org.apache.camel.processor.CatchProcessor;
+import org.apache.camel.util.ObjectHelper;
 
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementRef;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @version $Revision: 1.1 $
  */
-@XmlRootElement(name = "when")
-public class WhenType extends ExpressionNode {
-    public WhenType() {
+@XmlRootElement(name = "catch")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class CatchType extends ProcessorType {
+    @XmlElement(required = false)
+    private List<InterceptorRef> interceptors = new ArrayList<InterceptorRef>();
+    @XmlElement
+    private List<String> exceptions = new ArrayList<String>();
+    @XmlElementRef
+    private List<ProcessorType> outputs = new ArrayList<ProcessorType>();
+    @XmlTransient
+    private List<Class> exceptionClasses;
+
+    public CatchType() {
     }
 
-    public WhenType(Predicate predicate) {
-        super(predicate);
+    public CatchType(List<Class> exceptionClasses) {
+        this.exceptionClasses = exceptionClasses;
     }
 
-    public WhenType(ExpressionType expression) {
-        super(expression);
+    public CatchType(Class exceptionType) {
+        exceptionClasses = new ArrayList<Class>();
+        exceptionClasses.add(exceptionType);
     }
 
     @Override
     public String toString() {
-        return "When[ " + getExpression() + " -> " + getOutputs() + "]";
+        return "Catch[ " + getExceptionClasses() + " -> " + getOutputs() + "]";
     }
 
     @Override
-    public FilterProcessor createProcessor(RouteContext routeContext) throws Exception {
-        return createFilterProcessor(routeContext);
+    public CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
+        Processor childProcessor = routeContext.createProcessor(this);
+        return new CatchProcessor(getExceptionClasses(), childProcessor);
+    }
+
+    public List<InterceptorRef> getInterceptors() {
+        return interceptors;
+    }
+
+    public void setInterceptors(List<InterceptorRef> interceptors) {
+        this.interceptors = interceptors;
+    }
+
+    public List<ProcessorType> getOutputs() {
+        return outputs;
+    }
+
+    public void setOutputs(List<ProcessorType> outputs) {
+        this.outputs = outputs;
+    }
+
+    public List<Class> getExceptionClasses() {
+        if (exceptionClasses == null) {
+            exceptionClasses = createExceptionClasses();
+        }
+        return exceptionClasses;
+    }
+
+    public void setExceptionClasses(List<Class> exceptionClasses) {
+        this.exceptionClasses = exceptionClasses;
+    }
+
+    public List<String> getExceptions() {
+        return exceptions;
+    }
+
+    public void setExceptions(List<String> exceptions) {
+        this.exceptions = exceptions;
+    }
+
+    protected List<Class> createExceptionClasses() {
+        List<String> list = getExceptions();
+        List<Class> answer = new ArrayList<Class>(list.size());
+        for (String name : list) {
+            Class type = ObjectHelper.loadClass(name, getClass().getClassLoader());
+            answer.add(type);
+        }
+        return answer;
     }
 }

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyType.java
(from r563410, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyType.java?view=diff&rev=563428&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java&r1=563410&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyType.java&r2=563428
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyType.java
Tue Aug  7 01:50:43 2007
@@ -27,12 +27,12 @@
 /**
  * @version $Revision: 1.1 $
  */
-@XmlRootElement(name = "otherwise")
+@XmlRootElement(name = "finally")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class OtherwiseType extends OutputType {
+public class FinallyType extends OutputType {
     @Override
     public String toString() {
-        return "Otherwise[" + getOutputs() + "]";
+        return "Finally[" + getOutputs() + "]";
     }
 
     @Override

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryType.java
(from r563410, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryType.java?view=diff&rev=563428&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java&r1=563410&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryType.java&r2=563428
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryType.java Tue
Aug  7 01:50:43 2007
@@ -18,11 +18,10 @@
 package org.apache.camel.model;
 
 import org.apache.camel.Endpoint;
-import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.RouteContext;
-import org.apache.camel.processor.ChoiceProcessor;
-import org.apache.camel.processor.FilterProcessor;
+import org.apache.camel.processor.TryProcessor;
+import org.apache.camel.processor.CatchProcessor;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -37,101 +36,102 @@
 /**
  * @version $Revision: 1.1 $
  */
-@XmlRootElement(name = "choice")
+@XmlRootElement(name = "try")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class ChoiceType extends ProcessorType {
+public class TryType extends ProcessorType {
     @XmlElement(required = false)
     private List<InterceptorRef> interceptors = new ArrayList<InterceptorRef>();
     @XmlElementRef
-    private List<WhenType> whenClauses = new ArrayList<WhenType>();
+    private List<CatchType> catchClauses = new ArrayList<CatchType>();
     @XmlElement(required = false)
-    private OtherwiseType otherwise;
+    private FinallyType finallyClause;
 
     @Override
     public String toString() {
-        return "Choice[ " + getWhenClauses() + " " + getOtherwise() + "]";
+        return "Try[ " + getCatchClauses() + " " + getFinallyClause() + "]";
     }
 
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
-        List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
-        for (WhenType whenClaus : whenClauses) {
-            filters.add(whenClaus.createProcessor(routeContext));
+        Processor tryProcessor = routeContext.createProcessor(this);
+        Processor finallyProcessor = null;
+        if (finallyClause != null) {
+            finallyProcessor = finallyClause.createProcessor(routeContext);
         }
-        Processor otherwiseProcessor = null;
-        if (otherwise != null) {
-            otherwiseProcessor = otherwise.createProcessor(routeContext);
+        List<CatchProcessor> catchProcessors = new ArrayList<CatchProcessor>();
+        for (CatchType catchClause : catchClauses) {
+            catchProcessors.add(catchClause.createProcessor(routeContext));
         }
-        return new ChoiceProcessor(filters, otherwiseProcessor);
+        return new TryProcessor(tryProcessor, catchProcessors, finallyProcessor);
     }
 
     // Fluent API
     //-------------------------------------------------------------------------
-    public ChoiceType when(Predicate predicate) {
-        getWhenClauses().add(new WhenType(predicate));
+    public TryType when(Class exceptionType) {
+        getCatchClauses().add(new CatchType(exceptionType));
         return this;
     }
 
-    public OtherwiseType otherwise() {
-        OtherwiseType answer = new OtherwiseType();
-        setOtherwise(answer);
+    public FinallyType otherwise() {
+        FinallyType answer = new FinallyType();
+        setFinallyClause(answer);
         return answer;
     }
 
-    public ChoiceType to(Endpoint endpoint) {
+    public TryType to(Endpoint endpoint) {
         super.to(endpoint);
         return this;
     }
 
-    public ChoiceType to(Collection<Endpoint> endpoints) {
+    public TryType to(Collection<Endpoint> endpoints) {
         super.to(endpoints);
         return this;
     }
 
-    public ChoiceType to(Endpoint... endpoints) {
+    public TryType to(Endpoint... endpoints) {
         super.to(endpoints);
         return this;
     }
 
-    public ChoiceType to(String uri) {
+    public TryType to(String uri) {
         super.to(uri);
         return this;
     }
 
-    public ChoiceType to(String... uris) {
+    public TryType to(String... uris) {
         super.to(uris);
         return this;
     }
 
     // Properties
     //-------------------------------------------------------------------------
-    public List<WhenType> getWhenClauses() {
-        return whenClauses;
+    public List<CatchType> getCatchClauses() {
+        return catchClauses;
     }
 
-    public void setWhenClauses(List<WhenType> whenClauses) {
-        this.whenClauses = whenClauses;
+    public void setCatchClauses(List<CatchType> catchClauses) {
+        this.catchClauses = catchClauses;
     }
 
     public List<ProcessorType> getOutputs() {
-        if (otherwise != null) {
-            return otherwise.getOutputs();
+        if (finallyClause != null) {
+            return finallyClause.getOutputs();
         }
-        else if (whenClauses.isEmpty()) {
+        else if (catchClauses.isEmpty()) {
             return Collections.EMPTY_LIST;
         }
         else {
-            WhenType when = whenClauses.get(whenClauses.size() - 1);
+            CatchType when = catchClauses.get(catchClauses.size() - 1);
             return when.getOutputs();
         }
     }
 
-    public OtherwiseType getOtherwise() {
-        return otherwise;
+    public FinallyType getFinallyClause() {
+        return finallyClause;
     }
 
-    public void setOtherwise(OtherwiseType otherwise) {
-        this.otherwise = otherwise;
+    public void setFinallyClause(FinallyType finallyClause) {
+        this.finallyClause = finallyClause;
     }
 
     public List<InterceptorRef> getInterceptors() {
@@ -141,5 +141,4 @@
     public void setInterceptors(List<InterceptorRef> interceptors) {
         this.interceptors = interceptors;
     }
-}
-
+}
\ No newline at end of file

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java?view=auto&rev=563428
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
Tue Aug  7 01:50:43 2007
@@ -0,0 +1,49 @@
+/*
+ * 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.Processor;
+import org.apache.camel.Exchange;
+
+import java.util.List;
+
+/**
+ * @version $Revision: $
+ */
+public class CatchProcessor implements Processor {
+    private List<Class> exceptions;
+    private Processor processor;
+
+    public CatchProcessor(List<Class> exceptions, Processor processor) {
+        this.exceptions = exceptions;
+        this.processor = processor;
+    }
+
+
+    public boolean catches(Exception e) {
+        for (Class type : exceptions) {
+            if (type.isInstance(e)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        processor.process(exchange);
+    }
+}

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java?view=auto&rev=563428
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java
Tue Aug  7 01:50:43 2007
@@ -0,0 +1,82 @@
+/*
+ * 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.Exchange;
+import org.apache.camel.Processor;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.List;
+
+/**
+ * Implements try/catch/finally type processing
+ *
+ * @version $Revision: $
+ */
+public class TryProcessor implements Processor {
+    private static final Log log = LogFactory.getLog(TryProcessor.class);
+
+    private final Processor tryProcessor;
+    private final List<CatchProcessor> catchClauses;
+    private final Processor finallyProcessor;
+
+    public TryProcessor(Processor tryProcessor, List<CatchProcessor> catchClauses,
Processor finallyProcessor) {
+        this.tryProcessor = tryProcessor;
+        this.catchClauses = catchClauses;
+        this.finallyProcessor = finallyProcessor;
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        boolean doneTry = false;
+        try {
+            tryProcessor.process(exchange);
+            doneTry = true;
+
+            if (finallyProcessor != null) {
+                finallyProcessor.process(exchange);
+            }
+        }
+        catch (Exception e) {
+            handleException(exchange, e);
+            
+            if (!doneTry && finallyProcessor != null) {
+                try {
+                    finallyProcessor.process(exchange);
+                }
+                catch (Exception e2) {
+                    log.warn("Caught exception in finally block while handling other exception:
" + e2, e2);
+                }
+            }
+        }
+    }
+
+    protected void handleException(Exchange exchange, Exception e) throws Exception {
+        for (CatchProcessor catchClause : catchClauses) {
+            if (catchClause.catches(e)) {
+                try {
+                    catchClause.process(exchange);
+                }
+                catch (Exception e1) {
+                    log.warn("Caught exception inside catch clause: " + e1, e1);
+                    throw e1;
+                }
+                break;
+            }
+        }
+    }
+}

Modified: activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index?view=diff&rev=563428&r1=563427&r2=563428
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index (original)
+++ activemq/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index Tue
Aug  7 01:50:43 2007
@@ -16,9 +16,11 @@
 ## ------------------------------------------------------------------------
 AggregatorType
 BeanRef
+CatchType
 ChoiceType
 DelayerType
 FilterType
+FinallyType
 FromType
 IdempotentConsumerType
 MulticastType
@@ -32,4 +34,5 @@
 SplitterType
 ThrottlerType
 ToType
+TryType
 WhenType



Mime
View raw message