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 org.apache.camel.AsyncCallback;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Traceable;
023    import org.apache.camel.util.ExchangeHelper;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    /**
028     * Processor to handle do finally supporting asynchronous routing engine
029     *
030     * @version
031     */
032    public class FinallyProcessor extends DelegateAsyncProcessor implements Traceable {
033    
034        private static final Logger LOG = LoggerFactory.getLogger(FinallyProcessor.class);
035    
036        public FinallyProcessor(Processor processor) {
037            super(processor);
038        }
039    
040        @Override
041        public boolean process(final Exchange exchange, final AsyncCallback callback) {
042            // clear exception so finally block can be executed
043            final Exception e = exchange.getException();
044            exchange.setException(null);
045            // but store the caught exception as a property
046            if (e != null) {
047                exchange.setProperty(Exchange.EXCEPTION_CAUGHT, e);
048            }
049            // store the last to endpoint as the failure endpoint
050            if (exchange.getProperty(Exchange.FAILURE_ENDPOINT) == null) {
051                exchange.setProperty(Exchange.FAILURE_ENDPOINT, exchange.getProperty(Exchange.TO_ENDPOINT));
052            }
053    
054            boolean sync = processor.process(exchange, new AsyncCallback() {
055                public void done(boolean doneSync) {
056                    if (e == null) {
057                        exchange.removeProperty(Exchange.FAILURE_ENDPOINT);
058                    } else {
059                        // set exception back on exchange
060                        exchange.setException(e);
061                        exchange.setProperty(Exchange.EXCEPTION_CAUGHT, e);
062                    }
063    
064                    if (!doneSync) {
065                        // signal callback to continue routing async
066                        ExchangeHelper.prepareOutToIn(exchange);
067                        LOG.trace("Processing complete for exchangeId: {} >>> {}", exchange.getExchangeId(), exchange);
068                    }
069                    callback.done(doneSync);
070                }
071            });
072            return sync;
073        }
074    
075        @Override
076        public String toString() {
077            return "Finally{" + getProcessor() + "}";
078        }
079    
080        public String getTraceLabel() {
081            return "finally";
082        }
083    }