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

com.loocme.sys.util.FilePropUtil Maven / Gradle / Ivy

There is a newer version: 7.1.11
Show newest version
package com.loocme.sys.util;

import com.loocme.sys.util.ThreadUtil.ExecutorService;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.*;

/**
 * @author liuchi
 */
@Slf4j
public class FilePropUtil
{

    private static long LAST_UPDATE_TIME = 0;
    private static Map PROP_MAP = new HashMap(16);

    private static ExecutorService ESERVICE = null;
    private static String FILE_PATH = "";
    private static IWatchValue WATCH_VALUE_HANDLER = null;

    public static String getFilePath()
    {
        return FILE_PATH;
    }

    public static void start(final String filePath)
    {
        ESERVICE = ThreadUtil.newSingleThreadExecutor();
        FILE_PATH = filePath;

        reload(filePath);

        ESERVICE.execute(new Runnable()
        {

            @Override
            public void run()
            {
                while (true)
                {
                    try
                    {
                        Thread.sleep(10000);
                    }
                    catch (InterruptedException e)
                    {
                        log.error("", e);
                    }

                    reload(filePath);
                }
            }
        });
    }

    public static void start(final String filePath, IWatchValue handler)
    {
        WATCH_VALUE_HANDLER = handler;
        start(filePath);
    }

    public static Map loadAllProp(Properties props)
    {
        Iterator propsKeyIt = props.keySet().iterator();

        Map retMap = new HashMap<>(16);
        while (propsKeyIt.hasNext())
        {
            Object key = propsKeyIt.next();
            if (null == key)
            {
                continue;
            }

            String keyStr = key.toString();
            if (StringUtil.isNull(keyStr))
            {
                continue;
            }

            retMap.put(keyStr, props.getProperty(keyStr));
        }
        return retMap;
    }

    public static void reload(String filePath)
    {
        File propFile = new File(filePath);
        if (!propFile.exists())
        {
            return;
        }

        long lastUpdTime = propFile.lastModified();
        if (lastUpdTime <= LAST_UPDATE_TIME)
        {
            return;
        }

        LAST_UPDATE_TIME = lastUpdTime;

        Properties props = PropUtil.getPropFile(propFile);

        Map value = loadAllProp(props);
        if (MapUtil.isNull(value))
        {
            return;
        }

        Map orgValue = PROP_MAP;
        PROP_MAP = value;
        log.info("[file_mem_prop_changed]:" + value.toString());
        if (null != WATCH_VALUE_HANDLER)
        {
            value.forEach((k, v) -> {
                String newValue = null == v?"":v.toString();
                Object oldValueObj = orgValue.remove(k);
                String oldValue = null == oldValueObj?"":oldValueObj.toString();
                if (newValue.equals(oldValue))
                    return;
                WATCH_VALUE_HANDLER.changed(k, newValue, oldValue);
            });
            orgValue.forEach((k, v) -> {
                String oldValue = null == v?"":v.toString();
                WATCH_VALUE_HANDLER.changed(k, null, oldValue);
            });
        }
    }

    public static int getInt(String key)
    {
        return getInt(key, 0);
    }

    public static int getInt(String key, int defaultValue)
    {
        if (PROP_MAP.containsKey(key))
        {
            return MapUtil.getInteger(PROP_MAP, key);
        }
        else
        {
            return defaultValue;
        }
    }

    public static long getLong(String key)
    {
        return getLong(key, 0);
    }

    public static long getLong(String key, long defaultValue)
    {
        if (PROP_MAP.containsKey(key))
        {
            return MapUtil.getLong(PROP_MAP, key);
        }
        else
        {
            return defaultValue;
        }
    }

    public static String getString(String key)
    {
        return getString(key, "");
    }

    public static String getString(String key, String defaultValue)
    {
        return PROP_MAP.containsKey(key) ? MapUtil.getString(PROP_MAP, key) : defaultValue;
    }

    public static boolean getBoolean(String key)
    {
        return MapUtil.getBoolean(PROP_MAP, key);
    }

    public static boolean getBoolean(String key, boolean defaultValue)
    {
        return PROP_MAP.containsKey(key) ? MapUtil.getBoolean(PROP_MAP, key) : defaultValue;
    }

    public static double getDouble(String key)
    {
        return getDouble(key, 0.0);
    }

    public static double getDouble(String key, double defaultValue)
    {
        return PROP_MAP.containsKey(key) ? MapUtil.getDouble(PROP_MAP, key) : defaultValue;
    }

    public static float getFloat(String key)
    {
        return getFloat(key, 0);
    }

    public static float getFloat(String key, float defaultValue)
    {
        return PROP_MAP.containsKey(key) ? MapUtil.getFloat(PROP_MAP, key) : defaultValue;
    }

    public interface IWatchValue
    {
        public void changed(String key, String newVlue, String oldValue);
    }
}