Coverage Report - org.apache.camel.spring.spi.SpringTransactionPolicy
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringTransactionPolicy
0% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.spring.spi;
 19  
 
 20  
 import org.apache.camel.Processor;
 21  
 import org.apache.camel.RuntimeCamelException;
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.processor.DelegateProcessor;
 24  
 import org.apache.camel.spi.Policy;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 import org.springframework.transaction.TransactionDefinition;
 28  
 import org.springframework.transaction.TransactionStatus;
 29  
 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
 30  
 import org.springframework.transaction.support.TransactionTemplate;
 31  
 
 32  
 /**
 33  
  * Wraps the processor in a Spring transaction
 34  
  *
 35  
  * @version $Revision: 1.1 $
 36  
  */
 37  
 public class SpringTransactionPolicy<E> implements Policy<E> {
 38  0
     private static final transient Log log = LogFactory.getLog(SpringTransactionPolicy.class);
 39  
 
 40  
     private TransactionTemplate template;
 41  
 
 42  0
     public SpringTransactionPolicy() {
 43  0
     }
 44  
 
 45  0
     public SpringTransactionPolicy(TransactionTemplate template) {
 46  0
         this.template = template;
 47  0
     }
 48  
 
 49  
     public Processor wrap(Processor processor) {
 50  0
         final TransactionTemplate transactionTemplate = getTemplate();
 51  0
         if (transactionTemplate == null) {
 52  0
             log.warn("No TransactionTemplate available so transactions will not be enabled!");
 53  0
             return processor;
 54  
         }
 55  
 
 56  0
         return new DelegateProcessor(processor) {
 57  
 
 58  
             public void process(final Exchange exchange) {
 59  0
                 transactionTemplate.execute(new TransactionCallbackWithoutResult() {
 60  0
                     protected void doInTransactionWithoutResult(TransactionStatus status) {
 61  
                         try {
 62  0
                                                         processNext(exchange);
 63  0
                                                 } catch (Exception e) {
 64  0
                                                         throw new RuntimeCamelException(e);
 65  0
                                                 }
 66  0
                     }
 67  
                 });
 68  0
             }
 69  
 
 70  
             @Override
 71  
             public String toString() {
 72  0
                 return "SpringTransactionPolicy:"+propagationBehaviorToString(transactionTemplate.getPropagationBehavior())+"[" + getNext() + "]";
 73  
             }
 74  
 
 75  0
                         private String propagationBehaviorToString(int propagationBehavior) {
 76  0
                                 switch( propagationBehavior ) {
 77  
                                 case TransactionDefinition.PROPAGATION_MANDATORY:
 78  0
                                         return "PROPAGATION_MANDATORY";
 79  
                                 case TransactionDefinition.PROPAGATION_NESTED:
 80  0
                                         return "PROPAGATION_NESTED";
 81  
                                 case TransactionDefinition.PROPAGATION_NEVER:
 82  0
                                         return "PROPAGATION_NEVER";
 83  
                                 case TransactionDefinition.PROPAGATION_NOT_SUPPORTED:
 84  0
                                         return "PROPAGATION_NOT_SUPPORTED";
 85  
                                 case TransactionDefinition.PROPAGATION_REQUIRED:
 86  0
                                         return "PROPAGATION_REQUIRED";
 87  
                                 case TransactionDefinition.PROPAGATION_REQUIRES_NEW:
 88  0
                                         return "PROPAGATION_REQUIRES_NEW";
 89  
                                 case TransactionDefinition.PROPAGATION_SUPPORTS:
 90  0
                                         return "PROPAGATION_SUPPORTS";
 91  
                                 }
 92  0
                                 return "UNKOWN";
 93  
                         }
 94  
         };
 95  
     }
 96  
 
 97  
     public TransactionTemplate getTemplate() {
 98  0
         return template;
 99  
     }
 100  
 
 101  
     public void setTemplate(TransactionTemplate template) {
 102  0
         this.template = template;
 103  0
     }
 104  
 }