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.ErrorHandlerBuilderSupport;
021    import org.apache.camel.processor.ErrorHandlerSupport;
022    import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
023    import org.apache.camel.spi.RouteContext;
024    import org.apache.camel.util.ObjectHelper;
025    import org.springframework.beans.factory.InitializingBean;
026    import org.springframework.transaction.support.TransactionTemplate;
027    
028    /**
029     * An error handler which will roll the exception back if there is an error
030     * rather than using the dead letter channel and retry logic.
031     *
032     * A delay is also used after a rollback
033     *
034     * @version $Revision: 765920 $
035     */
036    public class TransactionErrorHandlerBuilder extends ErrorHandlerBuilderSupport implements InitializingBean {
037    
038        private TransactionTemplate transactionTemplate;
039        private ExceptionPolicyStrategy exceptionPolicyStrategy = ErrorHandlerSupport.createDefaultExceptionPolicyStrategy();
040    
041        public TransactionErrorHandlerBuilder() {
042        }
043    
044        public TransactionTemplate getTransactionTemplate() {
045            return transactionTemplate;
046        }
047    
048        public boolean supportTransacted() {
049            return true;
050        }
051    
052        public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception {
053            TransactionErrorHandler answer = new TransactionErrorHandler(transactionTemplate, processor, exceptionPolicyStrategy);
054            configure(answer);
055            return answer;
056        }
057    
058        public void afterPropertiesSet() throws Exception {
059            ObjectHelper.notNull(transactionTemplate, "transactionTemplate");
060        }
061    
062        public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
063            this.transactionTemplate = transactionTemplate;
064        }
065    
066        public void setSpringTransactionPolicy(SpringTransactionPolicy policy) {
067            this.transactionTemplate = policy.getTransactionTemplate();
068        }
069    
070        /**
071         * Sets the exception policy strategy to use for resolving the {@link org.apache.camel.model.OnExceptionDefinition}
072         * to use for a given thrown exception
073         */
074        public ExceptionPolicyStrategy getExceptionPolicyStrategy() {
075            return exceptionPolicyStrategy;
076        }
077    
078        public void setExceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
079            this.exceptionPolicyStrategy = exceptionPolicyStrategy;
080        }
081    
082        // Builder methods
083        // -------------------------------------------------------------------------
084    
085        /**
086         * Sets the exception policy to use
087         */
088        public TransactionErrorHandlerBuilder exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
089            setExceptionPolicyStrategy(exceptionPolicyStrategy);
090            return this;
091        }
092    
093    }