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