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.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.impl.DefaultUnitOfWork;
022    import org.apache.camel.spi.RouteContext;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    
026    import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
027    
028    /** 
029     * Handles calling the UnitOfWork.done() method when processing of an exchange
030     * is complete.
031     */
032    public final class UnitOfWorkProcessor extends DelegateProcessor {
033    
034        private static final transient Log LOG = LogFactory.getLog(UnitOfWorkProcessor.class);
035        private final RouteContext routeContext;
036    
037        public UnitOfWorkProcessor(Processor processor) {
038            this(null, processor);
039        }
040    
041        public UnitOfWorkProcessor(RouteContext routeContext, Processor processor) {
042            super(processor);
043            this.routeContext = routeContext;
044        }
045    
046        @Override
047        public String toString() {
048            return "UnitOfWork(" + processor + ")";
049        }
050    
051        @Override
052        protected void processNext(Exchange exchange) throws Exception {
053            if (exchange.getUnitOfWork() == null) {
054                // If there is no existing UoW, then we should start one and
055                // terminate it once processing is completed for the exchange.
056                final DefaultUnitOfWork uow = new DefaultUnitOfWork(exchange);
057                exchange.setUnitOfWork(uow);
058                try {
059                    uow.start();
060                } catch (Exception e) {
061                    throw wrapRuntimeCamelException(e);
062                }
063    
064                // process the exchange
065                try {
066                    processor.process(exchange);
067                } catch (Exception e) {
068                    exchange.setException(e);
069                } finally {
070                    // must always done unit of work
071                    done(uow, exchange);
072                }
073            } else {
074                // There was an existing UoW, so we should just pass through..
075                // so that the guy the initiated the UoW can terminate it.
076                processor.process(exchange);
077            }
078        }
079    
080        private void done(DefaultUnitOfWork uow, Exchange exchange) {
081            // unit of work is done
082            exchange.getUnitOfWork().done(exchange);
083            try {
084                uow.stop();
085            } catch (Exception e) {
086                LOG.warn("Exception occurred during stopping UnitOfWork for Exchange: " + exchange
087                    + ". This exception will be ignored.");
088            }
089            exchange.setUnitOfWork(null);
090        }
091    
092    }