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.ArrayList;
020import java.util.List;
021import java.util.concurrent.ExecutorService;
022
023import org.apache.camel.AggregationStrategy;
024import org.apache.camel.CamelContextAware;
025import org.apache.camel.Expression;
026import org.apache.camel.Processor;
027import org.apache.camel.Route;
028import org.apache.camel.model.ProcessorDefinition;
029import org.apache.camel.model.RecipientListDefinition;
030import org.apache.camel.processor.EvaluateExpressionProcessor;
031import org.apache.camel.processor.RecipientList;
032import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter;
033import org.apache.camel.processor.aggregate.ShareUnitOfWorkAggregationStrategy;
034import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
035
036public class RecipientListReifier extends ProcessorReifier<RecipientListDefinition<?>> {
037
038    public RecipientListReifier(Route route, ProcessorDefinition<?> definition) {
039        super(route, (RecipientListDefinition<?>)definition);
040    }
041
042    @Override
043    public Processor createProcessor() throws Exception {
044        final Expression expression = createExpression(definition.getExpression());
045
046        boolean isParallelProcessing = parseBoolean(definition.getParallelProcessing(), false);
047        boolean isStreaming = parseBoolean(definition.getStreaming(), false);
048        boolean isParallelAggregate = parseBoolean(definition.getParallelAggregate(), false);
049        boolean isShareUnitOfWork = parseBoolean(definition.getShareUnitOfWork(), false);
050        boolean isStopOnException = parseBoolean(definition.getStopOnException(), false);
051        boolean isIgnoreInvalidEndpoints = parseBoolean(definition.getIgnoreInvalidEndpoints(), false);
052        boolean isStopOnAggregateException = parseBoolean(definition.getStopOnAggregateException(), false);
053
054        RecipientList answer;
055        if (definition.getDelimiter() != null) {
056            answer = new RecipientList(camelContext, expression, parseString(definition.getDelimiter()));
057        } else {
058            answer = new RecipientList(camelContext, expression);
059        }
060        answer.setAggregationStrategy(createAggregationStrategy());
061        answer.setParallelProcessing(isParallelProcessing);
062        answer.setParallelAggregate(isParallelAggregate);
063        answer.setStreaming(isStreaming);
064        answer.setShareUnitOfWork(isShareUnitOfWork);
065        answer.setStopOnException(isStopOnException);
066        answer.setIgnoreInvalidEndpoints(isIgnoreInvalidEndpoints);
067        answer.setStopOnAggregateException(isStopOnAggregateException);
068        if (definition.getCacheSize() != null) {
069            answer.setCacheSize(parseInt(definition.getCacheSize()));
070        }
071        if (definition.getOnPrepareRef() != null) {
072            definition.setOnPrepare(mandatoryLookup(definition.getOnPrepareRef(), Processor.class));
073        }
074        if (definition.getOnPrepare() != null) {
075            answer.setOnPrepare(definition.getOnPrepare());
076        }
077        if (definition.getTimeout() != null) {
078            answer.setTimeout(parseDuration(definition.getTimeout()));
079        }
080
081        boolean shutdownThreadPool = willCreateNewThreadPool(definition, isParallelProcessing);
082        ExecutorService threadPool = getConfiguredExecutorService("RecipientList", definition, isParallelProcessing);
083        answer.setExecutorService(threadPool);
084        answer.setShutdownExecutorService(shutdownThreadPool);
085        long timeout = definition.getTimeout() != null ? parseDuration(definition.getTimeout()) : 0;
086        if (timeout > 0 && !isParallelProcessing) {
087            throw new IllegalArgumentException("Timeout is used but ParallelProcessing has not been enabled.");
088        }
089
090        // create a pipeline with two processors
091        // the first is the eval processor which evaluates the expression to use
092        // the second is the recipient list
093        List<Processor> pipe = new ArrayList<>(2);
094
095        // the eval processor must be wrapped in error handler, so in case there
096        // was an
097        // error during evaluation, the error handler can deal with it
098        // the recipient list is not in error handler, as its has its own
099        // special error handling
100        // when sending to the recipients individually
101        Processor evalProcessor = new EvaluateExpressionProcessor(expression);
102        evalProcessor = wrapInErrorHandler(evalProcessor, true);
103
104        pipe.add(evalProcessor);
105        pipe.add(answer);
106
107        // wrap recipient list in nested pipeline so this appears as one processor
108        return answer.newPipeline(camelContext, pipe);
109    }
110
111    private AggregationStrategy createAggregationStrategy() {
112        AggregationStrategy strategy = definition.getAggregationStrategy();
113        if (strategy == null && definition.getStrategyRef() != null) {
114            Object aggStrategy = lookup(parseString(definition.getStrategyRef()), Object.class);
115            if (aggStrategy instanceof AggregationStrategy) {
116                strategy = (AggregationStrategy)aggStrategy;
117            } else if (aggStrategy != null) {
118                AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(aggStrategy, parseString(definition.getStrategyMethodName()));
119                if (definition.getStrategyMethodAllowNull() != null) {
120                    adapter.setAllowNullNewExchange(parseBoolean(definition.getStrategyMethodAllowNull(), false));
121                    adapter.setAllowNullOldExchange(parseBoolean(definition.getStrategyMethodAllowNull(), false));
122                }
123                strategy = adapter;
124            } else {
125                throw new IllegalArgumentException("Cannot find AggregationStrategy in Registry with name: " + definition.getStrategyRef());
126            }
127        }
128
129        if (strategy == null) {
130            // default to use latest aggregation strategy
131            strategy = new UseLatestAggregationStrategy();
132        }
133
134        if (strategy instanceof CamelContextAware) {
135            ((CamelContextAware)strategy).setCamelContext(camelContext);
136        }
137
138        if (parseBoolean(definition.getShareUnitOfWork(), false)) {
139            // wrap strategy in share unit of work
140            strategy = new ShareUnitOfWorkAggregationStrategy(strategy);
141        }
142
143        return strategy;
144    }
145
146}