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.processor;
018    
019    import java.util.LinkedHashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Processor;
025    import org.apache.camel.model.OnExceptionDefinition;
026    import org.apache.camel.model.ProcessorDefinitionHelper;
027    import org.apache.camel.model.RouteDefinition;
028    import org.apache.camel.processor.exceptionpolicy.DefaultExceptionPolicyStrategy;
029    import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyKey;
030    import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
031    import org.apache.camel.spi.RouteContext;
032    import org.apache.camel.support.ChildServiceSupport;
033    import org.slf4j.Logger;
034    import org.slf4j.LoggerFactory;
035    
036    /**
037     * Support class for {@link ErrorHandler} implementations.
038     *
039     * @version 
040     */
041    public abstract class ErrorHandlerSupport extends ChildServiceSupport implements ErrorHandler {
042    
043        protected final Logger log = LoggerFactory.getLogger(getClass());
044    
045        protected final Map<ExceptionPolicyKey, OnExceptionDefinition> exceptionPolicies = new LinkedHashMap<ExceptionPolicyKey, OnExceptionDefinition>();
046        protected ExceptionPolicyStrategy exceptionPolicy = createDefaultExceptionPolicyStrategy();
047    
048        public void addExceptionPolicy(RouteContext routeContext, OnExceptionDefinition exceptionType) {
049            if (routeContext != null) {
050                // add error handler as child service so they get lifecycle handled
051                Processor errorHandler = exceptionType.getErrorHandler(routeContext.getRoute().getId());
052                if (errorHandler != null) {
053                    addChildService(errorHandler);
054                }
055            }
056    
057            List<Class<? extends Throwable>> list = exceptionType.getExceptionClasses();
058            for (Class<? extends Throwable> clazz : list) {
059                String routeId = null;
060                // only get the route id, if the exception type is route scoped
061                if (exceptionType.isRouteScoped()) {
062                    RouteDefinition route = ProcessorDefinitionHelper.getRoute(exceptionType);
063                    if (route != null) {
064                        routeId = route.getId();
065                    }
066                }
067                ExceptionPolicyKey key = new ExceptionPolicyKey(routeId, clazz, exceptionType.getOnWhen());
068                exceptionPolicies.put(key, exceptionType);
069            }
070        }
071    
072        /**
073         * Attempts to find the best suited {@link OnExceptionDefinition} to be used for handling the given thrown exception.
074         *
075         * @param exchange  the exchange
076         * @param exception the exception that was thrown
077         * @return the best exception type to handle this exception, <tt>null</tt> if none found.
078         */
079        protected OnExceptionDefinition getExceptionPolicy(Exchange exchange, Throwable exception) {
080            if (exceptionPolicy == null) {
081                throw new IllegalStateException("The exception policy has not been set");
082            }
083    
084            return exceptionPolicy.getExceptionPolicy(exceptionPolicies, exchange, exception);
085        }
086    
087        /**
088         * Sets the strategy to use for resolving the {@link OnExceptionDefinition} to use
089         * for handling thrown exceptions.
090         */
091        public void setExceptionPolicy(ExceptionPolicyStrategy exceptionPolicy) {
092            if (exceptionPolicy != null) {
093                this.exceptionPolicy = exceptionPolicy;
094            }
095        }
096    
097        /**
098         * Creates the default exception policy strategy to use.
099         */
100        public static ExceptionPolicyStrategy createDefaultExceptionPolicyStrategy() {
101            return new DefaultExceptionPolicyStrategy();
102        }
103    
104        /**
105         * Whether this error handler supports transacted exchanges or not.
106         */
107        public abstract boolean supportTransacted();
108    
109        /**
110         * Whether this error handler handles exhausted errors by moving the exchange to a dead letter channel.
111         */
112        public boolean isDeadLetterChannel() {
113            return false;
114        }
115    
116        /**
117         * Gets the output
118         */
119        public abstract Processor getOutput();
120    
121    }