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(handler).createErrorHandler(routeContext, processor);
041    }
042
043    @Override
044    public boolean supportTransacted() {
045        return supportTransacted;
046    }
047
048    @Override
049    public ErrorHandlerBuilder cloneBuilder() {
050        ErrorHandlerBuilderRef answer = new ErrorHandlerBuilderRef(ref);
051        cloneBuilder(answer);
052        return answer;
053    }
054
055    protected void cloneBuilder(ErrorHandlerBuilderRef other) {
056        super.cloneBuilder(other);
057
058        // no need to copy the handlers
059
060        other.supportTransacted = supportTransacted;
061    }
062
063    public String getRef() {
064        return ref;
065    }
066
067    private ErrorHandlerBuilder lookupErrorHandler(RouteContext routeContext) {
068        ErrorHandlerBuilder handler = (ErrorHandlerBuilder)ErrorHandlerReifier.lookupErrorHandlerFactory(routeContext, getRef());
069        ObjectHelper.notNull(handler, "error handler '" + ref + "'");
070
071        // configure if the handler support transacted
072        supportTransacted = handler.supportTransacted();
073
074        routeContext.addErrorHandlerFactoryReference(this, handler);
075
076        return handler;
077    }
078
079    @Override
080    public String toString() {
081        return "ErrorHandlerBuilderRef[" + ref + "]";
082    }
083}