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