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.Map; 020 021import javax.xml.bind.annotation.XmlAccessType; 022import javax.xml.bind.annotation.XmlAccessorType; 023import javax.xml.bind.annotation.XmlAnyAttribute; 024import javax.xml.bind.annotation.XmlTransient; 025import javax.xml.bind.annotation.XmlType; 026import javax.xml.namespace.QName; 027 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.StringHelper; 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 OtherAttributesAware { 041 @XmlTransient 042 private LoadBalancer loadBalancer; 043 @XmlTransient 044 private String loadBalancerTypeName; 045 // use xs:any to support optional property placeholders 046 @XmlAnyAttribute 047 private Map<QName, Object> otherAttributes; 048 049 public LoadBalancerDefinition() { 050 } 051 052 public LoadBalancerDefinition(LoadBalancer loadBalancer) { 053 this.loadBalancer = loadBalancer; 054 } 055 056 protected LoadBalancerDefinition(String loadBalancerTypeName) { 057 this.loadBalancerTypeName = loadBalancerTypeName; 058 } 059 060 /** 061 * Sets a named property on the data format instance using introspection 062 */ 063 protected void setProperty(Object bean, String name, Object value) { 064 try { 065 IntrospectionSupport.setProperty(bean, name, value); 066 } catch (Exception e) { 067 throw new IllegalArgumentException("Failed to set property " + name + " on " + bean + ". Reason: " + e, e); 068 } 069 } 070 071 /** 072 * Maximum number of outputs, as some load balancers only support 1 processor 073 */ 074 protected int getMaximumNumberOfOutputs() { 075 return Integer.MAX_VALUE; 076 } 077 078 /** 079 * Allows derived classes to customize the load balancer 080 */ 081 protected void configureLoadBalancer(LoadBalancer loadBalancer) { 082 } 083 084 public LoadBalancer getLoadBalancer(RouteContext routeContext) { 085 return loadBalancer; 086 } 087 088 public void setLoadBalancer(LoadBalancer loadBalancer) { 089 this.loadBalancer = loadBalancer; 090 } 091 092 @Override 093 public Map<QName, Object> getOtherAttributes() { 094 return otherAttributes; 095 } 096 097 @Override 098 public void setOtherAttributes(Map<QName, Object> otherAttributes) { 099 this.otherAttributes = otherAttributes; 100 } 101 102 /** 103 * Factory method to create the load balancer from the loadBalancerTypeName 104 */ 105 protected LoadBalancer createLoadBalancer(RouteContext routeContext) { 106 StringHelper.notEmpty(loadBalancerTypeName, "loadBalancerTypeName", this); 107 108 LoadBalancer answer = null; 109 if (loadBalancerTypeName != null) { 110 Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(loadBalancerTypeName, LoadBalancer.class); 111 if (type == null) { 112 throw new IllegalArgumentException("Cannot find class: " + loadBalancerTypeName + " in the classpath"); 113 } 114 answer = (LoadBalancer) routeContext.getCamelContext().getInjector().newInstance(type); 115 configureLoadBalancer(answer); 116 } 117 118 return answer; 119 } 120 121 @Override 122 public String toString() { 123 if (loadBalancer != null) { 124 return loadBalancer.toString(); 125 } else { 126 return loadBalancerTypeName; 127 } 128 } 129}