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