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 */ 017package org.apache.camel.spring.spi; 018 019import java.util.Map; 020 021import org.apache.camel.CamelContext; 022import org.apache.camel.ErrorHandlerFactory; 023import org.apache.camel.Processor; 024import org.apache.camel.reifier.errorhandler.DefaultErrorHandlerReifier; 025import org.apache.camel.spi.RouteContext; 026import org.apache.camel.spi.TransactedPolicy; 027import org.apache.camel.util.ObjectHelper; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030import org.springframework.transaction.PlatformTransactionManager; 031import org.springframework.transaction.support.TransactionTemplate; 032 033import static org.apache.camel.model.TransactedDefinition.PROPAGATION_REQUIRED; 034 035public class TransactionErrorHandlerReifier extends DefaultErrorHandlerReifier<TransactionErrorHandlerBuilder> { 036 037 private static final Logger LOG = LoggerFactory.getLogger(TransactionErrorHandlerReifier.class); 038 039 public TransactionErrorHandlerReifier(RouteContext routeContext, ErrorHandlerFactory definition) { 040 super(routeContext, definition); 041 } 042 043 @Override 044 public Processor createErrorHandler(Processor processor) throws Exception { 045 TransactionTemplate transactionTemplate = definition.getTransactionTemplate(); 046 if (transactionTemplate == null) { 047 // lookup in context if no transaction template has been configured 048 LOG.debug("No TransactionTemplate configured on TransactionErrorHandlerBuilder. Will try find it in the registry."); 049 050 Map<String, TransactedPolicy> mapPolicy = routeContext.lookupByType(TransactedPolicy.class); 051 if (mapPolicy != null && mapPolicy.size() == 1) { 052 TransactedPolicy policy = mapPolicy.values().iterator().next(); 053 if (policy instanceof SpringTransactionPolicy) { 054 transactionTemplate = ((SpringTransactionPolicy) policy).getTransactionTemplate(); 055 } 056 } 057 058 if (transactionTemplate == null) { 059 TransactedPolicy policy = routeContext.lookup(PROPAGATION_REQUIRED, TransactedPolicy.class); 060 if (policy instanceof SpringTransactionPolicy) { 061 transactionTemplate = ((SpringTransactionPolicy) policy).getTransactionTemplate(); 062 } 063 } 064 065 if (transactionTemplate == null) { 066 Map<String, TransactionTemplate> mapTemplate = routeContext.lookupByType(TransactionTemplate.class); 067 if (mapTemplate == null || mapTemplate.isEmpty()) { 068 LOG.trace("No TransactionTemplate found in registry."); 069 } else if (mapTemplate.size() == 1) { 070 transactionTemplate = mapTemplate.values().iterator().next(); 071 } else { 072 LOG.debug("Found {} TransactionTemplate in registry. Cannot determine which one to use. " 073 + "Please configure a TransactionTemplate on the TransactionErrorHandlerBuilder", mapTemplate.size()); 074 } 075 } 076 077 if (transactionTemplate == null) { 078 Map<String, PlatformTransactionManager> mapManager = routeContext.lookupByType(PlatformTransactionManager.class); 079 if (mapManager == null || mapManager.isEmpty()) { 080 LOG.trace("No PlatformTransactionManager found in registry."); 081 } else if (mapManager.size() == 1) { 082 transactionTemplate = new TransactionTemplate(mapManager.values().iterator().next()); 083 } else { 084 LOG.debug("Found {} PlatformTransactionManager in registry. Cannot determine which one to use for TransactionTemplate. " 085 + "Please configure a TransactionTemplate on the TransactionErrorHandlerBuilder", mapManager.size()); 086 } 087 } 088 089 if (transactionTemplate != null) { 090 LOG.debug("Found TransactionTemplate in registry to use: {}", transactionTemplate); 091 } 092 } 093 094 ObjectHelper.notNull(transactionTemplate, "transactionTemplate", this); 095 096 TransactionErrorHandler answer = new TransactionErrorHandler(routeContext.getCamelContext(), processor, 097 definition.getLogger(), definition.getOnRedelivery(), 098 definition.getRedeliveryPolicy(), definition.getExceptionPolicyStrategy(), transactionTemplate, 099 definition.getRetryWhilePolicy(routeContext.getCamelContext()), 100 getExecutorService(routeContext.getCamelContext()), 101 definition.getRollbackLoggingLevel(), definition.getOnExceptionOccurred()); 102 // configure error handler before we can use it 103 configure(routeContext, answer); 104 return answer; 105 } 106 107}