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.processor;
018
019import java.util.Collection;
020import java.util.Iterator;
021import java.util.List;
022
023import org.apache.camel.AsyncCallback;
024import org.apache.camel.AsyncProcessor;
025import org.apache.camel.CamelContext;
026import org.apache.camel.Exchange;
027import org.apache.camel.Processor;
028import org.apache.camel.Traceable;
029import org.apache.camel.spi.IdAware;
030import org.apache.camel.util.AsyncProcessorConverterHelper;
031import org.apache.camel.util.AsyncProcessorHelper;
032import org.apache.camel.util.ExchangeHelper;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import static org.apache.camel.processor.PipelineHelper.continueProcessing;
037
038/**
039 * Creates a Pipeline pattern where the output of the previous step is sent as
040 * input to the next step, reusing the same message exchanges
041 *
042 * @version 
043 */
044public class Pipeline extends MulticastProcessor implements AsyncProcessor, Traceable, IdAware {
045    private static final Logger LOG = LoggerFactory.getLogger(Pipeline.class);
046
047    private String id;
048
049    public Pipeline(CamelContext camelContext, Collection<Processor> processors) {
050        super(camelContext, processors);
051    }
052
053    public static Processor newInstance(CamelContext camelContext, List<Processor> processors) {
054        if (processors.isEmpty()) {
055            return null;
056        } else if (processors.size() == 1) {
057            return processors.get(0);
058        }
059        return new Pipeline(camelContext, processors);
060    }
061
062    public void process(Exchange exchange) throws Exception {
063        AsyncProcessorHelper.process(this, exchange);
064    }
065
066    public boolean process(Exchange exchange, AsyncCallback callback) {
067        Iterator<Processor> processors = getProcessors().iterator();
068        Exchange nextExchange = exchange;
069        boolean first = true;
070
071        while (continueRouting(processors, nextExchange)) {
072            if (first) {
073                first = false;
074            } else {
075                // prepare for next run
076                nextExchange = createNextExchange(nextExchange);
077            }
078
079            // get the next processor
080            Processor processor = processors.next();
081
082            AsyncProcessor async = AsyncProcessorConverterHelper.convert(processor);
083            boolean sync = process(exchange, nextExchange, callback, processors, async);
084
085            // continue as long its being processed synchronously
086            if (!sync) {
087                LOG.trace("Processing exchangeId: {} is continued being processed asynchronously", exchange.getExchangeId());
088                // the remainder of the pipeline will be completed async
089                // so we break out now, then the callback will be invoked which then continue routing from where we left here
090                return false;
091            }
092
093            LOG.trace("Processing exchangeId: {} is continued being processed synchronously", exchange.getExchangeId());
094
095            // check for error if so we should break out
096            if (!continueProcessing(nextExchange, "so breaking out of pipeline", LOG)) {
097                break;
098            }
099        }
100
101        // logging nextExchange as it contains the exchange that might have altered the payload and since
102        // we are logging the completion if will be confusing if we log the original instead
103        // we could also consider logging the original and the nextExchange then we have *before* and *after* snapshots
104        LOG.trace("Processing complete for exchangeId: {} >>> {}", exchange.getExchangeId(), nextExchange);
105
106        // copy results back to the original exchange
107        ExchangeHelper.copyResults(exchange, nextExchange);
108
109        callback.done(true);
110        return true;
111    }
112
113    private boolean process(final Exchange original, final Exchange exchange, final AsyncCallback callback,
114                            final Iterator<Processor> processors, final AsyncProcessor asyncProcessor) {
115        // this does the actual processing so log at trace level
116        LOG.trace("Processing exchangeId: {} >>> {}", exchange.getExchangeId(), exchange);
117
118        // implement asynchronous routing logic in callback so we can have the callback being
119        // triggered and then continue routing where we left
120        boolean sync = asyncProcessor.process(exchange, new AsyncCallback() {
121            public void done(boolean doneSync) {
122                // we only have to handle async completion of the pipeline
123                if (doneSync) {
124                    return;
125                }
126
127                // continue processing the pipeline asynchronously
128                Exchange nextExchange = exchange;
129                while (continueRouting(processors, nextExchange)) {
130                    AsyncProcessor processor = AsyncProcessorConverterHelper.convert(processors.next());
131
132                    // check for error if so we should break out
133                    if (!continueProcessing(nextExchange, "so breaking out of pipeline", LOG)) {
134                        break;
135                    }
136
137                    nextExchange = createNextExchange(nextExchange);
138                    doneSync = process(original, nextExchange, callback, processors, processor);
139                    if (!doneSync) {
140                        LOG.trace("Processing exchangeId: {} is continued being processed asynchronously", exchange.getExchangeId());
141                        return;
142                    }
143                }
144
145                ExchangeHelper.copyResults(original, nextExchange);
146                LOG.trace("Processing complete for exchangeId: {} >>> {}", original.getExchangeId(), original);
147                callback.done(false);
148            }
149        });
150
151        return sync;
152    }
153
154    /**
155     * Strategy method to create the next exchange from the previous exchange.
156     * <p/>
157     * Remember to copy the original exchange id otherwise correlation of ids in the log is a problem
158     *
159     * @param previousExchange the previous exchange
160     * @return a new exchange
161     */
162    protected Exchange createNextExchange(Exchange previousExchange) {
163        return PipelineHelper.createNextExchange(previousExchange);
164    }
165
166    protected boolean continueRouting(Iterator<Processor> it, Exchange exchange) {
167        boolean answer = true;
168
169        Object stop = exchange.getProperty(Exchange.ROUTE_STOP);
170        if (stop != null) {
171            boolean doStop = exchange.getContext().getTypeConverter().convertTo(Boolean.class, stop);
172            if (doStop) {
173                LOG.debug("ExchangeId: {} is marked to stop routing: {}", exchange.getExchangeId(), exchange);
174                answer = false;
175            }
176        } else {
177            // continue if there are more processors to route
178            answer = it.hasNext();
179        }
180
181        LOG.trace("ExchangeId: {} should continue routing: {}", exchange.getExchangeId(), answer);
182        return answer;
183    }
184
185    @Override
186    public String toString() {
187        return "Pipeline[" + getProcessors() + "]";
188    }
189
190    @Override
191    public String getTraceLabel() {
192        return "pipeline";
193    }
194
195    public String getId() {
196        return id;
197    }
198
199    public void setId(String id) {
200        this.id = id;
201    }
202}