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