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.ErrorHandlerBuilder;
021    import org.apache.camel.builder.ErrorHandlerBuilderSupport;
022    import org.apache.camel.processor.DelayPolicy;
023    import org.apache.camel.processor.RedeliveryPolicy;
024    import org.apache.camel.spi.RouteContext;
025    import org.apache.camel.util.ObjectHelper;
026    import org.springframework.beans.factory.InitializingBean;
027    import org.springframework.transaction.support.TransactionTemplate;
028    
029    /**
030     * An error handler which will roll the exception back if there is an error
031     * rather than using the dead letter channel and retry logic.
032     *
033     * A delay is also used after a rollback
034     *
035     * @version $Revision: 713123 $
036     */
037    public class TransactionErrorHandlerBuilder extends ErrorHandlerBuilderSupport implements Cloneable, InitializingBean {
038    
039        private TransactionTemplate transactionTemplate;
040        private DelayPolicy delayPolicy = new DelayPolicy();
041    
042        public TransactionErrorHandlerBuilder() {
043        }
044    
045        public TransactionTemplate getTransactionTemplate() {
046            return transactionTemplate;
047        }
048    
049        public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
050            this.transactionTemplate = transactionTemplate;
051        }
052    
053        public DelayPolicy getDelayPolicy() {
054            return delayPolicy;
055        }
056    
057        public void setDelayPolicy(DelayPolicy delayPolicy) {
058            this.delayPolicy = delayPolicy;
059        }
060    
061        public ErrorHandlerBuilder copy() {
062            try {
063                return (ErrorHandlerBuilder) clone();
064            } catch (CloneNotSupportedException e) {
065                throw new Error("Clone should be supported: " + e, e);
066            }
067        }
068    
069        public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception {
070            return new TransactionInterceptor(processor, transactionTemplate, delayPolicy);
071        }
072    
073        public void afterPropertiesSet() throws Exception {
074            ObjectHelper.notNull(transactionTemplate, "transactionTemplate");
075        }
076    
077        // Builder methods
078        // -------------------------------------------------------------------------
079    
080        public TransactionErrorHandlerBuilder delay(long delay) {
081            getDelayPolicy().delay(delay);
082            return this;
083        }
084    
085    }