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.impl.ServiceSupport; 026 import org.apache.camel.model.ExceptionType; 027 import org.apache.camel.processor.exceptionpolicy.DefaultExceptionPolicyStrategy; 028 import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy; 029 030 /** 031 * Support class for {@link ErrorHandler} implementations. 032 * 033 * @version $Revision: 37863 $ 034 */ 035 public abstract class ErrorHandlerSupport extends ServiceSupport implements ErrorHandler { 036 private Map<Class, ExceptionType> exceptionPolicies = new LinkedHashMap<Class, ExceptionType>(); 037 private ExceptionPolicyStrategy exceptionPolicy = createDefaultExceptionPolicyStrategy(); 038 039 public void addExceptionPolicy(ExceptionType exception) { 040 Processor processor = exception.getErrorHandler(); 041 addChildService(processor); 042 043 List<Class> list = exception.getExceptionClasses(); 044 045 for (Class exceptionType : list) { 046 exceptionPolicies.put(exceptionType, exception); 047 } 048 } 049 050 /** 051 * Attempts to invoke the handler for this particular exception if one is available 052 */ 053 protected boolean customProcessorForException(Exchange exchange, Throwable exception) throws Exception { 054 ExceptionType policy = getExceptionPolicy(exchange, exception); 055 if (policy != null) { 056 Processor processor = policy.getErrorHandler(); 057 if (processor != null) { 058 processor.process(exchange); 059 return true; 060 } 061 } 062 return false; 063 } 064 065 /** 066 * Attempts to find the best suited {@link ExceptionType} to be used for handling the given thrown exception. 067 * 068 * @param exchange the exchange 069 * @param exception the exception that was thrown 070 * @return the best exception type to handle this exception, <tt>null</tt> if none found. 071 */ 072 protected ExceptionType getExceptionPolicy(Exchange exchange, Throwable exception) { 073 if (exceptionPolicy == null) { 074 throw new IllegalStateException("The exception policy has not been set"); 075 } 076 077 return exceptionPolicy.getExceptionPolicy(exceptionPolicies, exchange, exception); 078 } 079 080 /** 081 * Sets the strategy to use for resolving the {@link ExceptionType} to use 082 * for handling thrown exceptions. 083 */ 084 public void setExceptionPolicy(ExceptionPolicyStrategy exceptionPolicy) { 085 this.exceptionPolicy = exceptionPolicy; 086 } 087 088 /** 089 * Creates the default exception policy strategy to use. 090 */ 091 public static ExceptionPolicyStrategy createDefaultExceptionPolicyStrategy() { 092 return new DefaultExceptionPolicyStrategy(); 093 } 094 095 }