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.Processor;
020 import org.apache.camel.spi.Policy;
021 import org.apache.commons.logging.Log;
022 import org.apache.commons.logging.LogFactory;
023 import org.springframework.transaction.PlatformTransactionManager;
024 import org.springframework.transaction.support.TransactionTemplate;
025
026 /**
027 * Wraps the processor in a Spring transaction
028 *
029 * @version $Revision: 674383 $
030 */
031 public class SpringTransactionPolicy<E> implements Policy<E> {
032 private static final transient Log LOG = LogFactory.getLog(SpringTransactionPolicy.class);
033 private TransactionTemplate template;
034 private String propagationBehaviorName;
035 private PlatformTransactionManager transactionManager;
036
037 /**
038 * Default constructor for easy spring configuration.
039 */
040 public SpringTransactionPolicy() {
041 }
042
043 public SpringTransactionPolicy(TransactionTemplate template) {
044 this.template = template;
045 }
046
047 public Processor wrap(Processor processor) {
048 final TransactionTemplate transactionTemplate = getTemplate();
049 if (transactionTemplate == null) {
050 LOG.warn("No TransactionTemplate available so transactions will not be enabled!");
051 return processor;
052 }
053
054 TransactionInterceptor answer = new TransactionInterceptor(transactionTemplate);
055 answer.setProcessor(processor);
056 return answer;
057 }
058
059 public TransactionTemplate getTemplate() {
060 if (template == null) {
061 template = new TransactionTemplate(transactionManager);
062 if (propagationBehaviorName != null) {
063 template.setPropagationBehaviorName(propagationBehaviorName);
064 }
065 }
066 return template;
067 }
068
069 public void setTemplate(TransactionTemplate template) {
070 this.template = template;
071 }
072
073 public void setTransactionManager(PlatformTransactionManager transactionManager) {
074 this.transactionManager = transactionManager;
075 }
076
077 public PlatformTransactionManager getTransactionManager() {
078 return transactionManager;
079 }
080
081 public void setPropagationBehaviorName(String propagationBehaviorName) {
082 this.propagationBehaviorName = propagationBehaviorName;
083 }
084
085 public String getPropagationBehaviorName() {
086 return propagationBehaviorName;
087 }
088
089 }