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