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     * Allows derived classes to customize the load balancer
066     */
067    protected void configureLoadBalancer(LoadBalancer loadBalancer) {
068    }
069
070    public LoadBalancer getLoadBalancer(RouteContext routeContext) {
071        return loadBalancer;
072    }
073
074    public void setLoadBalancer(LoadBalancer loadBalancer) {
075        this.loadBalancer = loadBalancer;
076    }
077
078    /**
079     * Factory method to create the load balancer from the loadBalancerTypeName
080     */
081    protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
082        ObjectHelper.notEmpty(loadBalancerTypeName, "loadBalancerTypeName", this);
083
084        LoadBalancer answer = null;
085        if (loadBalancerTypeName != null) {
086            Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(loadBalancerTypeName, LoadBalancer.class);
087            if (type == null) {
088                throw new IllegalArgumentException("Cannot find class: " + loadBalancerTypeName + " in the classpath");
089            }
090            answer = (LoadBalancer) routeContext.getCamelContext().getInjector().newInstance(type);
091            configureLoadBalancer(answer);
092        }
093
094        return answer;
095    }
096
097    @Override
098    public String toString() {
099        if (loadBalancer != null) {
100            return loadBalancer.toString();
101        } else {
102            return loadBalancerTypeName;
103        }
104    }
105}