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.processor.DelegateProcessor;
022 import org.apache.camel.spi.Interceptor;
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025 import org.springframework.transaction.TransactionStatus;
026 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
027 import org.springframework.transaction.support.TransactionTemplate;
028
029 /**
030 * Wraps the processor in a Spring transaction
031 *
032 * @version $Revision: 1.1 $
033 */
034 public class SpringTransactionInterceptor<E> implements Interceptor<E> {
035 private static final transient Log log = LogFactory.getLog(SpringTransactionInterceptor.class);
036
037 private TransactionTemplate template;
038
039 public SpringTransactionInterceptor() {
040 }
041
042 public SpringTransactionInterceptor(TransactionTemplate template) {
043 this.template = template;
044 }
045
046 public Processor<E> addIntercetors(Processor<E> processor) {
047 final TransactionTemplate transactionTemplate = getTemplate();
048 if (transactionTemplate == null) {
049 log.warn("No TransactionTemplate available so transactions will not be enabled!");
050 return processor;
051 }
052
053 return new DelegateProcessor<E>(processor) {
054
055 public void process(final E exchange) {
056 transactionTemplate.execute(new TransactionCallbackWithoutResult() {
057 protected void doInTransactionWithoutResult(TransactionStatus status) {
058 processNext(exchange);
059 }
060 });
061 }
062
063 @Override
064 public String toString() {
065 return "SpringTransaction[" + getNext() + "]";
066 }
067 };
068 }
069
070 public TransactionTemplate getTemplate() {
071 return template;
072 }
073
074 public void setTemplate(TransactionTemplate template) {
075 this.template = template;
076 }
077 }