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