package com.usky.common.core.util;

import java.util.Optional;

public class SpringEnvUtils {

    /**
     * 获取当前运行的profile,如果有多个profile,则取第一个
     *
     * @return
     */
    public static String getProfile() {
        return Optional.ofNullable(SpringContextUtils.getApplicationContext())
                .map(c -> c.getEnvironment())
                .map(m -> m.getActiveProfiles())
                .filter(p -> p.length > 0)
                .map(p -> p[0]).orElse(null);
    }

    /**
     * 获取当前运行的所有profile
     *
     * @return
     */
    public static String[] getProfiles() {
        return Optional.ofNullable(SpringContextUtils.getApplicationContext())
                .map(c -> c.getEnvironment())
                .map(m -> m.getActiveProfiles())
                .filter(p -> p.length > 0)
                .orElse(null);
    }

    public static String getProper(String key, String defaultValue) {
        return Optional.ofNullable(SpringContextUtils.getApplicationContext())
                .map(c -> c.getEnvironment())
                .map(m -> m.getProperty(key, defaultValue))
                .orElse(null);
    }
}
