All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.loadbalancer.ribbon.RibbonPropertiesFactory Maven / Gradle / Ivy

package org.zodiac.loadbalancer.ribbon;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanInstantiationException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;
import org.zodiac.loadbalancer.ribbon.constants.RibbonConstants;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import com.netflix.client.IClientConfigAware;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ServerListFilter;

public class RibbonPropertiesFactory {

    private Environment environment;

    private Map classToProperty = new HashMap<>();

    public RibbonPropertiesFactory() {
        classToProperty.put(ILoadBalancer.class, "NFLoadBalancerClassName");
        classToProperty.put(IPing.class, "NFLoadBalancerPingClassName");
        classToProperty.put(IRule.class, "NFLoadBalancerRuleClassName");
        classToProperty.put(ServerList.class, "NIWSServerListClassName");
        classToProperty.put(ServerListFilter.class, "NIWSServerListFilterClassName");
    }

    public boolean isSet(Class clazz, String name) {
        return StrUtil.isNotBlank(getClassName(clazz, name));
    }

    public String getClassName(Class clazz, String name) {
        if (this.classToProperty.containsKey(clazz)) {
            String classNameProperty = this.classToProperty.get(clazz);
            String className = environment
                    .getProperty(name + "." + RibbonConstants.NAMESPACE + "." + classNameProperty);
            return className;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public  C get(Class clazz, IClientConfig config, String name) {
        String className = getClassName(clazz, name);
        if (StrUtil.isNotBlank(className)) {
            try {
                Class toInstantiate = Class.forName(className);
                return (C) instantiateWithConfig(toInstantiate, config);
            }
            catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Unknown class to load " + className
                        + " for class " + clazz + " named " + name);
            }
        }
        return null;
    }

    static  C instantiateWithConfig(Class clazz, IClientConfig config) {
        return instantiateWithConfig(null, clazz, config);
    }

    static  C instantiateWithConfig(AnnotationConfigApplicationContext context,
            Class clazz, IClientConfig config) {
        C result = null;

        try {
            Constructor constructor = clazz.getConstructor(IClientConfig.class);
            result = constructor.newInstance(config);
        } catch (Throwable e) {
            // Ignored
        }

        if (result == null) {
            try {
                result = clazz.newInstance();
            } catch (InstantiationException ex) {
                throw new BeanInstantiationException(clazz, "Is it an abstract class?", ex);
            } catch (IllegalAccessException ex) {
                throw new BeanInstantiationException(clazz, "Is the constructor accessible?", ex);
            }

            if (result instanceof IClientConfigAware) {
                ((IClientConfigAware) result).initWithNiwsConfig(config);
            }

            if (context != null) {
                context.getAutowireCapableBeanFactory().autowireBean(result);
            }
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy