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     */
017    package org.apache.camel.model;
018    
019    import java.util.List;
020    import javax.xml.bind.annotation.XmlAccessType;
021    import javax.xml.bind.annotation.XmlAccessorType;
022    import javax.xml.bind.annotation.XmlTransient;
023    import javax.xml.bind.annotation.XmlType;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Processor;
027    import org.apache.camel.processor.loadbalancer.LoadBalancer;
028    import org.apache.camel.spi.RouteContext;
029    import org.apache.camel.util.IntrospectionSupport;
030    import org.apache.camel.util.ObjectHelper;
031    
032    /**
033     * Represents an XML <loadBalancer/> element
034     */
035    @XmlType(name = "loadBalancer")
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public class LoadBalancerDefinition extends IdentifiedType implements LoadBalancer {
038    
039        @XmlTransient
040        private LoadBalancer loadBalancer;
041        @XmlTransient
042        private String loadBalancerTypeName;
043    
044        public LoadBalancerDefinition() {
045        }
046    
047        public LoadBalancerDefinition(LoadBalancer loadBalancer) {
048            this.loadBalancer = loadBalancer;
049        }
050    
051        protected LoadBalancerDefinition(String loadBalancerTypeName) {
052            this.loadBalancerTypeName = loadBalancerTypeName;
053        }
054    
055        public static LoadBalancer getLoadBalancer(RouteContext routeContext, LoadBalancerDefinition type, String ref) {
056            if (type == null) {
057                ObjectHelper.notNull(ref, "ref or loadBalancer");
058                LoadBalancer loadBalancer = routeContext.lookup(ref, LoadBalancer.class);
059                if (loadBalancer instanceof LoadBalancerDefinition) {
060                    type = (LoadBalancerDefinition) loadBalancer;
061                } else {
062                    return loadBalancer;
063                }
064            }
065            return type.getLoadBalancer(routeContext);
066        }
067    
068    
069        /**
070         * Sets a named property on the data format instance using introspection
071         */
072        protected void setProperty(Object bean, String name, Object value) {
073            try {
074                IntrospectionSupport.setProperty(bean, name, value);
075            } catch (Exception e) {
076                throw new IllegalArgumentException("Failed to set property " + name + " on " + bean + ". Reason: " + e, e);
077            }
078        }
079    
080        /**
081         * Allows derived classes to customize the load balancer
082         */
083        protected void configureLoadBalancer(LoadBalancer loadBalancer) {
084        }
085    
086        public LoadBalancer getLoadBalancer(RouteContext routeContext) {
087            if (loadBalancer == null) {
088                loadBalancer = createLoadBalancer(routeContext);
089                ObjectHelper.notNull(loadBalancer, "loadBalancer");
090                configureLoadBalancer(loadBalancer);
091            }
092            return loadBalancer;
093        }
094    
095        /**
096         * Factory method to create the load balancer instance
097         */
098        @SuppressWarnings("unchecked")
099        protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
100            if (loadBalancerTypeName != null) {
101                Class type = routeContext.getCamelContext().getClassResolver().resolveClass(loadBalancerTypeName);
102                if (type == null) {
103                    throw new IllegalArgumentException("Cannot find class: " + loadBalancerTypeName + " in the classpath");
104                }
105                return (LoadBalancer) ObjectHelper.newInstance(type);
106            }
107            return null;
108        }
109    
110    
111        public void addProcessor(Processor processor) {
112            ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
113            loadBalancer.addProcessor(processor);
114        }
115    
116        public List<Processor> getProcessors() {
117            ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
118            return loadBalancer.getProcessors();
119        }
120    
121        public void removeProcessor(Processor processor) {
122            ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
123            loadBalancer.removeProcessor(processor);
124        }
125    
126        public void process(Exchange exchange) throws Exception {
127            ObjectHelper.notNull(loadBalancer, "loadBalancer", this);
128            loadBalancer.process(exchange);
129        }
130    
131    }