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.reifier.errorhandler.DefaultErrorHandlerReifier;
024import org.apache.camel.spi.RouteContext;
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(ErrorHandlerFactory definition) {
039        super(definition);
040    }
041
042    @Override
043    public Processor createErrorHandler(RouteContext routeContext, 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 = routeContext.lookupByType(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 = routeContext.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 = routeContext.lookupByType(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", mapTemplate.size());
073                }
074            }
075
076            if (transactionTemplate == null) {
077                Map<String, PlatformTransactionManager> mapManager = routeContext.lookupByType(PlatformTransactionManager.class);
078                if (mapManager == null || mapManager.isEmpty()) {
079                    LOG.trace("No PlatformTransactionManager found in registry.");
080                } else if (mapManager.size() == 1) {
081                    transactionTemplate = new TransactionTemplate(mapManager.values().iterator().next());
082                } else {
083                    LOG.debug("Found {} PlatformTransactionManager in registry. Cannot determine which one to use for TransactionTemplate. "
084                            + "Please configure a TransactionTemplate on the TransactionErrorHandlerBuilder", mapManager.size());
085                }
086            }
087
088            if (transactionTemplate != null) {
089                LOG.debug("Found TransactionTemplate in registry to use: {}", transactionTemplate);
090            }
091        }
092
093        ObjectHelper.notNull(transactionTemplate, "transactionTemplate", this);
094
095        TransactionErrorHandler answer = new TransactionErrorHandler(routeContext.getCamelContext(), processor,
096                definition.getLogger(), definition.getOnRedelivery(),
097                definition.getRedeliveryPolicy(), definition.getExceptionPolicyStrategy(), transactionTemplate,
098                definition.getRetryWhilePolicy(routeContext.getCamelContext()),
099                getExecutorService(routeContext.getCamelContext()),
100                definition.getRollbackLoggingLevel(), definition.getOnExceptionOccurred());
101        // configure error handler before we can use it
102        configure(routeContext, answer);
103        return answer;
104    }
105
106}