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.Exchange; 027 import org.apache.camel.Processor; 028 import org.apache.camel.model.IdentifiedType; 029 import org.apache.camel.processor.loadbalancer.LoadBalancer; 030 import org.apache.camel.spi.RouteContext; 031 import org.apache.camel.util.IntrospectionSupport; 032 import org.apache.camel.util.ObjectHelper; 033 import static org.apache.camel.util.ObjectHelper.notNull; 034 035 /** 036 * Represents an XML <loadBalance/> element 037 */ 038 @XmlType(name = "loadBalancerType") 039 @XmlAccessorType(XmlAccessType.FIELD) 040 public class LoadBalancerType extends IdentifiedType implements LoadBalancer { 041 042 @XmlTransient 043 private LoadBalancer loadBalancer; 044 @XmlTransient 045 private String loadBalancerTypeName; 046 047 public LoadBalancerType() { 048 } 049 050 public LoadBalancerType(LoadBalancer loadBalancer) { 051 this.loadBalancer = loadBalancer; 052 } 053 054 protected LoadBalancerType(String loadBalancerTypeName) { 055 this.loadBalancerTypeName = loadBalancerTypeName; 056 } 057 058 public static LoadBalancer getLoadBalancer(RouteContext routeContext, LoadBalancerType type, String ref) { 059 if (type == null) { 060 notNull(ref, "ref or LoadBalancerType"); 061 LoadBalancer loadBalancer = routeContext.lookup(ref, LoadBalancer.class); 062 if (loadBalancer instanceof LoadBalancerType) { 063 type = (LoadBalancerType) 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 080 + ". Reason: " + e, e); 081 } 082 } 083 084 /** 085 * Allows derived classes to customize the load balancer 086 */ 087 protected void configureLoadBalancer(LoadBalancer loadBalancer) { 088 } 089 090 public LoadBalancer getLoadBalancer(RouteContext routeContext) { 091 if (loadBalancer == null) { 092 loadBalancer = createLoadBalancer(routeContext); 093 ObjectHelper.notNull(loadBalancer, "loadBalancer"); 094 configureLoadBalancer(loadBalancer); 095 } 096 return loadBalancer; 097 } 098 099 /** 100 * Factory method to create the load balancer instance 101 */ 102 protected LoadBalancer createLoadBalancer(RouteContext routeContext) { 103 if (loadBalancerTypeName != null) { 104 Class type = ObjectHelper.loadClass(loadBalancerTypeName, getClass().getClassLoader()); 105 if (type == null) { 106 throw new IllegalArgumentException("The class " + loadBalancerTypeName + " is not on the classpath! Cannot use the loadBalancer " + this); 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"); 116 loadBalancer.addProcessor(processor); 117 } 118 119 public List<Processor> getProcessors() { 120 ObjectHelper.notNull(loadBalancer, "loadBalancer"); 121 return loadBalancer.getProcessors(); 122 } 123 124 public void removeProcessor(Processor processor) { 125 ObjectHelper.notNull(loadBalancer, "loadBalancer"); 126 loadBalancer.removeProcessor(processor); 127 } 128 129 public void process(Exchange exchange) throws Exception { 130 ObjectHelper.notNull(loadBalancer, "loadBalancer"); 131 loadBalancer.process(exchange); 132 } 133 134 }