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.model;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.concurrent.ExecutorService;
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlElement;
026import javax.xml.bind.annotation.XmlElementRef;
027import javax.xml.bind.annotation.XmlRootElement;
028import javax.xml.bind.annotation.XmlTransient;
029
030import org.apache.camel.Endpoint;
031import org.apache.camel.ExchangePattern;
032import org.apache.camel.Expression;
033import org.apache.camel.Processor;
034import org.apache.camel.Producer;
035import org.apache.camel.processor.CamelInternalProcessor;
036import org.apache.camel.processor.WireTapProcessor;
037import org.apache.camel.spi.Metadata;
038import org.apache.camel.spi.RouteContext;
039import org.apache.camel.util.CamelContextHelper;
040
041/**
042 * Routes a copy of a message (or creates a new message) to a secondary destination while continue routing the original message.
043 */
044@Metadata(label = "eip,endpoint,routing")
045@XmlRootElement(name = "wireTap")
046@XmlAccessorType(XmlAccessType.FIELD)
047public class WireTapDefinition<Type extends ProcessorDefinition<Type>> extends NoOutputDefinition<WireTapDefinition<Type>>
048        implements ExecutorServiceAwareDefinition<WireTapDefinition<Type>>, EndpointRequiredDefinition {
049    @XmlAttribute
050    protected String uri;
051    @XmlAttribute
052    @Deprecated
053    protected String ref;
054    @XmlTransient
055    protected Endpoint endpoint;
056    @XmlTransient
057    private Processor newExchangeProcessor;
058    @XmlAttribute(name = "processorRef")
059    private String newExchangeProcessorRef;
060    @XmlElement(name = "body")
061    private ExpressionSubElementDefinition newExchangeExpression;
062    @XmlElementRef
063    private List<SetHeaderDefinition> headers = new ArrayList<SetHeaderDefinition>();
064    @XmlTransient
065    private ExecutorService executorService;
066    @XmlAttribute
067    private String executorServiceRef;
068    @XmlAttribute @Metadata(defaultValue = "true")
069    private Boolean copy;
070    @XmlAttribute
071    private String onPrepareRef;
072    @XmlTransient
073    private Processor onPrepare;
074
075    public WireTapDefinition() {
076    }
077
078    public WireTapDefinition(String uri) {
079        setUri(uri);
080    }
081
082    public WireTapDefinition(Endpoint endpoint) {
083        setEndpoint(endpoint);
084    }
085
086    @Override
087    public String getEndpointUri() {
088        if (uri != null) {
089            return uri;
090        } else if (endpoint != null) {
091            return endpoint.getEndpointUri();
092        } else {
093            return null;
094        }
095    }
096
097    @Override
098    public Processor createProcessor(RouteContext routeContext) throws Exception {
099        // executor service is mandatory for wire tap
100        boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, true);
101        ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "WireTap", this, true);
102
103        // create the producer to send to the wire tapped endpoint
104        Endpoint endpoint = resolveEndpoint(routeContext);
105        Producer producer = endpoint.createProducer();
106
107        // create error handler we need to use for processing the wire tapped
108        Processor target = wrapInErrorHandler(routeContext, producer);
109
110        // and wrap in unit of work
111        String routeId = routeContext.getRoute().idOrCreate(routeContext.getCamelContext().getNodeIdFactory());
112        CamelInternalProcessor internal = new CamelInternalProcessor(target);
113        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeId));
114
115        // is true bt default
116        boolean isCopy = getCopy() == null || getCopy();
117
118        WireTapProcessor answer = new WireTapProcessor(endpoint, internal, getPattern(), threadPool, shutdownThreadPool);
119        answer.setCopy(isCopy);
120        if (newExchangeProcessorRef != null) {
121            newExchangeProcessor = routeContext.mandatoryLookup(newExchangeProcessorRef, Processor.class);
122        }
123        if (newExchangeProcessor != null) {
124            answer.addNewExchangeProcessor(newExchangeProcessor);
125        }
126        if (newExchangeExpression != null) {
127            answer.setNewExchangeExpression(newExchangeExpression.createExpression(routeContext));
128        }
129        if (headers != null && !headers.isEmpty()) {
130            for (SetHeaderDefinition header : headers) {
131                Processor processor = createProcessor(routeContext, header);
132                answer.addNewExchangeProcessor(processor);
133            }
134        }
135        if (onPrepareRef != null) {
136            onPrepare = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onPrepareRef, Processor.class);
137        }
138        if (onPrepare != null) {
139            answer.setOnPrepare(onPrepare);
140        }
141
142        return answer;
143    }
144
145    public ExchangePattern getPattern() {
146        return ExchangePattern.InOnly;
147    }
148
149    @Override
150    public String toString() {
151        return "WireTap[" + description() + "]";
152    }
153    
154    protected String description() {
155        return FromDefinition.description(getUri(), getRef(), getEndpoint());
156    }
157
158    @Override
159    public String getLabel() {
160        return "wireTap[" + description() + "]";
161    }
162
163    @Override
164    @SuppressWarnings("unchecked")
165    public Type end() {
166        // allow end() to return to previous type so you can continue in the DSL
167        return (Type) super.end();
168    }
169
170    @Override
171    public void addOutput(ProcessorDefinition<?> output) {
172        // add outputs on parent as this wiretap does not support outputs
173        getParent().addOutput(output);
174    }
175
176    public Endpoint resolveEndpoint(RouteContext context) {
177        if (endpoint == null) {
178            return context.resolveEndpoint(getUri(), getRef());
179        } else {
180            return endpoint;
181        }
182    }
183
184    // Fluent API
185    // -------------------------------------------------------------------------
186
187    /**
188     * Uses a custom thread pool
189     *
190     * @param executorService a custom {@link ExecutorService} to use as thread pool
191     *                        for sending tapped exchanges
192     * @return the builder
193     */
194    public WireTapDefinition<Type> executorService(ExecutorService executorService) {
195        setExecutorService(executorService);
196        return this;
197    }
198
199    /**
200     * Uses a custom thread pool
201     *
202     * @param executorServiceRef reference to lookup a custom {@link ExecutorService}
203     *                           to use as thread pool for sending tapped exchanges
204     * @return the builder
205     */
206    public WireTapDefinition<Type> executorServiceRef(String executorServiceRef) {
207        setExecutorServiceRef(executorServiceRef);
208        return this;
209    }
210
211    /**
212     * Uses a copy of the original exchange
213     *
214     * @return the builder
215     */
216    public WireTapDefinition<Type> copy() {
217        setCopy(true);
218        return this;
219    }
220    
221    /**
222     * Uses a copy of the original exchange
223     *
224     * @param copy if it is true camel will copy the original exchange,
225     *             if it is false camel will not copy the original exchange 
226     * @return the builder
227     */
228    public WireTapDefinition<Type> copy(boolean copy) {
229        setCopy(copy);
230        return this;
231    }
232
233    /**
234     * @deprecated will be removed in Camel 3.0 Instead use {@link #newExchangeBody(org.apache.camel.Expression)}
235     */
236    @Deprecated
237    public WireTapDefinition<Type> newExchange(Expression expression) {
238        return newExchangeBody(expression);
239    }
240
241    /**
242     * Sends a <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}
243     *
244     * @param expression expression that creates the new body to send
245     * @return the builder
246     * @see #newExchangeHeader(String, org.apache.camel.Expression)
247     */
248    public WireTapDefinition<Type> newExchangeBody(Expression expression) {
249        setNewExchangeExpression(expression);
250        return this;
251    }
252
253    /**
254     * Sends a <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}
255     *
256     * @param ref reference to the {@link Processor} to lookup in the {@link org.apache.camel.spi.Registry} to
257     *            be used for preparing the new exchange to send
258     * @return the builder
259     */
260    public WireTapDefinition<Type> newExchangeRef(String ref) {
261        setNewExchangeProcessorRef(ref);
262        return this;
263    }
264
265    /**
266     * Sends a <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}
267     *
268     * @param processor  processor preparing the new exchange to send
269     * @return the builder
270     * @see #newExchangeHeader(String, org.apache.camel.Expression)
271     */
272    public WireTapDefinition<Type> newExchange(Processor processor) {
273        setNewExchangeProcessor(processor);
274        return this;
275    }
276
277    /**
278     * Sets a header on the <i>new</i> Exchange, instead of tapping an existing, using {@link ExchangePattern#InOnly}.
279     * <p/>
280     * Use this together with the {@link #newExchange(org.apache.camel.Expression)} or {@link #newExchange(org.apache.camel.Processor)}
281     * methods.
282     *
283     * @param headerName  the header name
284     * @param expression  the expression setting the header value
285     * @return the builder
286     */
287    public WireTapDefinition<Type> newExchangeHeader(String headerName, Expression expression) {
288        headers.add(new SetHeaderDefinition(headerName, expression));
289        return this;
290    }
291
292    /**
293     * Uses the {@link Processor} when preparing the {@link org.apache.camel.Exchange} to be send.
294     * This can be used to deep-clone messages that should be send, or any custom logic needed before
295     * the exchange is send.
296     *
297     * @param onPrepare the processor
298     * @return the builder
299     */
300    public WireTapDefinition<Type> onPrepare(Processor onPrepare) {
301        setOnPrepare(onPrepare);
302        return this;
303    }
304
305    /**
306     * Uses the {@link Processor} when preparing the {@link org.apache.camel.Exchange} to be send.
307     * This can be used to deep-clone messages that should be send, or any custom logic needed before
308     * the exchange is send.
309     *
310     * @param onPrepareRef reference to the processor to lookup in the {@link org.apache.camel.spi.Registry}
311     * @return the builder
312     */
313    public WireTapDefinition<Type> onPrepareRef(String onPrepareRef) {
314        setOnPrepareRef(onPrepareRef);
315        return this;
316    }
317
318    public String getUri() {
319        return uri;
320    }
321
322    /**
323     * Uri of the endpoint to use as wire tap
324     */
325    public void setUri(String uri) {
326        this.uri = uri;
327    }
328
329    public String getRef() {
330        return ref;
331    }
332
333    /**
334     * Reference of the endpoint to use as wire tap
335     *
336     * @deprecated use uri with ref:uri instead
337     */
338    @Deprecated
339    public void setRef(String ref) {
340        this.ref = ref;
341    }
342
343    public Endpoint getEndpoint() {
344        return endpoint;
345    }
346
347    public void setEndpoint(Endpoint endpoint) {
348        this.endpoint = endpoint;
349    }
350
351    public Processor getNewExchangeProcessor() {
352        return newExchangeProcessor;
353    }
354
355    /**
356     * To use a Processor for creating a new body as the message to use for wire tapping
357     */
358    public void setNewExchangeProcessor(Processor processor) {
359        this.newExchangeProcessor = processor;
360    }
361
362    public String getNewExchangeProcessorRef() {
363        return newExchangeProcessorRef;
364    }
365
366    /**
367     * Reference to a Processor to use for creating a new body as the message to use for wire tapping
368     */
369    public void setNewExchangeProcessorRef(String ref) {
370        this.newExchangeProcessorRef = ref;
371    }
372
373    public ExpressionSubElementDefinition getNewExchangeExpression() {
374        return newExchangeExpression;
375    }
376
377    /**
378     * Expression used for creating a new body as the message to use for wire tapping
379     */
380    public void setNewExchangeExpression(ExpressionSubElementDefinition expression) {
381        this.newExchangeExpression = expression;
382    }
383
384    public void setNewExchangeExpression(Expression expression) {
385        this.newExchangeExpression = new ExpressionSubElementDefinition(expression);
386    }
387
388    public ExecutorService getExecutorService() {
389        return executorService;
390    }
391
392    public void setExecutorService(ExecutorService executorService) {
393        this.executorService = executorService;
394    }
395
396    public String getExecutorServiceRef() {
397        return executorServiceRef;
398    }
399
400    public void setExecutorServiceRef(String executorServiceRef) {
401        this.executorServiceRef = executorServiceRef;
402    }
403
404    public Boolean getCopy() {
405        return copy;
406    }
407
408    public void setCopy(Boolean copy) {
409        this.copy = copy;
410    }
411
412    public String getOnPrepareRef() {
413        return onPrepareRef;
414    }
415
416    public void setOnPrepareRef(String onPrepareRef) {
417        this.onPrepareRef = onPrepareRef;
418    }
419
420    public Processor getOnPrepare() {
421        return onPrepare;
422    }
423
424    public void setOnPrepare(Processor onPrepare) {
425        this.onPrepare = onPrepare;
426    }
427
428    public List<SetHeaderDefinition> getHeaders() {
429        return headers;
430    }
431
432    public void setHeaders(List<SetHeaderDefinition> headers) {
433        this.headers = headers;
434    }
435}