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.AggregationStrategy;
022import org.apache.camel.CamelContextAware;
023import org.apache.camel.Expression;
024import org.apache.camel.Processor;
025import org.apache.camel.model.ProcessorDefinition;
026import org.apache.camel.model.SplitDefinition;
027import org.apache.camel.processor.Splitter;
028import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter;
029import org.apache.camel.processor.aggregate.ShareUnitOfWorkAggregationStrategy;
030import org.apache.camel.spi.RouteContext;
031import org.apache.camel.support.CamelContextHelper;
032
033public class SplitReifier extends ExpressionReifier<SplitDefinition> {
034
035    public SplitReifier(RouteContext routeContext, ProcessorDefinition<?> definition) {
036        super(routeContext, (SplitDefinition)definition);
037    }
038
039    @Override
040    public Processor createProcessor() throws Exception {
041        Processor childProcessor = this.createChildProcessor(true);
042        definition.setAggregationStrategy(createAggregationStrategy());
043
044        boolean isParallelProcessing = parseBoolean(definition.getParallelProcessing(), false);
045        boolean isStreaming = parseBoolean(definition.getStreaming(), false);
046        boolean isShareUnitOfWork = parseBoolean(definition.getShareUnitOfWork(), false);
047        boolean isParallelAggregate = parseBoolean(definition.getParallelAggregate(), false);
048        boolean isStopOnAggregateException = parseBoolean(definition.getStopOnAggregateException(), false);
049        boolean isStopOnException = parseBoolean(definition.getStopOnException(), false);
050        boolean shutdownThreadPool = willCreateNewThreadPool(definition, isParallelProcessing);
051        ExecutorService threadPool = getConfiguredExecutorService("Split", definition, isParallelProcessing);
052
053        long timeout = definition.getTimeout() != null ? parseLong(definition.getTimeout()) : 0;
054        if (timeout > 0 && !isParallelProcessing) {
055            throw new IllegalArgumentException("Timeout is used but ParallelProcessing has not been enabled.");
056        }
057        if (definition.getOnPrepareRef() != null) {
058            definition.setOnPrepare(CamelContextHelper.mandatoryLookup(camelContext, parseString(definition.getOnPrepareRef()), Processor.class));
059        }
060
061        Expression exp = createExpression(definition.getExpression());
062
063        Splitter answer = new Splitter(camelContext, exp, childProcessor, definition.getAggregationStrategy(), isParallelProcessing, threadPool,
064                                       shutdownThreadPool, isStreaming, isStopOnException, timeout, definition.getOnPrepare(), isShareUnitOfWork, isParallelAggregate,
065                                       isStopOnAggregateException);
066        return answer;
067    }
068
069    private AggregationStrategy createAggregationStrategy() {
070        AggregationStrategy strategy = definition.getAggregationStrategy();
071        if (strategy == null && definition.getStrategyRef() != null) {
072            Object aggStrategy = routeContext.lookup(definition.getStrategyRef(), Object.class);
073            if (aggStrategy instanceof AggregationStrategy) {
074                strategy = (AggregationStrategy)aggStrategy;
075            } else if (aggStrategy != null) {
076                AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(aggStrategy, definition.getStrategyMethodName());
077                if (definition.getStrategyMethodAllowNull() != null) {
078                    adapter.setAllowNullNewExchange(parseBoolean(definition.getStrategyMethodAllowNull(), false));
079                    adapter.setAllowNullOldExchange(parseBoolean(definition.getStrategyMethodAllowNull(), false));
080                }
081                strategy = adapter;
082            } else {
083                throw new IllegalArgumentException("Cannot find AggregationStrategy in Registry with name: " + definition.getStrategyRef());
084            }
085        }
086
087        if (strategy instanceof CamelContextAware) {
088            ((CamelContextAware)strategy).setCamelContext(camelContext);
089        }
090
091        if (strategy != null && parseBoolean(definition.getShareUnitOfWork(), false)) {
092            // wrap strategy in share unit of work
093            strategy = new ShareUnitOfWorkAggregationStrategy(strategy);
094        }
095
096        return strategy;
097    }
098
099}