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: 36865 $
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    
040        private final List<CatchProcessor> catchClauses;
041    
042        private final Processor finallyProcessor;
043    
044        public TryProcessor(Processor tryProcessor, List<CatchProcessor> catchClauses, Processor finallyProcessor) {
045            this.tryProcessor = tryProcessor;
046            this.catchClauses = catchClauses;
047            this.finallyProcessor = finallyProcessor;
048        }
049    
050        public String toString() {
051            String finallyText = (finallyProcessor == null) ? "" : " Finally {" + finallyProcessor + "}";
052            return "Try {" + tryProcessor + "} " + catchClauses + finallyText;
053        }
054    
055        public void process(Exchange exchange) throws Exception {
056            Throwable e = null;
057            try {
058                tryProcessor.process(exchange);
059                e = exchange.getException();
060    
061                // Ignore it if it was handled by the dead letter channel.
062                if (e != null && DeadLetterChannel.isFailureHandled(exchange)) {
063                    e = null;
064                }
065            } catch (Throwable ex) {
066                e = ex;
067                exchange.setException(e);
068            }
069    
070            Exception unexpected = null;
071            try {
072                if (e != null) {
073                    LOG.info("Caught exception while processing exchange.", e);
074                    handleException(exchange, e);
075                }
076            } catch (Exception ex) {
077                unexpected = ex;
078            } catch (Throwable ex) {
079                unexpected = new RuntimeCamelException(ex);
080            } finally {
081                try {
082                    processFinally(exchange);
083                } catch (Exception ex) {
084                    unexpected = ex;
085                } catch (Throwable ex) {
086                    unexpected = new RuntimeCamelException(ex);
087                }
088                if (unexpected != null) {
089                    LOG.warn("Caught exception inside processFinally clause.", unexpected);
090                    throw unexpected;
091                }
092            }
093    
094            if (unexpected != null) {
095                LOG.warn("Caught exception inside handle clause.", unexpected);
096                throw unexpected;
097            }
098        }
099    
100        protected void doStart() throws Exception {
101            ServiceHelper.startServices(tryProcessor, catchClauses, finallyProcessor);
102        }
103    
104        protected void doStop() throws Exception {
105            ServiceHelper.stopServices(tryProcessor, catchClauses, finallyProcessor);
106        }
107    
108        protected void handleException(Exchange exchange, Throwable e) throws Throwable {
109            for (CatchProcessor catchClause : catchClauses) {
110                if (catchClause.catches(e)) {
111                    // lets attach the exception to the exchange
112                    Exchange localExchange = exchange.copy();
113                    localExchange.getIn().setHeader("caught.exception", e);
114                    // give the rest of the pipeline another chance
115                    localExchange.setException(null);
116    
117                    // do not catch any exception here, let it propagate up
118                    catchClause.process(localExchange);
119                    localExchange.getIn().removeHeader("caught.exception");
120                    ExchangeHelper.copyResults(exchange, localExchange);
121                    return;
122                }
123            }
124        }
125    
126        protected void processFinally(Exchange exchange) throws Throwable {
127            if (finallyProcessor != null) {
128                Throwable lastException = exchange.getException();
129                exchange.setException(null);
130    
131                // do not catch any exception here, let it propagate up
132                finallyProcessor.process(exchange);
133                if (exchange.getException() == null) {
134                    exchange.setException(lastException);
135                }
136            }
137        }
138    }