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.reifier.errorhandler;
018
019import org.apache.camel.Endpoint;
020import org.apache.camel.ErrorHandlerFactory;
021import org.apache.camel.NoSuchEndpointException;
022import org.apache.camel.Processor;
023import org.apache.camel.builder.DeadLetterChannelBuilder;
024import org.apache.camel.processor.errorhandler.DeadLetterChannel;
025import org.apache.camel.spi.RouteContext;
026import org.apache.camel.util.StringHelper;
027
028public class DeadLetterChannelReifier extends DefaultErrorHandlerReifier<DeadLetterChannelBuilder> {
029
030    public DeadLetterChannelReifier(RouteContext routeContext, ErrorHandlerFactory definition) {
031        super(routeContext, definition);
032    }
033
034    @Override
035    public Processor createErrorHandler(Processor processor) throws Exception {
036        validateDeadLetterUri();
037
038        DeadLetterChannel answer = new DeadLetterChannel(camelContext, processor, definition.getLogger(), definition.getOnRedelivery(),
039                                                         definition.getRedeliveryPolicy(), definition.getExceptionPolicyStrategy(), definition.getFailureProcessor(),
040                                                         definition.getDeadLetterUri(), definition.isDeadLetterHandleNewException(), definition.isUseOriginalMessage(),
041                                                         definition.isUseOriginalBody(), definition.getRetryWhilePolicy(camelContext),
042                                                         getExecutorService(camelContext), definition.getOnPrepareFailure(), definition.getOnExceptionOccurred());
043        // configure error handler before we can use it
044        configure(routeContext, answer);
045        return answer;
046    }
047
048    protected void validateDeadLetterUri() {
049        Endpoint deadLetter = definition.getDeadLetter();
050        String deadLetterUri = definition.getDeadLetterUri();
051        if (deadLetter == null) {
052            StringHelper.notEmpty(deadLetterUri, "deadLetterUri", this);
053            deadLetter = camelContext.getEndpoint(deadLetterUri);
054            if (deadLetter == null) {
055                throw new NoSuchEndpointException(deadLetterUri);
056            }
057            // TODO: ErrorHandler: no modification to the model should be done
058            definition.setDeadLetter(deadLetter);
059        }
060    }
061
062}