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 org.apache.camel.LoggingLevel;
020import org.apache.camel.builder.DefaultErrorHandlerBuilder;
021import org.apache.camel.reifier.errorhandler.ErrorHandlerReifier;
022import org.apache.camel.spi.CamelLogger;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.springframework.transaction.PlatformTransactionManager;
026import org.springframework.transaction.support.TransactionTemplate;
027
028/**
029 * A transactional error handler that supports leveraging Spring TransactionManager.
030 */
031public class TransactionErrorHandlerBuilder extends DefaultErrorHandlerBuilder {
032
033    static {
034        ErrorHandlerReifier.registerReifier(TransactionErrorHandlerBuilder.class, TransactionErrorHandlerReifier::new);
035    }
036
037    private static final Logger LOG = LoggerFactory.getLogger(TransactionErrorHandlerBuilder.class);
038    private static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
039    private TransactionTemplate transactionTemplate;
040    private LoggingLevel rollbackLoggingLevel = LoggingLevel.WARN;
041
042    public TransactionErrorHandlerBuilder() {
043        // no-arg constructor used by Spring DSL
044    }
045
046    public TransactionTemplate getTransactionTemplate() {
047        return transactionTemplate;
048    }
049
050    @Override
051    public boolean supportTransacted() {
052        return true;
053    }
054
055    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
056        this.transactionTemplate = transactionTemplate;
057    }
058
059    public void setSpringTransactionPolicy(SpringTransactionPolicy policy) {
060        this.transactionTemplate = policy.getTransactionTemplate();
061    }
062
063    public void setTransactionManager(PlatformTransactionManager transactionManager) {
064        this.transactionTemplate = new TransactionTemplate(transactionManager);
065    }
066
067    public LoggingLevel getRollbackLoggingLevel() {
068        return rollbackLoggingLevel;
069    }
070
071    /**
072     * Sets the logging level to use for logging transactional rollback.
073     * <p/>
074     * This option is default WARN.
075     *
076     * @param rollbackLoggingLevel the logging level
077     */
078    public void setRollbackLoggingLevel(LoggingLevel rollbackLoggingLevel) {
079        this.rollbackLoggingLevel = rollbackLoggingLevel;
080    }
081
082    // Builder methods
083    // -------------------------------------------------------------------------
084
085    /**
086     * Sets the logging level to use for logging transactional rollback.
087     * <p/>
088     * This option is default WARN.
089     *
090     * @param rollbackLoggingLevel the logging level
091     */
092    public TransactionErrorHandlerBuilder rollbackLoggingLevel(LoggingLevel rollbackLoggingLevel) {
093        setRollbackLoggingLevel(rollbackLoggingLevel);
094        return this;
095    }
096
097    // Implementation
098    // -------------------------------------------------------------------------
099
100    @Override
101    protected CamelLogger createLogger() {
102        return new CamelLogger(LoggerFactory.getLogger(TransactionErrorHandler.class), LoggingLevel.ERROR);
103    }
104
105    @Override
106    public String toString() {
107        return "TransactionErrorHandlerBuilder";
108    }
109
110}