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.exceptionpolicy;
018    
019    import java.util.Map;
020    import java.util.Set;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.model.ExceptionType;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    /**
028     * The default strategy used in Camel to resolve the {@link org.apache.camel.model.ExceptionType} that should
029     * handle the thrown exception.
030     * <p/>
031     * This strategy applies the following rules:
032     * <ul>
033     *   <li>The exception type must be configured with an Exception that is an instance of the thrown exception</li>
034     *   <li>If the exception type has exactly the thrown exception then its selected</li>
035     *   <li>Otherwise the type that has an exception that is super of the thrown exception is selected
036     *       (recurring up the exception hierarchy)
037     *  </ul>
038     */
039    public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
040    
041        private static final transient Log LOG = LogFactory.getLog(DefaultExceptionPolicyStrategy.class);
042    
043        public ExceptionType getExceptionPolicy(Map<Class, ExceptionType> exceptionPolicices, Exchange exchange,
044                                                Throwable exception) {
045            // the goal is to find the exception with the same/closet inheritance level as the target exception being thrown
046            int targetLevel = getInheritanceLevel(exception.getClass());
047            // candidate is the best candidate found so far to return
048            ExceptionType candidate = null;
049            // difference in inheritance level between the current candidate and the thrown exception (target level)
050            int candidateDiff = Integer.MAX_VALUE;
051    
052            // loop through all the entries and find the best candidates to use
053            Set<Map.Entry<Class, ExceptionType>> entries = exceptionPolicices.entrySet();
054            for (Map.Entry<Class, ExceptionType> entry : entries) {
055                Class clazz = entry.getKey();
056                ExceptionType type = entry.getValue();
057    
058                // must be instance of check to ensure that the clazz is one type of the thrown exception
059                if (clazz.isInstance(exception)) {
060    
061                    // exact match
062                    if (clazz.equals(exception.getClass())) {
063                        candidate = type;
064                        break;
065                    }
066    
067                    // not an exact match so find the best candidate
068                    int level = getInheritanceLevel(clazz);
069                    int diff = targetLevel - level;
070    
071                    if (diff < candidateDiff) {
072                        // replace with a much better candidate
073                        candidate = type;
074                        candidateDiff = diff;
075                    }
076                }
077            }
078    
079            if (LOG.isDebugEnabled()) {
080                LOG.debug("Using " + candidate + " as the exception policy");
081            }
082            return candidate;
083    
084        }
085    
086        private static int getInheritanceLevel(Class clazz) {
087            if (clazz == null || "java.lang.Object".equals(clazz.getName())) {
088                return 0;
089            }
090            return 1 + getInheritanceLevel(clazz.getSuperclass());
091        }
092    
093    }