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