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