package cn.com.qmth.sdk.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 类注释 * * @author WANGWEI * @date 2018年10月8日 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved. */ public class PropertiesUtil { private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class); private static final Properties PROPS = new Properties(); static { init(); } /** * 构造函数 * */ private PropertiesUtil() { } /** * 初始化方法 * * @author WANGWEI */ public static void init() { configureAndWatch(PathUtil.getResoucePath("qmth.properties")); } /** * 加载配置文件并观察配置文件 * * @author WANGWEI * @param path */ public static synchronized void configureAndWatch(String path) { if (StringUtils.isBlank(path)) { return; } try { File file = new File(path); loadFromFile(file, PROPS); } catch (Exception e) { LOG.error("Fail to load and watch file [" + path + "].", e); } } /** * @param path * @param props */ public static void loadFromDir(String path, Properties props) { File dir = new File(path + ""); if ((!dir.exists()) || (!dir.isDirectory())) { LOG.error("directory [" + path + "] is illegal."); return; } LOG.info("Loading all Properties files from path [" + path + "]."); File[] files = dir.listFiles(); if (null == files || 0 == files.length) { return; } for (File file : files) { if (file.isDirectory()) { continue; } if (file.getName().toLowerCase().endsWith(".properties")) { loadFromFile(file, props); } } } /** * @param resourceName * @param props */ public static void loadFromResource(String resourceName, Properties props) { if (null != PropertiesUtil.class.getClassLoader()) { InputStream in = PropertiesUtil.class.getClassLoader() .getResourceAsStream(resourceName); loadFromStream(in, props); } else { throw new RuntimeException("fail to get class loader"); } } /** * @param file * @param props * @throws IOException */ public static void loadFromFile(File file, Properties props) { if (null == file) { LOG.error("file is null."); return; } if (!file.isFile()) { LOG.error("file is not a normal file."); return; } String path = PathUtil.getCanonicalPath(file); LOG.info("Loading properties from file [" + path + "]"); try { loadFromStream(new FileInputStream(file), props); } catch (Exception e) { throw new RuntimeException(e); } } /** * @param is * @param props */ public static void loadFromStream(InputStream is, Properties props) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); props.load(reader); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(is); } } /** * @param key * @return */ public static String getString(String key) { String value = PROPS.getProperty(key); if (StringUtils.isNotBlank(value)) { return value.trim(); } else { if (LOG.isDebugEnabled()) { LOG.debug("No property value, key = " + key); } return null; } } /** * @param key * @param defaultValue * @return */ public static String getString(String key, String defaultValue) { String value = getString(key); if (null != value) { return value; } return defaultValue; } /** * @param key * @param defaultValue * @return */ public static int getInt(String key, int defaultValue) { String value = getString(key); if (null != value) { try { return Integer.parseInt(value); } catch (NumberFormatException e) { PROPS.setProperty(key, String.valueOf(defaultValue)); return defaultValue; } } return defaultValue; } /** * @param key * @param defaultValue * @return */ public static long getLong(String key, long defaultValue) { String value = getString(key); if (null != value) { try { return Long.parseLong(value); } catch (NumberFormatException e) { return defaultValue; } } return defaultValue; } /** * 获取boolean * * @author WANGWEI * @param key * @param defaultVale * @return */ public static boolean getBoolean(String key, boolean defaultVale) { String value = getString(key); if (null == value) { return defaultVale; } if (value.equals("true")) { return true; } else if (value.equals("false")) { return false; } else { return defaultVale; } } }