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