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 org.apache.camel.ErrorHandlerFactory; 020import org.apache.camel.Processor; 021import org.apache.camel.reifier.errorhandler.ErrorHandlerReifier; 022import org.apache.camel.spi.RouteContext; 023import org.apache.camel.util.ObjectHelper; 024 025/** 026 * Represents a proxy to an error handler builder which is resolved by named 027 * reference 028 */ 029public class ErrorHandlerBuilderRef extends ErrorHandlerBuilderSupport { 030 private final String ref; 031 private boolean supportTransacted; 032 033 public ErrorHandlerBuilderRef(String ref) { 034 this.ref = ref; 035 } 036 037 @Override 038 public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception { 039 ErrorHandlerFactory handler = lookupErrorHandler(routeContext); 040 return ErrorHandlerReifier.reifier(routeContext, handler).createErrorHandler(processor); 041 } 042 043 @Override 044 public ErrorHandlerFactory getOrLookupErrorHandlerFactory(RouteContext routeContext) { 045 return lookupErrorHandler(routeContext); 046 } 047 048 @Override 049 public boolean supportTransacted() { 050 return supportTransacted; 051 } 052 053 @Override 054 public ErrorHandlerBuilder cloneBuilder() { 055 ErrorHandlerBuilderRef answer = new ErrorHandlerBuilderRef(ref); 056 cloneBuilder(answer); 057 return answer; 058 } 059 060 protected void cloneBuilder(ErrorHandlerBuilderRef other) { 061 super.cloneBuilder(other); 062 063 // no need to copy the handlers 064 065 other.supportTransacted = supportTransacted; 066 } 067 068 public String getRef() { 069 return ref; 070 } 071 072 private ErrorHandlerBuilder lookupErrorHandler(RouteContext routeContext) { 073 ErrorHandlerBuilder handler = (ErrorHandlerBuilder)ErrorHandlerReifier.lookupErrorHandlerFactory(routeContext, getRef()); 074 ObjectHelper.notNull(handler, "error handler '" + ref + "'"); 075 076 // configure if the handler support transacted 077 supportTransacted = handler.supportTransacted(); 078 079 routeContext.addErrorHandlerFactoryReference(this, handler); 080 081 return handler; 082 } 083 084 @Override 085 public String toString() { 086 return "ErrorHandlerBuilderRef[" + ref + "]"; 087 } 088}