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.builder; 018 019 import java.util.ArrayList; 020 import java.util.HashMap; 021 import java.util.List; 022 import java.util.Map; 023 024 import org.apache.camel.model.OnExceptionDefinition; 025 import org.apache.camel.processor.ErrorHandler; 026 import org.apache.camel.processor.ErrorHandlerSupport; 027 import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy; 028 import org.apache.camel.spi.RouteContext; 029 import org.apache.camel.util.ObjectHelper; 030 031 /** 032 * Base class for builders of error handling. 033 * 034 * @version 035 */ 036 public abstract class ErrorHandlerBuilderSupport implements ErrorHandlerBuilder { 037 private Map<RouteContext, List<OnExceptionDefinition>> onExceptions = new HashMap<RouteContext, List<OnExceptionDefinition>>(); 038 private ExceptionPolicyStrategy exceptionPolicyStrategy; 039 040 public void addErrorHandlers(RouteContext routeContext, OnExceptionDefinition exception) { 041 // only add if we not already have it 042 List<OnExceptionDefinition> list = onExceptions.get(routeContext); 043 if (list == null) { 044 list = new ArrayList<OnExceptionDefinition>(); 045 onExceptions.put(routeContext, list); 046 } 047 if (!list.contains(exception)) { 048 list.add(exception); 049 } 050 } 051 052 protected void cloneBuilder(ErrorHandlerBuilderSupport other) { 053 if (!onExceptions.isEmpty()) { 054 Map<RouteContext, List<OnExceptionDefinition>> copy = new HashMap<RouteContext, List<OnExceptionDefinition>>(onExceptions); 055 other.onExceptions = copy; 056 } 057 other.exceptionPolicyStrategy = exceptionPolicyStrategy; 058 } 059 060 public void configure(RouteContext routeContext, ErrorHandler handler) { 061 if (handler instanceof ErrorHandlerSupport) { 062 ErrorHandlerSupport handlerSupport = (ErrorHandlerSupport) handler; 063 064 List<OnExceptionDefinition> list = onExceptions.get(routeContext); 065 if (list != null) { 066 for (OnExceptionDefinition exception : list) { 067 handlerSupport.addExceptionPolicy(routeContext, exception); 068 } 069 } 070 } 071 } 072 073 public List<OnExceptionDefinition> getErrorHandlers(RouteContext routeContext) { 074 return onExceptions.get(routeContext); 075 } 076 077 public void setErrorHandlers(RouteContext routeContext, List<OnExceptionDefinition> exceptions) { 078 this.onExceptions.put(routeContext, exceptions); 079 } 080 081 /** 082 * Sets the exception policy to use 083 */ 084 public ErrorHandlerBuilderSupport exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) { 085 setExceptionPolicyStrategy(exceptionPolicyStrategy); 086 return this; 087 } 088 089 public ExceptionPolicyStrategy getExceptionPolicyStrategy() { 090 return exceptionPolicyStrategy; 091 } 092 093 public void setExceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) { 094 ObjectHelper.notNull(exceptionPolicyStrategy, "ExceptionPolicyStrategy"); 095 this.exceptionPolicyStrategy = exceptionPolicyStrategy; 096 } 097 }