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.builder; 018 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.model.OnExceptionDefinition; 026import org.apache.camel.processor.ErrorHandler; 027import org.apache.camel.processor.ErrorHandlerSupport; 028import org.apache.camel.processor.RedeliveryErrorHandler; 029import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy; 030import org.apache.camel.spi.RouteContext; 031import org.apache.camel.util.ObjectHelper; 032 033/** 034 * Base class for builders of error handling. 035 * 036 * @version 037 */ 038public abstract class ErrorHandlerBuilderSupport implements ErrorHandlerBuilder { 039 private Map<RouteContext, List<OnExceptionDefinition>> onExceptions = new HashMap<RouteContext, List<OnExceptionDefinition>>(); 040 private ExceptionPolicyStrategy exceptionPolicyStrategy; 041 042 public void addErrorHandlers(RouteContext routeContext, OnExceptionDefinition exception) { 043 // only add if we not already have it 044 List<OnExceptionDefinition> list = onExceptions.get(routeContext); 045 if (list == null) { 046 list = new ArrayList<OnExceptionDefinition>(); 047 onExceptions.put(routeContext, list); 048 } 049 if (!list.contains(exception)) { 050 list.add(exception); 051 } 052 } 053 054 protected void cloneBuilder(ErrorHandlerBuilderSupport other) { 055 if (!onExceptions.isEmpty()) { 056 Map<RouteContext, List<OnExceptionDefinition>> copy = new HashMap<RouteContext, List<OnExceptionDefinition>>(onExceptions); 057 other.onExceptions = copy; 058 } 059 other.exceptionPolicyStrategy = exceptionPolicyStrategy; 060 } 061 062 public void configure(RouteContext routeContext, ErrorHandler handler) { 063 if (handler instanceof ErrorHandlerSupport) { 064 ErrorHandlerSupport handlerSupport = (ErrorHandlerSupport) handler; 065 066 List<OnExceptionDefinition> list = onExceptions.get(routeContext); 067 if (list != null) { 068 for (OnExceptionDefinition exception : list) { 069 handlerSupport.addExceptionPolicy(routeContext, exception); 070 } 071 } 072 } 073 if (handler instanceof RedeliveryErrorHandler) { 074 boolean original = ((RedeliveryErrorHandler) handler).isUseOriginalMessagePolicy(); 075 if (original) { 076 // ensure allow original is turned on 077 routeContext.setAllowUseOriginalMessage(true); 078 } 079 } 080 } 081 082 public List<OnExceptionDefinition> getErrorHandlers(RouteContext routeContext) { 083 return onExceptions.get(routeContext); 084 } 085 086 public void setErrorHandlers(RouteContext routeContext, List<OnExceptionDefinition> exceptions) { 087 this.onExceptions.put(routeContext, exceptions); 088 } 089 090 /** 091 * Sets the exception policy to use 092 */ 093 public ErrorHandlerBuilderSupport exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) { 094 setExceptionPolicyStrategy(exceptionPolicyStrategy); 095 return this; 096 } 097 098 public ExceptionPolicyStrategy getExceptionPolicyStrategy() { 099 return exceptionPolicyStrategy; 100 } 101 102 public void setExceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) { 103 ObjectHelper.notNull(exceptionPolicyStrategy, "ExceptionPolicyStrategy"); 104 this.exceptionPolicyStrategy = exceptionPolicyStrategy; 105 } 106 107 /** 108 * Remove the OnExceptionList by look up the route id from the ErrorHandlerBuilder internal map 109 * @param id the route id 110 * @return true if the route context is found and removed 111 */ 112 public boolean removeOnExceptionList(String id) { 113 for (RouteContext routeContext : onExceptions.keySet()) { 114 if (getRouteId(routeContext).equals(id)) { 115 onExceptions.remove(routeContext); 116 return true; 117 } 118 } 119 return false; 120 } 121 122 protected String getRouteId(RouteContext routeContext) { 123 CamelContext context = routeContext.getCamelContext(); 124 if (context != null) { 125 return routeContext.getRoute().idOrCreate(context.getNodeIdFactory()); 126 } else { 127 return routeContext.getRoute().getId(); 128 } 129 } 130}