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