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.builder;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.camel.model.OnExceptionDefinition;
023    import org.apache.camel.processor.ErrorHandler;
024    import org.apache.camel.processor.ErrorHandlerSupport;
025    import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
026    import org.apache.camel.spi.RouteContext;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * Base class for builders of error handling.
031     *
032     * @version 
033     */
034    public abstract class ErrorHandlerBuilderSupport implements ErrorHandlerBuilder {
035        private List<OnExceptionDefinition> exceptions = new ArrayList<OnExceptionDefinition>();
036        private ExceptionPolicyStrategy exceptionPolicyStrategy;
037    
038        public void addErrorHandlers(OnExceptionDefinition exception) {
039            // only add if we not already have it
040            if (!exceptions.contains(exception)) {
041                exceptions.add(exception);
042            }
043        }
044    
045        public void configure(RouteContext routeContext, ErrorHandler handler) {
046            if (handler instanceof ErrorHandlerSupport) {
047                ErrorHandlerSupport handlerSupport = (ErrorHandlerSupport) handler;
048    
049                for (OnExceptionDefinition exception : exceptions) {
050                    handlerSupport.addExceptionPolicy(routeContext, exception);
051                }
052            }
053        }
054    
055        public List<OnExceptionDefinition> getErrorHandlers() {
056            return exceptions;
057        }
058    
059        public void setErrorHandlers(List<OnExceptionDefinition> exceptions) {
060            this.exceptions.clear();
061            this.exceptions.addAll(exceptions);
062        }
063    
064        /**
065         * Sets the exception policy to use
066         */
067        public ErrorHandlerBuilderSupport exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
068            setExceptionPolicyStrategy(exceptionPolicyStrategy);
069            return this;
070        }
071    
072        public ExceptionPolicyStrategy getExceptionPolicyStrategy() {
073            return exceptionPolicyStrategy;
074        }
075    
076        public void setExceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
077            ObjectHelper.notNull(exceptionPolicyStrategy, "ExceptionPolicyStrategy");
078            this.exceptionPolicyStrategy = exceptionPolicyStrategy;
079        }
080    }