001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.processor;
018    
019    import java.util.List;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Processor;
023    import org.apache.camel.RuntimeCamelException;
024    import org.apache.camel.impl.ServiceSupport;
025    import org.apache.camel.util.ExchangeHelper;
026    import org.apache.camel.util.ServiceHelper;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * Implements try/catch/finally type processing
032     *
033     * @version $Revision: 41910 $
034     */
035    public class TryProcessor extends ServiceSupport implements Processor {
036        private static final transient Log LOG = LogFactory.getLog(TryProcessor.class);
037    
038        private final Processor tryProcessor;
039        private final List<CatchProcessor> catchClauses;
040        private final Processor finallyProcessor;
041    
042        public TryProcessor(Processor tryProcessor, List<CatchProcessor> catchClauses, Processor finallyProcessor) {
043            this.tryProcessor = tryProcessor;
044            this.catchClauses = catchClauses;
045            this.finallyProcessor = finallyProcessor;
046        }
047    
048        public String toString() {
049            String finallyText = (finallyProcessor == null) ? "" : " Finally {" + finallyProcessor + "}";
050            return "Try {" + tryProcessor + "} " + catchClauses + finallyText;
051        }
052    
053        public void process(Exchange exchange) throws Exception {
054            Throwable e = null;
055            try {
056                tryProcessor.process(exchange);
057                e = exchange.getException();
058    
059                // Ignore it if it was handled by the dead letter channel.
060                if (e != null && DeadLetterChannel.isFailureHandled(exchange)) {
061                    e = null;
062                }
063            } catch (Throwable ex) {
064                e = ex;
065                exchange.setException(e);
066            }
067    
068            Exception unexpected = null;
069            try {
070                if (e != null) {
071                    LOG.info("Caught exception while processing exchange.", e);
072                    handleException(exchange, e);
073                }
074            } catch (Exception ex) {
075                unexpected = ex;
076            } catch (Throwable ex) {
077                unexpected = new RuntimeCamelException(ex);
078            } finally {
079                try {
080                    processFinally(exchange);
081                } catch (Exception ex) {
082                    unexpected = ex;
083                } catch (Throwable ex) {
084                    unexpected = new RuntimeCamelException(ex);
085                }
086                if (unexpected != null) {
087                    LOG.warn("Caught exception inside processFinally clause.", unexpected);
088                    throw unexpected;
089                }
090            }
091    
092            if (unexpected != null) {
093                LOG.warn("Caught exception inside handle clause.", unexpected);
094                throw unexpected;
095            }
096        }
097    
098        protected void doStart() throws Exception {
099            ServiceHelper.startServices(tryProcessor, catchClauses, finallyProcessor);
100        }
101    
102        protected void doStop() throws Exception {
103            ServiceHelper.stopServices(tryProcessor, catchClauses, finallyProcessor);
104        }
105    
106        protected void handleException(Exchange exchange, Throwable e) throws Throwable {
107            for (CatchProcessor catchClause : catchClauses) {
108                if (catchClause.catches(e)) {
109                    // lets attach the exception to the exchange
110                    Exchange localExchange = exchange.copy();
111                    localExchange.getIn().setHeader("caught.exception", e);
112                    // give the rest of the pipeline another chance
113                    localExchange.setException(null);
114    
115                    // do not catch any exception here, let it propagate up
116                    catchClause.process(localExchange);
117                    localExchange.getIn().removeHeader("caught.exception");
118                    ExchangeHelper.copyResults(exchange, localExchange);
119                    return;
120                }
121            }
122        }
123    
124        protected void processFinally(Exchange exchange) throws Throwable {
125            if (finallyProcessor != null) {
126                Throwable lastException = exchange.getException();
127                exchange.setException(null);
128    
129                // do not catch any exception here, let it propagate up
130                finallyProcessor.process(exchange);
131                if (exchange.getException() == null) {
132                    exchange.setException(lastException);
133                }
134            }
135        }
136    }