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.reifier; 018 019import java.util.concurrent.ExecutorService; 020 021import org.apache.camel.ExchangePattern; 022import org.apache.camel.Expression; 023import org.apache.camel.Processor; 024import org.apache.camel.builder.ExpressionBuilder; 025import org.apache.camel.model.ProcessorDefinition; 026import org.apache.camel.model.ProcessorDefinitionHelper; 027import org.apache.camel.model.SetHeaderDefinition; 028import org.apache.camel.model.WireTapDefinition; 029import org.apache.camel.processor.CamelInternalProcessor; 030import org.apache.camel.processor.SendDynamicProcessor; 031import org.apache.camel.processor.WireTapProcessor; 032import org.apache.camel.spi.RouteContext; 033import org.apache.camel.support.CamelContextHelper; 034 035public class WireTapReifier extends ToDynamicReifier<WireTapDefinition<?>> { 036 037 public WireTapReifier(ProcessorDefinition<?> definition) { 038 super(definition); 039 } 040 041 @Override 042 public Processor createProcessor(RouteContext routeContext) throws Exception { 043 // executor service is mandatory for wire tap 044 boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, definition, true); 045 ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "WireTap", definition, true); 046 047 // must use InOnly for WireTap 048 definition.setPattern(ExchangePattern.InOnly); 049 050 // create the send dynamic producer to send to the wire tapped endpoint 051 SendDynamicProcessor dynamicTo = (SendDynamicProcessor)super.createProcessor(routeContext); 052 053 // create error handler we need to use for processing the wire tapped 054 Processor target = wrapInErrorHandler(routeContext, dynamicTo); 055 056 // and wrap in unit of work 057 CamelInternalProcessor internal = new CamelInternalProcessor(target); 058 internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext)); 059 060 // is true by default 061 boolean isCopy = definition.getCopy() == null || definition.getCopy(); 062 063 WireTapProcessor answer = new WireTapProcessor(dynamicTo, internal, definition.getPattern(), threadPool, shutdownThreadPool, definition.isDynamic()); 064 answer.setCopy(isCopy); 065 if (definition.getNewExchangeProcessorRef() != null) { 066 definition.setNewExchangeProcessor(routeContext.mandatoryLookup(definition.getNewExchangeProcessorRef(), Processor.class)); 067 } 068 if (definition.getNewExchangeProcessor() != null) { 069 answer.addNewExchangeProcessor(definition.getNewExchangeProcessor()); 070 } 071 if (definition.getNewExchangeExpression() != null) { 072 answer.setNewExchangeExpression(definition.getNewExchangeExpression().createExpression(routeContext)); 073 } 074 if (definition.getHeaders() != null && !definition.getHeaders().isEmpty()) { 075 for (SetHeaderDefinition header : definition.getHeaders()) { 076 Processor processor = createProcessor(routeContext, header); 077 answer.addNewExchangeProcessor(processor); 078 } 079 } 080 if (definition.getOnPrepareRef() != null) { 081 definition.setOnPrepare(CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), definition.getOnPrepareRef(), Processor.class)); 082 } 083 if (definition.getOnPrepare() != null) { 084 answer.setOnPrepare(definition.getOnPrepare()); 085 } 086 087 return answer; 088 } 089 090 @Override 091 protected Expression createExpression(RouteContext routeContext, String uri) { 092 // whether to use dynamic or static uri 093 if (definition.isDynamic()) { 094 return super.createExpression(routeContext, uri); 095 } else { 096 return ExpressionBuilder.constantExpression(uri); 097 } 098 } 099 100}