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;
018
019import java.util.Map;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAnyAttribute;
024import javax.xml.bind.annotation.XmlTransient;
025import javax.xml.bind.annotation.XmlType;
026import javax.xml.namespace.QName;
027
028import org.apache.camel.processor.loadbalancer.LoadBalancer;
029import org.apache.camel.spi.Metadata;
030
031/**
032 * Balances message processing among a number of nodes
033 */
034@Metadata(label = "eip,routing")
035@XmlType(name = "loadBalancer")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class LoadBalancerDefinition extends IdentifiedType implements OtherAttributesAware {
038    @XmlTransient
039    private LoadBalancer loadBalancer;
040    @XmlTransient
041    private String loadBalancerTypeName;
042    // use xs:any to support optional property placeholders
043    @XmlAnyAttribute
044    private Map<QName, Object> otherAttributes;
045
046    public LoadBalancerDefinition() {
047    }
048
049    public LoadBalancerDefinition(LoadBalancer loadBalancer) {
050        this.loadBalancer = loadBalancer;
051    }
052
053    protected LoadBalancerDefinition(String loadBalancerTypeName) {
054        this.loadBalancerTypeName = loadBalancerTypeName;
055    }
056
057    /**
058     * Maximum number of outputs, as some load balancers only support 1
059     * processor
060     */
061    public int getMaximumNumberOfOutputs() {
062        return Integer.MAX_VALUE;
063    }
064
065    /**
066     * Allows derived classes to customize the load balancer
067     */
068    public void configureLoadBalancer(LoadBalancer loadBalancer) {
069    }
070
071    public LoadBalancer getLoadBalancer() {
072        return loadBalancer;
073    }
074
075    public void setLoadBalancer(LoadBalancer loadBalancer) {
076        this.loadBalancer = loadBalancer;
077    }
078
079    public String getLoadBalancerTypeName() {
080        return loadBalancerTypeName;
081    }
082
083    @Override
084    public Map<QName, Object> getOtherAttributes() {
085        return otherAttributes;
086    }
087
088    @Override
089    public void setOtherAttributes(Map<QName, Object> otherAttributes) {
090        this.otherAttributes = otherAttributes;
091    }
092
093    @Override
094    public String toString() {
095        if (loadBalancer != null) {
096            return loadBalancer.toString();
097        } else {
098            return loadBalancerTypeName;
099        }
100    }
101}