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.Route;
024import org.apache.camel.builder.DeadLetterChannelBuilder;
025import org.apache.camel.processor.errorhandler.DeadLetterChannel;
026import org.apache.camel.util.StringHelper;
027
028public class DeadLetterChannelReifier extends DefaultErrorHandlerReifier<DeadLetterChannelBuilder> {
029
030    public DeadLetterChannelReifier(Route route, ErrorHandlerFactory definition) {
031        super(route, 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(),
039                                                         getBean(Processor.class, definition.getOnRedelivery(), definition.getOnRedeliveryRef()),
040                                                         definition.getRedeliveryPolicy(), definition.getExceptionPolicyStrategy(),
041                                                         getBean(Processor.class, definition.getFailureProcessor(), definition.getFailureProcessorRef()),
042                                                         definition.getDeadLetterUri(), definition.isDeadLetterHandleNewException(), definition.isUseOriginalMessage(),
043                                                         definition.isUseOriginalBody(),
044                                                         definition.getRetryWhilePolicy(camelContext),
045                                                         getExecutorService(definition.getExecutorService(), definition.getExecutorServiceRef()),
046                                                         getBean(Processor.class, definition.getOnPrepareFailure(), definition.getOnPrepareFailureRef()),
047                                                         getBean(Processor.class, definition.getOnExceptionOccurred(), definition.getOnExceptionOccurredRef()));
048        // configure error handler before we can use it
049        configure(answer);
050        return answer;
051    }
052
053    protected void validateDeadLetterUri() {
054        Endpoint deadLetter = definition.getDeadLetter();
055        String deadLetterUri = definition.getDeadLetterUri();
056        if (deadLetter == null) {
057            StringHelper.notEmpty(deadLetterUri, "deadLetterUri", this);
058            deadLetter = camelContext.getEndpoint(deadLetterUri);
059            if (deadLetter == null) {
060                throw new NoSuchEndpointException(deadLetterUri);
061            }
062            // TODO: ErrorHandler: no modification to the model should be done
063            definition.setDeadLetter(deadLetter);
064        }
065    }
066
067}