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.List;
020    import java.util.concurrent.ThreadPoolExecutor;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    
028    import org.apache.camel.Processor;
029    import org.apache.camel.processor.MulticastProcessor;
030    import org.apache.camel.processor.aggregate.AggregationStrategy;
031    import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
032    import org.apache.camel.processor.interceptor.StreamCachingInterceptor;
033    import org.apache.camel.spi.RouteContext;
034    
035    /**
036     * Represents an XML <multicast/> element
037     *
038     * @version $Revision: 64898 $
039     */
040    @XmlRootElement(name = "multicast")
041    @XmlAccessorType(XmlAccessType.FIELD)
042    public class MulticastType extends OutputType<ProcessorType> {
043        @XmlAttribute(required = false)
044        private Boolean parallelProcessing;
045        @XmlAttribute(required = false)
046        private String strategyRef;
047        @XmlAttribute(required = false)
048        private String threadPoolRef;    
049        @XmlTransient
050        private AggregationStrategy aggregationStrategy;
051        @XmlTransient
052        private ThreadPoolExecutor threadPoolExecutor;
053    
054        @Override
055        public String toString() {
056            return "Multicast[" + getOutputs() + "]";
057        }
058    
059        @Override
060        public String getShortName() {
061            return "multicast";
062        }
063    
064        @Override
065        public Processor createProcessor(RouteContext routeContext) throws Exception {
066            return createOutputsProcessor(routeContext);
067        }
068    
069        // Fluent API
070        // -------------------------------------------------------------------------
071        /**
072         * Set the multicasting aggregationStrategy
073         * @param aggregationStrategy 
074         *
075         * @return the builder
076         */
077        public MulticastType aggregationStrategy(AggregationStrategy aggregationStrategy) {
078            setAggregationStrategy(aggregationStrategy);
079            return this;
080        }
081        
082        /**
083         * Set to run the multicasting action parallely
084         *
085         * @return the builder
086         */
087        public MulticastType parallelProcessing() {
088            setParallelProcessing(true);
089            return this;
090        }
091        
092        /**
093         * Set the multicasting action's thread model
094         * @param parallelProcessing <tt>true</tt> to use a thread pool, 
095         * if <tt>false</tt> then work is done in the calling thread
096         *
097         * @return the builder
098         */
099        public MulticastType parallelProcessing(boolean parallelProcessing) {
100            setParallelProcessing(parallelProcessing);
101            return this;
102        }
103        
104        /**
105         * Setting the executor for executing the multicasting action.
106         *   
107         * @param executor , it should be a instance of ThreadPoolExcutor
108         * NOTE in Camel 2.0 , it will change to use the instance which implements Executor interface
109         *
110         * @return the builder
111         */
112        public MulticastType executor(ThreadPoolExecutor executor) {
113            setThreadPoolExecutor(executor);
114            return this;
115        }    
116            
117        protected Processor createCompositeProcessor(RouteContext routeContext, List<Processor> list) {
118            if (aggregationStrategy == null && strategyRef != null) {
119                aggregationStrategy = routeContext.lookup(strategyRef, AggregationStrategy.class);
120            }
121            if (aggregationStrategy == null) {
122                aggregationStrategy = new UseLatestAggregationStrategy();
123            }
124            if (threadPoolRef != null) {
125                threadPoolExecutor = routeContext.lookup(threadPoolRef, ThreadPoolExecutor.class);
126            }
127            return new MulticastProcessor(list, aggregationStrategy, isParallelProcessing(), threadPoolExecutor);
128        }
129    
130        public AggregationStrategy getAggregationStrategy() {
131            return aggregationStrategy;
132        }
133        
134        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
135            this.aggregationStrategy = aggregationStrategy;        
136        }
137    
138        public boolean isParallelProcessing() {
139            return parallelProcessing != null ? parallelProcessing : false;
140        }
141    
142        public void setParallelProcessing(boolean parallelProcessing) {
143            this.parallelProcessing = parallelProcessing;        
144        }
145    
146        public ThreadPoolExecutor getThreadPoolExecutor() {
147            return threadPoolExecutor;
148        }
149    
150        public void setThreadPoolExecutor(ThreadPoolExecutor executor) {
151            this.threadPoolExecutor = executor;        
152    
153        }
154        
155        @Override
156        protected Processor wrapProcessorInInterceptors(RouteContext routeContext, Processor target) throws Exception {        
157            //CAMEL-1193 now we need to wrap the multicast processor with the interceptors
158            //Current we wrap the StreamCachingInterceptor by default
159            return super.wrapProcessorInInterceptors(routeContext, new StreamCachingInterceptor(target));        
160        }
161    }