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 */ 017package org.apache.camel.processor; 018 019import java.util.LinkedHashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.camel.Exchange; 024import org.apache.camel.Processor; 025import org.apache.camel.model.OnExceptionDefinition; 026import org.apache.camel.model.ProcessorDefinitionHelper; 027import org.apache.camel.model.RouteDefinition; 028import org.apache.camel.processor.exceptionpolicy.DefaultExceptionPolicyStrategy; 029import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyKey; 030import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy; 031import org.apache.camel.spi.RouteContext; 032import org.apache.camel.support.ChildServiceSupport; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036/** 037 * Support class for {@link ErrorHandler} implementations. 038 * 039 * @version 040 */ 041public 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<>(); 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}