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.IdentityHashMap;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Processor;
026    import org.apache.camel.impl.ServiceSupport;
027    import org.apache.camel.model.ExceptionType;
028    
029    /**
030     * @version $Revision: 642753 $
031     */
032    public abstract class ErrorHandlerSupport extends ServiceSupport implements ErrorHandler {
033        private Map<Class, ExceptionType> exceptionPolicices = new IdentityHashMap<Class, ExceptionType>();
034    
035        public void addExceptionPolicy(ExceptionType exception) {
036            Processor processor = exception.getErrorHandler();
037            addChildService(processor);
038    
039            List<Class> list = exception.getExceptionClasses();
040    
041            for (Class exceptionType : list) {
042                exceptionPolicices.put(exceptionType, exception);
043            }
044        }
045    
046        /**
047         * Attempts to invoke the handler for this particular exception if one is available
048         */
049        protected boolean customProcessorForException(Exchange exchange, Throwable exception) throws Exception {
050            ExceptionType policy = getExceptionPolicy(exchange, exception);
051            Processor processor = policy.getErrorHandler();
052            if (processor != null) {
053                processor.process(exchange);
054                return true;
055            }
056            return false;
057        }
058    
059        protected ExceptionType getExceptionPolicy(Exchange exchange, Throwable exception) {
060            Set<Map.Entry<Class, ExceptionType>> entries = exceptionPolicices.entrySet();
061            for (Map.Entry<Class, ExceptionType> entry : entries) {
062                Class type = entry.getKey();
063                if (type.isInstance(exception)) {
064                    return entry.getValue();
065                }
066            }
067            return null;
068        }
069    }