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: 671918 $
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 String getShortName() {
070            return "splitter";
071        }
072    
073        @Override
074        public Processor createProcessor(RouteContext routeContext) throws Exception {
075            Processor childProcessor = routeContext.createProcessor(this);
076            if (aggregationStrategy == null) {
077                aggregationStrategy = new UseLatestAggregationStrategy();
078            }
079            if (threadPoolExecutor == null) {
080                threadPoolExecutor = new ThreadPoolExecutor(4, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
081            }
082            return new Splitter(getExpression().createExpression(routeContext), childProcessor, aggregationStrategy,
083                    isParallelProcessing(), threadPoolExecutor);
084        }
085        
086        public AggregationStrategy getAggregationStrategy() {
087            return aggregationStrategy;
088        }
089    
090        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
091            this.aggregationStrategy = aggregationStrategy;
092        }
093        
094        public boolean isParallelProcessing() {
095            return parallelProcessing != null ? parallelProcessing : false;
096        }
097    
098        public void setParallelProcessing(boolean parallelProcessing) {
099            this.parallelProcessing = parallelProcessing;
100        }
101    
102        public ThreadPoolExecutor getThreadPoolExecutor() {
103            return threadPoolExecutor;
104        }
105    
106        public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
107            this.threadPoolExecutor = threadPoolExecutor;
108        }
109    }