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;
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlAnyAttribute;
023import javax.xml.bind.annotation.XmlTransient;
024import javax.xml.bind.annotation.XmlType;
025import javax.xml.namespace.QName;
026
027import org.apache.camel.processor.loadbalancer.LoadBalancer;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.RouteContext;
030import org.apache.camel.util.IntrospectionSupport;
031import org.apache.camel.util.ObjectHelper;
032
033/**
034 * Balances message processing among a number of nodes
035 */
036@Metadata(label = "eip,routing")
037@XmlType(name = "loadBalancer")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class LoadBalancerDefinition extends IdentifiedType implements OtherAttributesAware {
040    @XmlTransient
041    private LoadBalancer loadBalancer;
042    @XmlTransient
043    private String loadBalancerTypeName;
044    // use xs:any to support optional property placeholders
045    @XmlAnyAttribute
046    private Map<QName, Object> otherAttributes;
047
048    public LoadBalancerDefinition() {
049    }
050
051    public LoadBalancerDefinition(LoadBalancer loadBalancer) {
052        this.loadBalancer = loadBalancer;
053    }
054
055    protected LoadBalancerDefinition(String loadBalancerTypeName) {
056        this.loadBalancerTypeName = loadBalancerTypeName;
057    }
058
059    /**
060     * Sets a named property on the data format instance using introspection
061     */
062    protected void setProperty(Object bean, String name, Object value) {
063        try {
064            IntrospectionSupport.setProperty(bean, name, value);
065        } catch (Exception e) {
066            throw new IllegalArgumentException("Failed to set property " + name + " on " + bean + ". Reason: " + e, e);
067        }
068    }
069
070    /**
071     * Maximum number of outputs, as some load balancers only support 1 processor
072     */
073    protected int getMaximumNumberOfOutputs() {
074        return Integer.MAX_VALUE;
075    }
076
077    /**
078     * Allows derived classes to customize the load balancer
079     */
080    protected void configureLoadBalancer(LoadBalancer loadBalancer) {
081    }
082
083    public LoadBalancer getLoadBalancer(RouteContext routeContext) {
084        return loadBalancer;
085    }
086
087    public void setLoadBalancer(LoadBalancer loadBalancer) {
088        this.loadBalancer = loadBalancer;
089    }
090
091    @Override
092    public Map<QName, Object> getOtherAttributes() {
093        return otherAttributes;
094    }
095
096    @Override
097    public void setOtherAttributes(Map<QName, Object> otherAttributes) {
098        this.otherAttributes = otherAttributes;
099    }
100
101    /**
102     * Factory method to create the load balancer from the loadBalancerTypeName
103     */
104    protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
105        ObjectHelper.notEmpty(loadBalancerTypeName, "loadBalancerTypeName", this);
106
107        LoadBalancer answer = null;
108        if (loadBalancerTypeName != null) {
109            Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(loadBalancerTypeName, LoadBalancer.class);
110            if (type == null) {
111                throw new IllegalArgumentException("Cannot find class: " + loadBalancerTypeName + " in the classpath");
112            }
113            answer = (LoadBalancer) routeContext.getCamelContext().getInjector().newInstance(type);
114            configureLoadBalancer(answer);
115        }
116
117        return answer;
118    }
119
120    @Override
121    public String toString() {
122        if (loadBalancer != null) {
123            return loadBalancer.toString();
124        } else {
125            return loadBalancerTypeName;
126        }
127    }
128}