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.management.mbean;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.api.management.ManagedResource;
021import org.apache.camel.api.management.mbean.ManagedWeightedBalancerMBean;
022import org.apache.camel.model.LoadBalanceDefinition;
023import org.apache.camel.model.ProcessorDefinition;
024import org.apache.camel.model.loadbalancer.WeightedLoadBalancerDefinition;
025import org.apache.camel.processor.loadbalancer.WeightedLoadBalancer;
026
027@ManagedResource(description = "Managed Weighted LoadBalancer")
028public class ManagedWeightedLoadBalancer extends ManagedProcessor implements ManagedWeightedBalancerMBean {
029    private final WeightedLoadBalancer processor;
030
031    public ManagedWeightedLoadBalancer(CamelContext context, WeightedLoadBalancer processor, LoadBalanceDefinition definition) {
032        super(context, processor, definition);
033        this.processor = processor;
034    }
035
036    @Override
037    public Integer getSize() {
038        return processor.getProcessors().size();
039    }
040
041    @Override
042    public LoadBalanceDefinition getDefinition() {
043        return (LoadBalanceDefinition) super.getDefinition();
044    }
045
046    @Override
047    public String getRoundRobin() {
048        WeightedLoadBalancerDefinition weighted = (WeightedLoadBalancerDefinition) getDefinition().getLoadBalancerType();
049        if (weighted != null) {
050            return weighted.getRoundRobin();
051        } else {
052            return null;
053        }
054    }
055
056    @Override
057    public String getDistributionRatio() {
058        WeightedLoadBalancerDefinition weighted = (WeightedLoadBalancerDefinition) getDefinition().getLoadBalancerType();
059        if (weighted != null) {
060            return weighted.getDistributionRatio();
061        } else {
062            return null;
063        }
064    }
065
066    @Override
067    public String getDistributionRatioDelimiter() {
068        WeightedLoadBalancerDefinition weighted = (WeightedLoadBalancerDefinition) getDefinition().getLoadBalancerType();
069        if (weighted != null) {
070            return weighted.getDistributionRatioDelimiter();
071        } else {
072            return null;
073        }
074    }
075
076    @Override
077    public String getLastChosenProcessorId() {
078        int idx = processor.getLastChosenProcessorIndex();
079        if (idx != -1) {
080            LoadBalanceDefinition def = getDefinition();
081            ProcessorDefinition<?> output = def.getOutputs().get(idx);
082            if (output != null) {
083                return output.getId();
084            }
085        }
086        return null;
087    }
088
089}