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.builder.ErrorHandlerBuilder;
021 import org.apache.camel.builder.ErrorHandlerBuilderRef;
022 import org.apache.camel.spi.RouteContext;
023 import org.apache.camel.spi.TransactedPolicy;
024 import org.apache.camel.util.ObjectHelper;
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import org.springframework.transaction.PlatformTransactionManager;
028 import org.springframework.transaction.support.TransactionTemplate;
029
030 /**
031 * Wraps the processor in a Spring transaction
032 *
033 * @version $Revision: 829746 $
034 */
035 public class SpringTransactionPolicy implements TransactedPolicy {
036 private static final transient Log LOG = LogFactory.getLog(SpringTransactionPolicy.class);
037 private TransactionTemplate template;
038 private String propagationBehaviorName;
039 private PlatformTransactionManager transactionManager;
040
041 /**
042 * Default constructor for easy spring configuration.
043 */
044 public SpringTransactionPolicy() {
045 }
046
047 public SpringTransactionPolicy(TransactionTemplate template) {
048 this.template = template;
049 }
050
051 public SpringTransactionPolicy(PlatformTransactionManager transactionManager) {
052 this.transactionManager = transactionManager;
053 }
054
055 public Processor wrap(RouteContext routeContext, Processor processor) {
056 TransactionErrorHandler answer;
057
058 // the goal is to configure the error handler builder on the route as a transacted error handler,
059 // either its already a transacted or if not we replace it with a transacted one that we configure here
060 // and wrap the processor in the transacted error handler as we can have transacted routes that change
061 // propagation behavior, eg: from A required -> B -> requiresNew C (advanced use-case)
062 // if we should not support this we do not need to wrap the processor as we only need one transacted error handler
063
064 // find the existing error handler builder
065 ErrorHandlerBuilder builder = routeContext.getRoute().getErrorHandlerBuilder();
066
067 // check if its a ref if so then do a lookup
068 if (builder instanceof ErrorHandlerBuilderRef) {
069 // its a reference to a error handler so lookup the reference
070 ErrorHandlerBuilderRef builderRef = (ErrorHandlerBuilderRef) builder;
071 String ref = builderRef.getRef();
072 // only lookup if there was explicit an error handler builder configured
073 // otherwise its just the "default" that has not explicit been configured
074 // and if so then we can safely replace that with our transacted error handler
075 if (ErrorHandlerBuilderRef.isErrorHandlerBuilderConfigured(ref)) {
076 if (LOG.isDebugEnabled()) {
077 LOG.debug("Looking up ErrorHandlerBuilder with ref: " + ref);
078 }
079 builder = ErrorHandlerBuilderRef.lookupErrorHandlerBuilder(routeContext, ref);
080 }
081 }
082
083 if (builder != null && builder.supportTransacted()) {
084 // already a TX error handler then we are good to go
085 if (LOG.isDebugEnabled()) {
086 LOG.debug("The ErrorHandlerBuilder configured is already a TransactionErrorHandlerBuilder: " + builder);
087 }
088 answer = createTransactionErrorHandler(routeContext, processor, builder);
089 answer.setExceptionPolicy(builder.getExceptionPolicyStrategy());
090 // configure our answer based on the existing error handler
091 builder.configure(answer);
092 } else {
093 // no transaction error handler builder configure so create a temporary one as we got all
094 // the needed information form the configured builder anyway this allow us to use transacted
095 // routes anyway even though the error handler is not transactional, eg ease of configuration
096 if (LOG.isDebugEnabled()) {
097 if (builder != null) {
098 LOG.debug("The ErrorHandlerBuilder configured is not a TransactionErrorHandlerBuilder: " + builder);
099 } else {
100 LOG.debug("No ErrorHandlerBuilder configured, will use default TransactionErrorHandlerBuilder settings");
101 }
102 }
103 TransactionErrorHandlerBuilder txBuilder = new TransactionErrorHandlerBuilder();
104 txBuilder.setTransactionTemplate(getTransactionTemplate());
105 txBuilder.setSpringTransactionPolicy(this);
106 if (builder != null) {
107 // use error handlers from the configured builder
108 txBuilder.setErrorHandlers(builder.getErrorHandlers());
109 }
110 answer = createTransactionErrorHandler(routeContext, processor, txBuilder);
111 answer.setExceptionPolicy(txBuilder.getExceptionPolicyStrategy());
112 // configure our answer based on the existing error handler
113 txBuilder.configure(answer);
114
115 // set the route to use our transacted error handler builder
116 routeContext.getRoute().setErrorHandlerBuilder(txBuilder);
117 }
118
119 // return with wrapped transacted error handler
120 return answer;
121 }
122
123 protected TransactionErrorHandler createTransactionErrorHandler(RouteContext routeContext, Processor processor, ErrorHandlerBuilder builder) {
124 TransactionErrorHandler answer;
125 try {
126 answer = (TransactionErrorHandler) builder.createErrorHandler(routeContext, processor);
127 } catch (Exception e) {
128 throw ObjectHelper.wrapRuntimeCamelException(e);
129 }
130 return answer;
131 }
132
133 public TransactionTemplate getTransactionTemplate() {
134 if (template == null) {
135 ObjectHelper.notNull(transactionManager, "transactionManager");
136 template = new TransactionTemplate(transactionManager);
137 if (propagationBehaviorName != null) {
138 template.setPropagationBehaviorName(propagationBehaviorName);
139 }
140 }
141 return template;
142 }
143
144 public void setTransactionTemplate(TransactionTemplate template) {
145 this.template = template;
146 }
147
148 public void setTransactionManager(PlatformTransactionManager transactionManager) {
149 this.transactionManager = transactionManager;
150 }
151
152 public PlatformTransactionManager getTransactionManager() {
153 return transactionManager;
154 }
155
156 public void setPropagationBehaviorName(String propagationBehaviorName) {
157 this.propagationBehaviorName = propagationBehaviorName;
158 }
159
160 public String getPropagationBehaviorName() {
161 return propagationBehaviorName;
162 }
163 }