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 org.apache.camel.Endpoint;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Expression;
022    import org.apache.camel.LoggingLevel;
023    import org.apache.camel.Predicate;
024    import org.apache.camel.Processor;
025    import org.apache.camel.processor.DeadLetterChannel;
026    import org.apache.camel.processor.Logger;
027    import org.apache.camel.processor.RecipientList;
028    import org.apache.camel.processor.RedeliveryPolicy;
029    import org.apache.camel.processor.SendProcessor;
030    import org.apache.camel.spi.RouteContext;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * A builder of a <a
035     * href="http://camel.apache.org/dead-letter-channel.html">Dead Letter
036     * Channel</a>
037     *
038     * @version $Revision: 792966 $
039     */
040    public class DeadLetterChannelBuilder extends DefaultErrorHandlerBuilder {
041    
042        public DeadLetterChannelBuilder() {
043            // no-arg constructor used by Spring DSL
044        }
045    
046        public DeadLetterChannelBuilder(Endpoint deadLetter) {
047            setDeadLetter(deadLetter);
048        }
049    
050        public DeadLetterChannelBuilder(String uri) {
051            setDeadLetterUri(uri);
052        }
053    
054        public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception {
055            DeadLetterChannel answer = new DeadLetterChannel(processor, getLogger(), getOnRedelivery(), getRedeliveryPolicy(),
056                    getHandledPolicy(), getExceptionPolicyStrategy(), getFailureProcessor(), getDeadLetterUri(),
057                    isUseOriginalMessage());
058            // configure error handler before we can use it
059            configure(answer);
060            return answer;
061        }
062    
063        public boolean supportTransacted() {
064            return false;
065        }
066    
067        // Properties
068        // -------------------------------------------------------------------------
069    
070        public Processor getFailureProcessor() {
071            if (failureProcessor == null) {
072                if (deadLetter != null) {
073                    failureProcessor = new SendProcessor(deadLetter);
074                } else {
075                    // use a recipient list since we only have an uri for the endpoint
076                    failureProcessor = new RecipientList(new Expression() {
077                        public Object evaluate(Exchange exchange) {
078                            return deadLetterUri;
079                        }
080    
081                        public <T> T evaluate(Exchange exchange, Class<T> type) {
082                            return exchange.getContext().getTypeConverter().convertTo(type, deadLetterUri);
083                        }
084                    });
085                }
086            }
087            return failureProcessor;
088        }
089    
090        protected Predicate createHandledPolicy() {
091            // should be handled by default for dead letter channel
092            return PredicateBuilder.toPredicate(ExpressionBuilder.constantExpression(true));
093        }
094    
095        @Override
096        protected RedeliveryPolicy createRedeliveryPolicy() {
097            return new RedeliveryPolicy();
098        }
099    
100        protected Logger createLogger() {
101            return new Logger(LogFactory.getLog(DeadLetterChannel.class), LoggingLevel.ERROR);
102        }
103    
104        @Override
105        public String toString() {
106            return "DeadLetterChannelBuilder(" + deadLetterUri + ")";
107        }
108    }