|
@@ -1,245 +1,245 @@
|
|
|
-package cn.com.qmth.examcloud.commons.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.Map.Entry;
|
|
|
-import java.util.Properties;
|
|
|
-import java.util.Set;
|
|
|
-
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
-
|
|
|
-import com.google.common.collect.Sets;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
-
|
|
|
-/**
|
|
|
- * 类注释
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @date 2018年10月8日
|
|
|
- * @Copyright (c) 2018-2020 http://www.qmth.com.cn/ All Rights Reserved.
|
|
|
- */
|
|
|
-public class PropertiesUtil {
|
|
|
-
|
|
|
- private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(PropertiesUtil.class);
|
|
|
-
|
|
|
- private static final Properties PROPS = new Properties();
|
|
|
-
|
|
|
- private static final Set<String> PROP_FILES = Sets.newConcurrentHashSet();
|
|
|
-
|
|
|
- /**
|
|
|
- * 构造函数
|
|
|
- *
|
|
|
- */
|
|
|
- private PropertiesUtil() {
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置属性
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param key
|
|
|
- * @param value
|
|
|
- */
|
|
|
- public static void setProperty(String key, String value) {
|
|
|
- PROPS.setProperty(key, value);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 加载配置文件
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param resourceName
|
|
|
- */
|
|
|
- public static void loadFromResource(String resourceName) {
|
|
|
- loadFromPath(PathUtil.getResoucePath(resourceName));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 加载配置文件
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param path
|
|
|
- */
|
|
|
- public static synchronized void loadFromPath(String path) {
|
|
|
- if (StringUtils.isBlank(path)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- File file = new File(path);
|
|
|
-
|
|
|
- if (PROP_FILES.contains(file.getCanonicalPath())) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- loadFromFile(file, PROPS);
|
|
|
-
|
|
|
- PROP_FILES.add(file.getCanonicalPath());
|
|
|
- } catch (Exception e) {
|
|
|
- LOG.error("Fail to load and watch file [" + path + "].", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @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 ExamCloudRuntimeException("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 ExamCloudRuntimeException(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"));
|
|
|
- Properties temp = new Properties();
|
|
|
- temp.load(reader);
|
|
|
- for (Entry<Object, Object> entry : temp.entrySet()) {
|
|
|
- Object key = entry.getKey();
|
|
|
- Object value = entry.getValue();
|
|
|
- if (key instanceof String) {
|
|
|
- key = ((String) key).trim();
|
|
|
- }
|
|
|
- if (value instanceof String) {
|
|
|
- value = ((String) value).trim();
|
|
|
- }
|
|
|
- props.put(key, value);
|
|
|
- }
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ExamCloudRuntimeException(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;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+// package cn.com.qmth.examcloud.commons.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.Map.Entry;
|
|
|
+// import java.util.Properties;
|
|
|
+// import java.util.Set;
|
|
|
+//
|
|
|
+// import org.apache.commons.io.IOUtils;
|
|
|
+// import org.apache.commons.lang.StringUtils;
|
|
|
+//
|
|
|
+// import com.google.common.collect.Sets;
|
|
|
+//
|
|
|
+// import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
+// import org.slf4j.Logger;
|
|
|
+// import org.slf4j.LoggerFactory;
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 类注释
|
|
|
+// *
|
|
|
+// * @author WANGWEI
|
|
|
+// * @date 2018年10月8日
|
|
|
+// * @Copyright (c) 2018-2020 http://www.qmth.com.cn/ All Rights Reserved.
|
|
|
+// */
|
|
|
+// public class PropertiesUtil {
|
|
|
+//
|
|
|
+// private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class);
|
|
|
+//
|
|
|
+// private static final Properties PROPS = new Properties();
|
|
|
+//
|
|
|
+// private static final Set<String> PROP_FILES = Sets.newConcurrentHashSet();
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 构造函数
|
|
|
+// *
|
|
|
+// */
|
|
|
+// private PropertiesUtil() {
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 设置属性
|
|
|
+// *
|
|
|
+// * @author WANGWEI
|
|
|
+// * @param key
|
|
|
+// * @param value
|
|
|
+// */
|
|
|
+// public static void setProperty(String key, String value) {
|
|
|
+// PROPS.setProperty(key, value);
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 加载配置文件
|
|
|
+// *
|
|
|
+// * @author WANGWEI
|
|
|
+// * @param resourceName
|
|
|
+// */
|
|
|
+// public static void loadFromResource(String resourceName) {
|
|
|
+// loadFromPath(PathUtil.getResoucePath(resourceName));
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 加载配置文件
|
|
|
+// *
|
|
|
+// * @author WANGWEI
|
|
|
+// * @param path
|
|
|
+// */
|
|
|
+// public static synchronized void loadFromPath(String path) {
|
|
|
+// if (StringUtils.isBlank(path)) {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// File file = new File(path);
|
|
|
+//
|
|
|
+// if (PROP_FILES.contains(file.getCanonicalPath())) {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// loadFromFile(file, PROPS);
|
|
|
+//
|
|
|
+// PROP_FILES.add(file.getCanonicalPath());
|
|
|
+// } catch (Exception e) {
|
|
|
+// LOG.error("Fail to load and watch file [" + path + "].", e);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * @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 ExamCloudRuntimeException("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 ExamCloudRuntimeException(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"));
|
|
|
+// Properties temp = new Properties();
|
|
|
+// temp.load(reader);
|
|
|
+// for (Entry<Object, Object> entry : temp.entrySet()) {
|
|
|
+// Object key = entry.getKey();
|
|
|
+// Object value = entry.getValue();
|
|
|
+// if (key instanceof String) {
|
|
|
+// key = ((String) key).trim();
|
|
|
+// }
|
|
|
+// if (value instanceof String) {
|
|
|
+// value = ((String) value).trim();
|
|
|
+// }
|
|
|
+// props.put(key, value);
|
|
|
+// }
|
|
|
+//
|
|
|
+// } catch (IOException e) {
|
|
|
+// throw new ExamCloudRuntimeException(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;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|