Coverage Report - org.apache.camel.processor.ErrorHandlerSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
ErrorHandlerSupport
73% 
75% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.processor;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Processor;
 22  
 import org.apache.camel.impl.ServiceSupport;
 23  
 import org.apache.camel.model.ExceptionType;
 24  
 
 25  
 import java.util.IdentityHashMap;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 import java.util.Set;
 29  
 
 30  
 /**
 31  
  * @version $Revision: 1.1 $
 32  
  */
 33  294
 public abstract class ErrorHandlerSupport extends ServiceSupport implements ErrorHandler {
 34  294
     private Map<Class, ExceptionType> exceptionPolicices = new IdentityHashMap<Class, ExceptionType>();
 35  
 
 36  
     public void addExceptionPolicy(ExceptionType exception) {
 37  24
         Processor processor = exception.getErrorHandler();
 38  24
         addChildService(processor);
 39  
 
 40  24
         List<Class> list = exception.getExceptionClasses();
 41  
 
 42  24
         for (Class exceptionType : list) {
 43  24
             exceptionPolicices.put(exceptionType, exception);
 44  24
         }
 45  24
     }
 46  
 
 47  
     /**
 48  
      * Attempts to invoke the handler for this particular exception if one is available
 49  
      *
 50  
      * @param exchange
 51  
      * @param exception
 52  
      * @return
 53  
      */
 54  
     protected boolean customProcessorForException(Exchange exchange, Throwable exception) throws Exception {
 55  0
         ExceptionType policy = getExceptionPolicy(exchange, exception);
 56  0
         Processor processor = policy.getErrorHandler();
 57  0
         if (processor != null) {
 58  0
             processor.process(exchange);
 59  0
             return true;
 60  
         }
 61  0
         return false;
 62  
     }
 63  
 
 64  
     protected ExceptionType getExceptionPolicy(Exchange exchange, Throwable exception) {
 65  21
         Set<Map.Entry<Class, ExceptionType>> entries = exceptionPolicices.entrySet();
 66  21
         for (Map.Entry<Class, ExceptionType> entry : entries) {
 67  16
             Class type = entry.getKey();
 68  16
             if (type.isInstance(exception)) {
 69  12
                 return entry.getValue();
 70  
             }
 71  4
         }
 72  9
         return null;
 73  
     }
 74  
 }