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    
027    /**
028     * Base class for builders of error handling.
029     *
030     * @version $Revision: 765920 $
031     */
032    public abstract class ErrorHandlerBuilderSupport implements ErrorHandlerBuilder {
033        private List<OnExceptionDefinition> exceptions = new ArrayList<OnExceptionDefinition>();
034        private ExceptionPolicyStrategy exceptionPolicyStrategy = ErrorHandlerSupport.createDefaultExceptionPolicyStrategy();
035    
036        public void addErrorHandlers(OnExceptionDefinition exception) {
037            // only add if we not already have it
038            if (!exceptions.contains(exception)) {
039                exceptions.add(exception);
040            }
041        }
042    
043        public void configure(ErrorHandler handler) {
044            if (handler instanceof ErrorHandlerSupport) {
045                ErrorHandlerSupport handlerSupport = (ErrorHandlerSupport) handler;
046    
047                for (OnExceptionDefinition exception : exceptions) {
048                    handlerSupport.addExceptionPolicy(exception);
049                }
050            }
051        }
052    
053        public List<OnExceptionDefinition> getErrorHandlers() {
054            return exceptions;
055        }
056    
057        public void setErrorHandlers(List<OnExceptionDefinition> exceptions) {
058            this.exceptions.clear();
059            this.exceptions.addAll(exceptions);
060        }
061    
062        /**
063         * Sets the exception policy to use
064         */
065        public ErrorHandlerBuilderSupport exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
066            setExceptionPolicyStrategy(exceptionPolicyStrategy);
067            return this;
068        }
069    
070        public ExceptionPolicyStrategy getExceptionPolicyStrategy() {
071            return exceptionPolicyStrategy;
072        }
073    
074        public void setExceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) {
075            this.exceptionPolicyStrategy = exceptionPolicyStrategy;
076        }
077    }