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.reifier.loadbalancer; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.function.Function; 022 023import org.apache.camel.model.LoadBalancerDefinition; 024import org.apache.camel.model.loadbalancer.CustomLoadBalancerDefinition; 025import org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition; 026import org.apache.camel.model.loadbalancer.RandomLoadBalancerDefinition; 027import org.apache.camel.model.loadbalancer.RoundRobinLoadBalancerDefinition; 028import org.apache.camel.model.loadbalancer.StickyLoadBalancerDefinition; 029import org.apache.camel.model.loadbalancer.TopicLoadBalancerDefinition; 030import org.apache.camel.model.loadbalancer.WeightedLoadBalancerDefinition; 031import org.apache.camel.processor.loadbalancer.LoadBalancer; 032import org.apache.camel.spi.RouteContext; 033import org.apache.camel.util.StringHelper; 034 035public class LoadBalancerReifier<T extends LoadBalancerDefinition> { 036 037 private static final Map<Class<?>, Function<LoadBalancerDefinition, LoadBalancerReifier<? extends LoadBalancerDefinition>>> LOAD_BALANCERS; 038 static { 039 Map<Class<?>, Function<LoadBalancerDefinition, LoadBalancerReifier<? extends LoadBalancerDefinition>>> map = new HashMap<>(); 040 map.put(LoadBalancerDefinition.class, LoadBalancerReifier::new); 041 map.put(CustomLoadBalancerDefinition.class, CustomLoadBalancerReifier::new); 042 map.put(FailoverLoadBalancerDefinition.class, FailoverLoadBalancerReifier::new); 043 map.put(RandomLoadBalancerDefinition.class, RandomLoadBalancerReifier::new); 044 map.put(RoundRobinLoadBalancerDefinition.class, RoundRobinLoadBalancerReifier::new); 045 map.put(StickyLoadBalancerDefinition.class, StickyLoadBalancerReifier::new); 046 map.put(TopicLoadBalancerDefinition.class, TopicLoadBalancerReifier::new); 047 map.put(WeightedLoadBalancerDefinition.class, WeightedLoadBalancerReifier::new); 048 LOAD_BALANCERS = map; 049 } 050 051 protected final T definition; 052 053 public LoadBalancerReifier(T definition) { 054 this.definition = definition; 055 } 056 057 public static LoadBalancerReifier<? extends LoadBalancerDefinition> reifier(LoadBalancerDefinition definition) { 058 Function<LoadBalancerDefinition, LoadBalancerReifier<? extends LoadBalancerDefinition>> reifier = LOAD_BALANCERS.get(definition.getClass()); 059 if (reifier != null) { 060 return reifier.apply(definition); 061 } 062 throw new IllegalStateException("Unsupported definition: " + definition); 063 } 064 065 /** 066 * Factory method to create the load balancer from the loadBalancerTypeName 067 */ 068 public LoadBalancer createLoadBalancer(RouteContext routeContext) { 069 String loadBalancerTypeName = definition.getLoadBalancerTypeName(); 070 StringHelper.notEmpty(loadBalancerTypeName, "loadBalancerTypeName", this); 071 072 LoadBalancer answer = null; 073 if (loadBalancerTypeName != null) { 074 Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(loadBalancerTypeName, LoadBalancer.class); 075 if (type == null) { 076 throw new IllegalArgumentException("Cannot find class: " + loadBalancerTypeName + " in the classpath"); 077 } 078 answer = (LoadBalancer)routeContext.getCamelContext().getInjector().newInstance(type, false); 079 definition.configureLoadBalancer(answer); 080 } 081 082 return answer; 083 } 084 085}