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.spring.spi;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.RuntimeCamelException;
021    import org.apache.camel.processor.DelegateProcessor;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    import org.springframework.transaction.TransactionDefinition;
026    import org.springframework.transaction.TransactionStatus;
027    import org.springframework.transaction.support.TransactionCallbackWithoutResult;
028    import org.springframework.transaction.support.TransactionTemplate;
029    
030    /**
031     * @version $Revision: 630591 $
032     */
033    public class TransactionInterceptor extends DelegateProcessor {
034        private static final transient Log LOG = LogFactory.getLog(TransactionInterceptor.class);
035        private final TransactionTemplate transactionTemplate;
036    
037        public TransactionInterceptor(TransactionTemplate transactionTemplate) {
038            this.transactionTemplate = transactionTemplate;
039        }
040    
041        public void process(final Exchange exchange) {
042            LOG.info("transaction begin");
043    
044            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
045                protected void doInTransactionWithoutResult(TransactionStatus status) {
046                    try {
047                        processNext(exchange);
048                    } catch (Exception e) {
049                        throw new RuntimeCamelException(e);
050                    }
051                }
052            });
053    
054            LOG.info("transaction commit");
055        }
056    
057        @Override
058        public String toString() {
059            return "TransactionInterceptor:" + propagationBehaviorToString(transactionTemplate.getPropagationBehavior()) + "[" + getProcessor() + "]";
060        }
061    
062        private String propagationBehaviorToString(int propagationBehavior) {
063            String rc;
064            switch (propagationBehavior) {
065            case TransactionDefinition.PROPAGATION_MANDATORY:
066                rc = "PROPAGATION_MANDATORY";
067                break;
068            case TransactionDefinition.PROPAGATION_NESTED:
069                rc = "PROPAGATION_NESTED";
070                break;
071            case TransactionDefinition.PROPAGATION_NEVER:
072                rc = "PROPAGATION_NEVER";
073                break;
074            case TransactionDefinition.PROPAGATION_NOT_SUPPORTED:
075                rc = "PROPAGATION_NOT_SUPPORTED";
076                break;
077            case TransactionDefinition.PROPAGATION_REQUIRED:
078                rc = "PROPAGATION_REQUIRED";
079                break;
080            case TransactionDefinition.PROPAGATION_REQUIRES_NEW:
081                rc = "PROPAGATION_REQUIRES_NEW";
082                break;
083            case TransactionDefinition.PROPAGATION_SUPPORTS:
084                rc = "PROPAGATION_SUPPORTS";
085                break;
086            default:
087                rc = "UNKOWN"; 
088            }
089            return rc;
090        }
091    }