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.processor.loadbalancer;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    public abstract class WeightedLoadBalancer extends QueueLoadBalancer {
023        private List<Integer> distributionRatioList = new ArrayList<Integer>();
024        private List<DistributionRatio> runtimeRatios = new ArrayList<DistributionRatio>();
025        
026        public WeightedLoadBalancer(List<Integer> distributionRatios) {
027            deepCloneDistributionRatios(distributionRatios);
028            loadRuntimeRatios(distributionRatios);
029        }
030        
031        protected void deepCloneDistributionRatios(List<Integer> distributionRatios) {
032            for (Integer value : distributionRatios) {
033                this.distributionRatioList.add(value);
034            }
035        }
036    
037        @Override
038        protected void doStart() throws Exception {
039            super.doStart();
040            if (getProcessors().size() != getDistributionRatioList().size()) {
041                throw new IllegalArgumentException("Loadbalacing with " + getProcessors().size()
042                    + " should match number of distributions " + getDistributionRatioList().size());
043            }
044        }
045    
046        protected void loadRuntimeRatios(List<Integer> distributionRatios) {
047            int position = 0;
048            
049            for (Integer value : distributionRatios) {
050                runtimeRatios.add(new DistributionRatio(position++, value.intValue()));
051            }
052        }
053        
054        protected boolean isRuntimeRatiosZeroed() {
055            boolean cleared = true;
056            
057            for (DistributionRatio runtimeRatio : runtimeRatios) {
058                if (runtimeRatio.getRuntimeWeight() > 0) {
059                    cleared = false;
060                }
061            }        
062            return cleared; 
063        }
064        
065        protected void resetRuntimeRatios() {
066            for (DistributionRatio runtimeRatio : runtimeRatios) {
067                runtimeRatio.setRuntimeWeight(runtimeRatio.getDistributionWeight());
068            }
069        }
070    
071        public List<Integer> getDistributionRatioList() {
072            return distributionRatioList;
073        }
074    
075        public void setDistributionRatioList(List<Integer> distributionRatioList) {
076            this.distributionRatioList = distributionRatioList;
077        }
078    
079        public List<DistributionRatio> getRuntimeRatios() {
080            return runtimeRatios;
081        }
082    
083        public void setRuntimeRatios(ArrayList<DistributionRatio> runtimeRatios) {
084            this.runtimeRatios = runtimeRatios;
085        }    
086        
087    }