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.List;
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlTransient;
023import javax.xml.bind.annotation.XmlType;
024
025import org.apache.camel.AsyncCallback;
026import org.apache.camel.Exchange;
027import org.apache.camel.Processor;
028import org.apache.camel.processor.loadbalancer.LoadBalancer;
029import org.apache.camel.spi.Metadata;
030import org.apache.camel.spi.RouteContext;
031import org.apache.camel.util.IntrospectionSupport;
032import org.apache.camel.util.ObjectHelper;
033
034/**
035 * Balances message processing among a number of nodes
036 */
037@Metadata(label = "eip,routing")
038@XmlType(name = "loadBalancer")
039@XmlAccessorType(XmlAccessType.FIELD)
040public class LoadBalancerDefinition extends IdentifiedType implements LoadBalancer {
041    @XmlTransient
042    private LoadBalancer loadBalancer;
043    @XmlTransient
044    private String loadBalancerTypeName;
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    public static LoadBalancer getLoadBalancer(RouteContext routeContext, LoadBalancerDefinition type, String ref) {
058        if (type == null) {
059            ObjectHelper.notNull(ref, "ref or loadBalancer");
060            LoadBalancer loadBalancer = routeContext.mandatoryLookup(ref, LoadBalancer.class);
061            if (loadBalancer instanceof LoadBalancerDefinition) {
062                type = (LoadBalancerDefinition) loadBalancer;
063            } else {
064                return loadBalancer;
065            }
066        }
067        return type.getLoadBalancer(routeContext);
068    }
069
070
071    /**
072     * Sets a named property on the data format instance using introspection
073     */
074    protected void setProperty(Object bean, String name, Object value) {
075        try {
076            IntrospectionSupport.setProperty(bean, name, value);
077        } catch (Exception e) {
078            throw new IllegalArgumentException("Failed to set property " + name + " on " + bean + ". Reason: " + e, e);
079        }
080    }
081
082    /**
083     * Allows derived classes to customize the load balancer
084     */
085    protected void configureLoadBalancer(LoadBalancer loadBalancer) {
086    }
087
088    public LoadBalancer getLoadBalancer(RouteContext routeContext) {
089        if (loadBalancer == null) {
090            loadBalancer = createLoadBalancer(routeContext);
091            ObjectHelper.notNull(loadBalancer, "loadBalancer");
092            configureLoadBalancer(loadBalancer);
093        }
094        return loadBalancer;
095    }
096
097    /**
098     * Factory method to create the load balancer instance
099     */
100    protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
101        if (loadBalancerTypeName != null) {
102            Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(loadBalancerTypeName);
103            if (type == null) {
104                throw new IllegalArgumentException("Cannot find class: " + loadBalancerTypeName + " in the classpath");
105            }
106            return (LoadBalancer) ObjectHelper.newInstance(type);
107        }
108        return null;
109    }
110
111
112    public void addProcessor(Processor processor) {
113        ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
114        loadBalancer.addProcessor(processor);
115    }
116
117    public List<Processor> getProcessors() {
118        ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
119        return loadBalancer.getProcessors();
120    }
121
122    public void removeProcessor(Processor processor) {
123        ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
124        loadBalancer.removeProcessor(processor);
125    }
126
127    public void process(Exchange exchange) throws Exception {
128        ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
129        loadBalancer.process(exchange);
130    }
131
132    public boolean process(Exchange exchange, final AsyncCallback callback) {
133        ObjectHelper.notNull(loadBalancer, "loadBalancer");
134        return loadBalancer.process(exchange, new AsyncCallback() {
135            public void done(boolean doneSync) {
136                // only handle the async case
137                if (doneSync) {
138                    return;
139                } else {
140                    callback.done(false);
141                }
142            }
143        });
144    }
145
146    @Override
147    public String toString() {
148        if (loadBalancer != null) {
149            return loadBalancer.toString();
150        } else {
151            return loadBalancerTypeName;
152        }
153    }
154}