java-获取配置工具

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

@SuppressWarnings("unused")
public class ReadPropertiesUtil {
    static final Logger log = LoggerFactory.getLogger(ManifestUtil.class);

    private ReadPropertiesUtil() {
        throw new IllegalStateException("Utility class");
    }

    private static Properties loadProperties(String filePath) {
        Properties properties = null;
        try {
            File file = new File(filePath);
            if (!file.exists() || !file.isFile()) {
                return null;
            } else {
                properties = new Properties();
                try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()));
                     BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
                    properties.load(bf);
                }
            }
        } catch (Exception e) {
            log.error("ReadPropertiesUtil.getProperties.err:{}", e.getMessage());
        }
        return properties;
    }

    public static Properties getProperties(String filePath) {
        Properties properties = loadProperties(filePath);
        if (properties == null) {
            return new Properties();
        } else {
            return properties;
        }
    }

    public static List<Properties> getAllProperties(List<Properties> useList, String... filePaths) {
        List<Properties> properties = useList == null ? new LinkedList<>() : useList;
        if (filePaths != null) {
            Properties one;
            for (String filePath : filePaths) {
                one = loadProperties(filePath);
                if (one != null) {
                    properties.add(one);
                }
            }
        }
        return properties;
    }

    public static List<Properties> getAllProperties(String... filePaths) {
        return getAllProperties(new LinkedList<>(), filePaths);
    }

}

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

@SuppressWarnings("unused")
public class FincmpConfig {
    static final Logger log = LoggerFactory.getLogger(ManifestUtil.class);

    static final String FLUSH_STATE_FILE_PATH = "config/flush_properties_state";
    static final String[] LOAD_PROPERTIES_PATHS = new String[]{
            "config/fincmp.properties"
    };
    static final List<Properties> ps = ReadPropertiesUtil.getAllProperties(
            LOAD_PROPERTIES_PATHS
    );
    static final AtomicBoolean lock = new AtomicBoolean(false);

    private FincmpConfig() {
        throw new IllegalStateException("Utility class");
    }

    private static void lock() {
        while (lock.get()) {
            Thread nowThread = Thread.currentThread();
            log.info(String.format("Thread[%s][%s]flushProperties.lock...", nowThread.getId(), nowThread.getName()));
        }
    }

    private static void flushProperties() {
        if (new File(FLUSH_STATE_FILE_PATH).exists()) {
            try {
                lock.set(true);
                ps.clear();
                ReadPropertiesUtil.getAllProperties(ps, LOAD_PROPERTIES_PATHS);
            } catch (Exception e) {
                lock.set(false);
            } finally {
                lock.set(false);
            }
        }
    }

    private static void lockFlush() {
        lock();
        flushProperties();
    }

    public static String getValue(String key) {
        lockFlush();
        for (Properties p : ps) {
            String v = p.getProperty(key);
            if (v != null) {
                return v;
            }
        }
        return "";
    }

    public static boolean getBool(String key) {
        lockFlush();
        return Boolean.parseBoolean(getValue(key));
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值