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

org.zodiac.eureka.client.util.EurekaClientHolder Maven / Gradle / Ivy

package org.zodiac.eureka.client.util;

import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.core.application.AppContext;
import org.zodiac.core.application.AppEnvType;
import org.zodiac.core.application.AppInstance;
import org.zodiac.core.application.ApplicationInfo;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

public abstract class EurekaClientHolder {

    private EurekaClientHolder() {
        super();
    }

    private static final Logger LOG = SmartSlf4jLoggerFactory.getLogger(EurekaClientHolder.class);

    private static volatile AtomicBoolean initialized = new AtomicBoolean(false);

    private static final Map SERVER_MAP;

    private static final Function DEFAULT_SERVER_MAPPER = (value) -> "http://localhost:8761";

    static {
        SERVER_MAP = AppEnvType.envTypes().stream().collect(Collectors.toMap(envType -> envType, DEFAULT_SERVER_MAPPER));
    }

    public static void initialize(Supplier> serversSupplier) {
        if (initialized.compareAndSet(false, true)) {
            try {
                SERVER_MAP.putAll(serversSupplier.get());
            } catch (Throwable t) {
                initialized.compareAndSet(true, false);
                LOG.error("{}", ExceptionUtil.stackTrace(t));
            }
        }
    }

    public static String getServerByModule(AppContext context) {
        if (null == context) {
            return null;
        }
        return getServerByModule(context.getAppInstance());
    }

    public static String getServerByModule(AppInstance instance) {
        if (null == instance) {
            return null;
        }
        return getServer(instance.getModule());
    }

    public static String getServerByModule(ApplicationInfo applicationInfo) {
        if (null == applicationInfo) {
            return null;
        }
        return getServer(applicationInfo.getModule());
    }

    /*==========================================================================================*/
    public static String getServerByRegion(AppContext context) {
        if (null == context) {
            return null;
        }
        return getServerByRegion(context.getAppInstance());
    }

    public static String getServerByRegion(AppInstance instance) {
        if (null == instance) {
            return null;
        }
        return getServer(instance.getRegion());
    }

    public static String getServerByRegion(ApplicationInfo applicationInfo) {
        if (null == applicationInfo) {
            return null;
        }
        return getServer(applicationInfo.getRegion());
    }

    /*==========================================================================================*/
    public static String getServerByZone(AppContext context) {
        if (null == context) {
            return null;
        }
        return getServerByZone(context.getAppInstance());
    }

    public static String getServerByZone(AppInstance instance) {
        if (null == instance) {
            return null;
        }
        return getServer(instance.getZone());
    }

    public static String getServerByZone(ApplicationInfo applicationInfo) {
        if (null == applicationInfo) {
            return null;
        }
        return getServer(applicationInfo.getZone());
    }

    /*==========================================================================================*/
    public static String getServerByCluster(AppContext context) {
        if (null == context) {
            return null;
        }
        return getServerByCluster(context.getAppInstance());
    }

    public static String getServerByCluster(AppInstance instance) {
        if (null == instance) {
            return null;
        }
        return getServer(instance.getCluster());
    }

    public static String getServerByCluster(ApplicationInfo applicationInfo) {
        if (null == applicationInfo) {
            return null;
        }
        return getServer(applicationInfo.getCluster());
    }

    /*==========================================================================================*/
    public static String getServerByGroup(AppContext context) {
        if (null == context) {
            return null;
        }
        return getServerByGroup(context.getAppInstance());
    }

    public static String getServerByGroup(AppInstance instance) {
        if (null == instance) {
            return null;
        }
        return getServer(instance.getAppGroup());
    }

    public static String getServerByGroup(ApplicationInfo applicationInfo) {
        if (null == applicationInfo) {
            return null;
        }
        return getServer(applicationInfo.getGroup());
    }

    /*==========================================================================================*/
    public static String getServerByEnvType(AppEnvType envType) {
        if (null == envType) {
            return null;
        }
        return getServer(envType.envType());
    }

    public static String getServerByEnvType(AppContext context) {
        if (null == context) {
            return null;
        }
        return getServerByEnvType(context.getAppInstance());
    }

    public static String getServerByEnvType(AppInstance instance) {
        if (null == instance) {
            return null;
        }
        return getServer(instance.getEnvType());
    }

    public static String getServerByEnvType(ApplicationInfo applicationInfo) {
        if (null == applicationInfo) {
            return null;
        }
        return getServer(applicationInfo.getEnvType());
    }

    /*==========================================================================================*/
    public static String getServer(String name) {
        String _name = StrUtil.trimToNull(name);
        if (null == _name) {
            return null;
        }
        return SERVER_MAP.get(_name.toLowerCase());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy