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.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
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.XmlElement;
027    import javax.xml.bind.annotation.XmlElementRef;
028    import javax.xml.bind.annotation.XmlElements;
029    import javax.xml.bind.annotation.XmlRootElement;
030    
031    import org.apache.camel.Exchange;
032    import org.apache.camel.Expression;
033    import org.apache.camel.Processor;
034    import org.apache.camel.model.loadbalancer.LoadBalancerType;
035    import org.apache.camel.model.loadbalancer.RandomLoadBalanceStrategy;
036    import org.apache.camel.model.loadbalancer.RoundRobinLoadBalanceStrategy;
037    import org.apache.camel.model.loadbalancer.StickyLoadBalanceStrategy;
038    import org.apache.camel.model.loadbalancer.TopicLoadBalanceStrategy;
039    import org.apache.camel.processor.SendProcessor;
040    import org.apache.camel.processor.loadbalancer.LoadBalancer;
041    import org.apache.camel.processor.loadbalancer.RandomLoadBalancer;
042    import org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer;
043    import org.apache.camel.processor.loadbalancer.StickyLoadBalancer;
044    import org.apache.camel.processor.loadbalancer.TopicLoadBalancer;
045    import org.apache.camel.spi.RouteContext;
046    import org.apache.camel.util.CollectionStringBuffer;
047    
048    /**
049     * Represents an XML <loadBalance/> element
050     */
051    @XmlRootElement(name = "loadBalance")
052    @XmlAccessorType(XmlAccessType.FIELD)
053    public class LoadBalanceType extends ProcessorType<LoadBalanceType> {
054        @XmlAttribute(required = false)
055        private String ref;
056    
057        @XmlElements({
058            @XmlElement(required = false, name = "roundRobin", type = RoundRobinLoadBalanceStrategy.class),
059            @XmlElement(required = false, name = "random", type = RandomLoadBalanceStrategy.class),
060            @XmlElement(required = false, name = "sticky", type = StickyLoadBalanceStrategy.class),
061            @XmlElement(required = false, name = "topic", type = TopicLoadBalanceStrategy.class)}
062            )
063        private LoadBalancerType loadBalancerType;
064    
065        @XmlElementRef
066        private List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>();
067    
068        public LoadBalanceType() {
069        }
070    
071        @Override
072        public String getShortName() {
073            return "loadbalance";
074        }
075    
076        public List<ProcessorType<?>> getOutputs() {
077            return outputs;
078        }
079    
080        public void setOutputs(List<ProcessorType<?>> outputs) {
081            this.outputs = outputs;
082            if (outputs != null) {
083                for (ProcessorType output : outputs) {
084                    configureChild(output);
085                }
086            }
087        }
088    
089    
090        @Override
091        protected void configureChild(ProcessorType output) {
092            super.configureChild(output);
093            if (isInheritErrorHandler()) {
094                output.setErrorHandlerBuilder(getErrorHandlerBuilder());
095            }
096        }
097    
098        public String getRef() {
099            return ref;
100        }
101    
102        public void setRef(String ref) {
103            this.ref = ref;
104        }
105    
106        public LoadBalancerType getLoadBalancerType() {
107            return loadBalancerType;
108        }
109    
110        public void setLoadBalancerType(LoadBalancerType loadbalancer) {
111            loadBalancerType = loadbalancer;
112        }
113    
114        protected Processor createOutputsProcessor(RouteContext routeContext, Collection<ProcessorType<?>> outputs)
115            throws Exception {
116            LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref);
117            for (ProcessorType processorType : outputs) {
118                // The outputs should be the SendProcessor
119                SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext);
120                loadBalancer.addProcessor(processor);
121            }
122            return loadBalancer;
123        }
124    
125        // when this method will be called
126        @Override
127        public Processor createProcessor(RouteContext routeContext) throws Exception {
128            LoadBalancer loadBalancer = LoadBalancerType.getLoadBalancer(routeContext, loadBalancerType, ref);
129            for (ProcessorType processorType : getOutputs()) {
130                // The outputs should be the SendProcessor
131                SendProcessor processor = (SendProcessor) processorType.createProcessor(routeContext);
132                loadBalancer.addProcessor(processor);
133            }
134    
135            return loadBalancer;
136        }
137    
138        // Fluent API
139        // -------------------------------------------------------------------------
140        public LoadBalanceType setLoadBalancer(LoadBalancer loadBalancer) {
141            loadBalancerType = new LoadBalancerType(loadBalancer);
142            return this;
143        }
144    
145        public LoadBalanceType roundRobin() {
146            loadBalancerType = new LoadBalancerType(new RoundRobinLoadBalancer());
147            return this;
148        }
149    
150        public LoadBalanceType random() {
151            loadBalancerType = new LoadBalancerType(new RandomLoadBalancer());
152            return this;
153        }
154    
155        public LoadBalanceType sticky(Expression<Exchange> correlationExpression) {
156            loadBalancerType = new LoadBalancerType(new StickyLoadBalancer(correlationExpression));
157            return this;
158        }
159    
160        public LoadBalanceType topic() {
161            loadBalancerType = new LoadBalancerType(new TopicLoadBalancer());
162            return this;
163        }
164    
165        @Override
166        public String getLabel() {
167            CollectionStringBuffer buffer = new CollectionStringBuffer();
168            List<ProcessorType<?>> list = getOutputs();
169            for (ProcessorType<?> processorType : list) {
170                buffer.append(processorType.getLabel());
171            }
172            return buffer.toString();
173        }
174    
175        @Override
176        public String toString() {
177            String result;
178            if (loadBalancerType != null) {
179                result = "LoadBalanceType[" + loadBalancerType + ", ";
180            } else {
181                result =  "LoadBalanceType[" + ref + ", ";
182            }
183            result = result + getOutputs() + "]";
184            return result;
185        }
186    
187    }