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.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022import java.util.concurrent.Callable;
023import java.util.concurrent.ExecutorService;
024import java.util.concurrent.atomic.LongAdder;
025
026import org.apache.camel.AsyncCallback;
027import org.apache.camel.AsyncProcessor;
028import org.apache.camel.CamelContext;
029import org.apache.camel.CamelContextAware;
030import org.apache.camel.Exchange;
031import org.apache.camel.ExchangePattern;
032import org.apache.camel.Expression;
033import org.apache.camel.Message;
034import org.apache.camel.Processor;
035import org.apache.camel.ShutdownRunningTask;
036import org.apache.camel.StreamCache;
037import org.apache.camel.Traceable;
038import org.apache.camel.impl.DefaultExchange;
039import org.apache.camel.spi.EndpointUtilizationStatistics;
040import org.apache.camel.spi.IdAware;
041import org.apache.camel.spi.ShutdownAware;
042import org.apache.camel.support.ServiceSupport;
043import org.apache.camel.util.AsyncProcessorHelper;
044import org.apache.camel.util.ExchangeHelper;
045import org.apache.camel.util.ObjectHelper;
046import org.apache.camel.util.ServiceHelper;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050/**
051 * Processor for wire tapping exchanges to an endpoint destination.
052 *
053 * @version 
054 */
055public class WireTapProcessor extends ServiceSupport implements AsyncProcessor, Traceable, ShutdownAware, IdAware, CamelContextAware {
056    private static final Logger LOG = LoggerFactory.getLogger(WireTapProcessor.class);
057    private String id;
058    private CamelContext camelContext;
059    private final SendDynamicProcessor dynamicProcessor;
060    private final String uri;
061    private final boolean dynamicUri;
062    private final Processor processor;
063    private final ExchangePattern exchangePattern;
064    private final ExecutorService executorService;
065    private volatile boolean shutdownExecutorService;
066    private final LongAdder taskCount = new LongAdder();
067
068    // expression or processor used for populating a new exchange to send
069    // as opposed to traditional wiretap that sends a copy of the original exchange
070    private Expression newExchangeExpression;
071    private List<Processor> newExchangeProcessors;
072    private boolean copy;
073    private Processor onPrepare;
074
075    public WireTapProcessor(SendDynamicProcessor dynamicProcessor, Processor processor, ExchangePattern exchangePattern,
076                            ExecutorService executorService, boolean shutdownExecutorService, boolean dynamicUri) {
077        this.dynamicProcessor = dynamicProcessor;
078        this.uri = dynamicProcessor.getUri();
079        this.processor = processor;
080        this.exchangePattern = exchangePattern;
081        ObjectHelper.notNull(executorService, "executorService");
082        this.executorService = executorService;
083        this.shutdownExecutorService = shutdownExecutorService;
084        this.dynamicUri = dynamicUri;
085    }
086
087    @Override
088    public String toString() {
089        return "WireTap[" + uri + "]";
090    }
091
092    @Override
093    public String getTraceLabel() {
094        return "wireTap(" + uri + ")";
095    }
096
097    public String getId() {
098        return id;
099    }
100
101    public void setId(String id) {
102        this.id = id;
103    }
104
105    public CamelContext getCamelContext() {
106        return camelContext;
107    }
108
109    public void setCamelContext(CamelContext camelContext) {
110        this.camelContext = camelContext;
111    }
112
113    @Override
114    public boolean deferShutdown(ShutdownRunningTask shutdownRunningTask) {
115        // not in use
116        return true;
117    }
118
119    @Override
120    public int getPendingExchangesSize() {
121        return taskCount.intValue();
122    }
123
124    @Override
125    public void prepareShutdown(boolean suspendOnly, boolean forced) {
126        // noop
127    }
128
129    public EndpointUtilizationStatistics getEndpointUtilizationStatistics() {
130        return dynamicProcessor.getEndpointUtilizationStatistics();
131    }
132
133    public void process(Exchange exchange) throws Exception {
134        AsyncProcessorHelper.process(this, exchange);
135    }
136
137    public boolean process(final Exchange exchange, final AsyncCallback callback) {
138        if (!isStarted()) {
139            throw new IllegalStateException("WireTapProcessor has not been started: " + this);
140        }
141
142        // must configure the wire tap beforehand
143        Exchange target;
144        try {
145            target = configureExchange(exchange, exchangePattern);
146        } catch (Exception e) {
147            exchange.setException(e);
148            callback.done(true);
149            return true;
150        }
151
152        final Exchange wireTapExchange = target;
153
154        // send the exchange to the destination using an executor service
155        try {
156            executorService.submit(new Callable<Exchange>() {
157                public Exchange call() throws Exception {
158                    taskCount.increment();
159                    try {
160                        LOG.debug(">>>> (wiretap) {} {}", uri, wireTapExchange);
161                        processor.process(wireTapExchange);
162                    } catch (Throwable e) {
163                        LOG.warn("Error occurred during processing " + wireTapExchange + " wiretap to " + uri + ". This exception will be ignored.", e);
164                    } finally {
165                        taskCount.decrement();
166                    }
167                    return wireTapExchange;
168                }
169            });
170        } catch (Throwable e) {
171            // in case the thread pool rejects or cannot submit the task then we need to catch
172            // so camel error handler can react
173            exchange.setException(e);
174        }
175
176        // continue routing this synchronously
177        callback.done(true);
178        return true;
179    }
180
181
182    protected Exchange configureExchange(Exchange exchange, ExchangePattern pattern) throws IOException {
183        Exchange answer;
184        if (copy) {
185            // use a copy of the original exchange
186            answer = configureCopyExchange(exchange);
187        } else {
188            // use a new exchange
189            answer = configureNewExchange(exchange);
190        }
191
192        // prepare the exchange
193        if (newExchangeExpression != null) {
194            Object body = newExchangeExpression.evaluate(answer, Object.class);
195            if (body != null) {
196                answer.getIn().setBody(body);
197            }
198        }
199
200        if (newExchangeProcessors != null) {
201            for (Processor processor : newExchangeProcessors) {
202                try {
203                    processor.process(answer);
204                } catch (Exception e) {
205                    throw ObjectHelper.wrapRuntimeCamelException(e);
206                }
207            }
208        }
209
210        // if the body is a stream cache we must use a copy of the stream in the wire tapped exchange
211        Message msg = answer.hasOut() ? answer.getOut() : answer.getIn();
212        if (msg.getBody() instanceof StreamCache) {
213            // in parallel processing case, the stream must be copied, therefore get the stream
214            StreamCache cache = (StreamCache) msg.getBody();
215            StreamCache copied = cache.copy(answer);
216            if (copied != null) {
217                msg.setBody(copied);
218            }
219        }
220
221        // invoke on prepare on the exchange if specified
222        if (onPrepare != null) {
223            try {
224                onPrepare.process(answer);
225            } catch (Exception e) {
226                throw ObjectHelper.wrapRuntimeCamelException(e);
227            }
228        }
229
230        return answer;
231    }
232
233    private Exchange configureCopyExchange(Exchange exchange) {
234        // must use a copy as we dont want it to cause side effects of the original exchange
235        Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false);
236        // set MEP to InOnly as this wire tap is a fire and forget
237        copy.setPattern(ExchangePattern.InOnly);
238        // remove STREAM_CACHE_UNIT_OF_WORK property because this wire tap will
239        // close its own created stream cache(s)
240        copy.removeProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK);
241        return copy;
242    }
243
244    private Exchange configureNewExchange(Exchange exchange) {
245        return new DefaultExchange(exchange.getFromEndpoint(), ExchangePattern.InOnly);
246    }
247
248    public List<Processor> getNewExchangeProcessors() {
249        return newExchangeProcessors;
250    }
251
252    public void setNewExchangeProcessors(List<Processor> newExchangeProcessors) {
253        this.newExchangeProcessors = newExchangeProcessors;
254    }
255
256    public Expression getNewExchangeExpression() {
257        return newExchangeExpression;
258    }
259
260    public void setNewExchangeExpression(Expression newExchangeExpression) {
261        this.newExchangeExpression = newExchangeExpression;
262    }
263
264    public void addNewExchangeProcessor(Processor processor) {
265        if (newExchangeProcessors == null) {
266            newExchangeProcessors = new ArrayList<>();
267        }
268        newExchangeProcessors.add(processor);
269    }
270
271    public boolean isCopy() {
272        return copy;
273    }
274
275    public void setCopy(boolean copy) {
276        this.copy = copy;
277    }
278
279    public Processor getOnPrepare() {
280        return onPrepare;
281    }
282
283    public void setOnPrepare(Processor onPrepare) {
284        this.onPrepare = onPrepare;
285    }
286
287    public String getUri() {
288        return uri;
289    }
290
291    public int getCacheSize() {
292        return dynamicProcessor.getCacheSize();
293    }
294
295    public boolean isIgnoreInvalidEndpoint() {
296        return dynamicProcessor.isIgnoreInvalidEndpoint();
297    }
298
299    public boolean isDynamicUri() {
300        return dynamicUri;
301    }
302
303    @Override
304    protected void doStart() throws Exception {
305        ServiceHelper.startService(processor);
306    }
307
308    @Override
309    protected void doStop() throws Exception {
310        ServiceHelper.stopService(processor);
311    }
312
313    @Override
314    protected void doShutdown() throws Exception {
315        ServiceHelper.stopAndShutdownService(processor);
316        if (shutdownExecutorService) {
317            getCamelContext().getExecutorServiceManager().shutdownNow(executorService);
318        }
319    }
320}