001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.spring.spi;
019    
020    import org.apache.camel.Processor;
021    import org.apache.camel.RuntimeCamelException;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.processor.DelegateProcessor;
024    import org.apache.camel.spi.Policy;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.springframework.transaction.TransactionDefinition;
028    import org.springframework.transaction.TransactionStatus;
029    import org.springframework.transaction.support.TransactionCallbackWithoutResult;
030    import org.springframework.transaction.support.TransactionTemplate;
031    
032    /**
033     * Wraps the processor in a Spring transaction
034     *
035     * @version $Revision: 1.1 $
036     */
037    public class SpringTransactionPolicy<E> implements Policy<E> {
038        private static final transient Log log = LogFactory.getLog(SpringTransactionPolicy.class);
039    
040        private TransactionTemplate template;
041    
042        public SpringTransactionPolicy() {
043        }
044    
045        public SpringTransactionPolicy(TransactionTemplate template) {
046            this.template = template;
047        }
048    
049        public Processor wrap(Processor processor) {
050            final TransactionTemplate transactionTemplate = getTemplate();
051            if (transactionTemplate == null) {
052                log.warn("No TransactionTemplate available so transactions will not be enabled!");
053                return processor;
054            }
055    
056            return new DelegateProcessor(processor) {
057    
058                public void process(final Exchange exchange) {
059                    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
060                        protected void doInTransactionWithoutResult(TransactionStatus status) {
061                            try {
062                                                            processNext(exchange);
063                                                    } catch (Exception e) {
064                                                            throw new RuntimeCamelException(e);
065                                                    }
066                        }
067                    });
068                }
069    
070                @Override
071                public String toString() {
072                    return "SpringTransactionPolicy:"+propagationBehaviorToString(transactionTemplate.getPropagationBehavior())+"[" + getNext() + "]";
073                }
074    
075                            private String propagationBehaviorToString(int propagationBehavior) {
076                                    switch( propagationBehavior ) {
077                                    case TransactionDefinition.PROPAGATION_MANDATORY:
078                                            return "PROPAGATION_MANDATORY";
079                                    case TransactionDefinition.PROPAGATION_NESTED:
080                                            return "PROPAGATION_NESTED";
081                                    case TransactionDefinition.PROPAGATION_NEVER:
082                                            return "PROPAGATION_NEVER";
083                                    case TransactionDefinition.PROPAGATION_NOT_SUPPORTED:
084                                            return "PROPAGATION_NOT_SUPPORTED";
085                                    case TransactionDefinition.PROPAGATION_REQUIRED:
086                                            return "PROPAGATION_REQUIRED";
087                                    case TransactionDefinition.PROPAGATION_REQUIRES_NEW:
088                                            return "PROPAGATION_REQUIRES_NEW";
089                                    case TransactionDefinition.PROPAGATION_SUPPORTS:
090                                            return "PROPAGATION_SUPPORTS";
091                                    }
092                                    return "UNKOWN";
093                            }
094            };
095        }
096    
097        public TransactionTemplate getTemplate() {
098            return template;
099        }
100    
101        public void setTemplate(TransactionTemplate template) {
102            this.template = template;
103        }
104    }