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.loadbalancer;
018    
019    import java.util.ArrayList;
020    import java.util.List;
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.XmlElement;
026    import javax.xml.bind.annotation.XmlElementWrapper;
027    import javax.xml.bind.annotation.XmlRootElement;
028    
029    import org.apache.camel.model.LoadBalancerDefinition;
030    import org.apache.camel.processor.loadbalancer.LoadBalancer;
031    import org.apache.camel.processor.loadbalancer.WeightedLoadBalancer;
032    import org.apache.camel.processor.loadbalancer.WeightedRandomLoadBalancer;
033    import org.apache.camel.processor.loadbalancer.WeightedRoundRobinLoadBalancer;
034    import org.apache.camel.spi.RouteContext;
035    
036    /**
037     * Represents an XML <weighted/> element
038     */
039    @XmlRootElement(name = "weighted")
040    @XmlAccessorType(XmlAccessType.FIELD)
041    public class WeightedLoadBalancerDefinition extends LoadBalancerDefinition {
042        
043        @XmlAttribute(name = "roundRobin", required = false)
044        private Boolean roundRobin = Boolean.FALSE;
045        
046        @XmlAttribute(name = "distributionRatio", required = true)
047        private String distributionRatio;
048        
049        @XmlAttribute(name = "distributionRatioDelimiter", required = false)
050        private String distributionRatioDelimiter;
051        
052        @Override
053        protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
054            WeightedLoadBalancer loadBalancer = null;
055            List<Integer> distributionRatioList = new ArrayList<Integer>();
056            
057            try {
058                if (distributionRatioDelimiter == null) {
059                    distributionRatioDelimiter = ",";
060                }
061                
062                String[] ratios = distributionRatio.split(distributionRatioDelimiter);
063                for (String ratio : ratios) {
064                    distributionRatioList.add(new Integer(ratio.trim()));
065                }
066                
067                if (!roundRobin) {
068                    loadBalancer = new WeightedRandomLoadBalancer(distributionRatioList);
069                } else {
070                    loadBalancer = new WeightedRoundRobinLoadBalancer(distributionRatioList);
071                }
072            } catch (Exception e) {
073                
074            }
075            return loadBalancer;
076        }
077    
078        public Boolean isRoundRobin() {
079            return roundRobin;
080        }
081    
082        public void setRoundRobin(Boolean roundRobin) {
083            this.roundRobin = roundRobin;
084        }
085    
086        public String getDistributionRatio() {
087            return distributionRatio;
088        }
089    
090        public void setDistributionRatioList(String distributionRatio) {
091            this.distributionRatio = distributionRatio;
092        }
093    
094        @Override
095        public String toString() {
096            if (!roundRobin) { 
097                return "WeightedRandomLoadBalancer[" + distributionRatio + "]";
098            } else {
099                return "WeightedRoundRobinLoadBalancer[" + distributionRatio + "]";
100            }
101        }
102    }