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     */
017    package org.apache.camel.model;
018    
019    import java.util.concurrent.LinkedBlockingQueue;
020    import java.util.concurrent.ThreadPoolExecutor;
021    import java.util.concurrent.TimeUnit;
022    
023    import javax.xml.bind.annotation.XmlAccessType;
024    import javax.xml.bind.annotation.XmlAccessorType;
025    import javax.xml.bind.annotation.XmlAttribute;
026    import javax.xml.bind.annotation.XmlRootElement;
027    import javax.xml.bind.annotation.XmlTransient;
028    
029    import org.apache.camel.Expression;
030    import org.apache.camel.Processor;
031    import org.apache.camel.model.language.ExpressionType;
032    import org.apache.camel.processor.Splitter;
033    import org.apache.camel.processor.aggregate.AggregationStrategy;
034    import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
035    import org.apache.camel.spi.RouteContext;
036    
037    /**
038     * Represents an XML <splitter/> element
039     *
040     * @version $Revision: 41908 $
041     */
042    @XmlRootElement(name = "splitter")
043    @XmlAccessorType(XmlAccessType.FIELD)
044    public class SplitterType extends ExpressionNode {
045        @XmlTransient
046        private AggregationStrategy aggregationStrategy;
047        @XmlAttribute(required = false)
048        private Boolean parallelProcessing;
049        @XmlTransient
050        private ThreadPoolExecutor threadPoolExecutor;
051        
052        public SplitterType() {
053        }
054    
055        public SplitterType(Expression expression) {
056            super(expression);
057        }
058    
059        public SplitterType(ExpressionType expression) {
060            super(expression);
061        }
062    
063        @Override
064        public String toString() {
065            return "Splitter[ " + getExpression() + " -> " + getOutputs() + "]";
066        }
067    
068        @Override
069        public Processor createProcessor(RouteContext routeContext) throws Exception {
070            Processor childProcessor = routeContext.createProcessor(this);
071            if (aggregationStrategy == null) {
072                aggregationStrategy = new UseLatestAggregationStrategy();
073            }
074            if (threadPoolExecutor == null) {
075                threadPoolExecutor = new ThreadPoolExecutor(4, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
076            }
077            return new Splitter(getExpression().createExpression(routeContext), childProcessor, aggregationStrategy,
078                    isParallelProcessing(), threadPoolExecutor);
079        }
080        
081        public AggregationStrategy getAggregationStrategy() {
082            return aggregationStrategy;
083        }
084    
085        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
086            this.aggregationStrategy = aggregationStrategy;
087        }
088        
089        public boolean isParallelProcessing() {
090            return parallelProcessing != null ? parallelProcessing : false;
091        }
092    
093        public void setParallelProcessing(boolean parallelProcessing) {
094            this.parallelProcessing = parallelProcessing;
095        }
096    
097        public ThreadPoolExecutor getThreadPoolExecutor() {
098            return threadPoolExecutor;
099        }
100    
101        public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
102            this.threadPoolExecutor = threadPoolExecutor;
103        }
104    }