PropertiesUtil.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package cn.com.qmth.examcloud.commons.util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.util.Properties;
  9. import java.util.Set;
  10. import org.apache.commons.io.IOUtils;
  11. import org.apache.commons.lang.StringUtils;
  12. import com.google.common.collect.Sets;
  13. import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
  14. import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
  15. import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
  16. /**
  17. * 类注释
  18. *
  19. * @author WANGWEI
  20. * @date 2018年10月8日
  21. * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  22. */
  23. public class PropertiesUtil {
  24. private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(PropertiesUtil.class);
  25. private static final Properties PROPS = new Properties();
  26. private static final Set<String> PROP_FILES = Sets.newConcurrentHashSet();
  27. static {
  28. init();
  29. }
  30. /**
  31. * 构造函数
  32. *
  33. */
  34. private PropertiesUtil() {
  35. }
  36. /**
  37. * 初始化方法
  38. *
  39. * @author WANGWEI
  40. */
  41. public static void init() {
  42. configure(PathUtil.getResoucePath("resource.properties"));
  43. }
  44. /**
  45. * 加载配置文件并观察配置文件
  46. *
  47. * @author WANGWEI
  48. * @param path
  49. */
  50. public static synchronized void configure(String path) {
  51. if (StringUtils.isBlank(path)) {
  52. return;
  53. }
  54. try {
  55. File file = new File(path);
  56. if (PROP_FILES.contains(file.getCanonicalPath())) {
  57. return;
  58. }
  59. loadFromFile(file, PROPS);
  60. PROP_FILES.add(file.getCanonicalPath());
  61. } catch (Exception e) {
  62. LOG.error("Fail to load and watch file [" + path + "].", e);
  63. }
  64. }
  65. /**
  66. * @param path
  67. * @param props
  68. */
  69. public static void loadFromDir(String path, Properties props) {
  70. File dir = new File(path + "");
  71. if ((!dir.exists()) || (!dir.isDirectory())) {
  72. LOG.error("directory [" + path + "] is illegal.");
  73. return;
  74. }
  75. LOG.info("Loading all Properties files from path [" + path + "].");
  76. File[] files = dir.listFiles();
  77. if (null == files || 0 == files.length) {
  78. return;
  79. }
  80. for (File file : files) {
  81. if (file.isDirectory()) {
  82. continue;
  83. }
  84. if (file.getName().toLowerCase().endsWith(".properties")) {
  85. loadFromFile(file, props);
  86. }
  87. }
  88. }
  89. /**
  90. * @param resourceName
  91. * @param props
  92. */
  93. public static void loadFromResource(String resourceName, Properties props) {
  94. if (null != PropertiesUtil.class.getClassLoader()) {
  95. InputStream in = PropertiesUtil.class.getClassLoader()
  96. .getResourceAsStream(resourceName);
  97. loadFromStream(in, props);
  98. } else {
  99. throw new ExamCloudRuntimeException("fail to get class loader");
  100. }
  101. }
  102. /**
  103. * @param file
  104. * @param props
  105. * @throws IOException
  106. */
  107. public static void loadFromFile(File file, Properties props) {
  108. if (null == file) {
  109. LOG.error("file is null.");
  110. return;
  111. }
  112. if (!file.isFile()) {
  113. LOG.error("file is not a normal file.");
  114. return;
  115. }
  116. String path = PathUtil.getCanonicalPath(file);
  117. LOG.info("Loading properties from file [" + path + "]");
  118. try {
  119. loadFromStream(new FileInputStream(file), props);
  120. } catch (Exception e) {
  121. throw new ExamCloudRuntimeException(e);
  122. }
  123. }
  124. /**
  125. * @param is
  126. * @param props
  127. */
  128. public static void loadFromStream(InputStream is, Properties props) {
  129. BufferedReader reader = null;
  130. try {
  131. reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  132. props.load(reader);
  133. } catch (IOException e) {
  134. throw new ExamCloudRuntimeException(e);
  135. } finally {
  136. IOUtils.closeQuietly(reader);
  137. IOUtils.closeQuietly(is);
  138. }
  139. }
  140. /**
  141. * @param key
  142. * @return
  143. */
  144. public static String getString(String key) {
  145. String value = PROPS.getProperty(key);
  146. if (StringUtils.isNotBlank(value)) {
  147. return value.trim();
  148. } else {
  149. if (LOG.isDebugEnabled()) {
  150. LOG.debug("No property key named [" + key + "] is configured.");
  151. }
  152. return null;
  153. }
  154. }
  155. /**
  156. * @param key
  157. * @param defaultValue
  158. * @return
  159. */
  160. public static String getString(String key, String defaultValue) {
  161. String value = getString(key);
  162. if (null != value) {
  163. return value;
  164. }
  165. return defaultValue;
  166. }
  167. /**
  168. * @param key
  169. * @param defaultValue
  170. * @return
  171. */
  172. public static int getInt(String key, int defaultValue) {
  173. String value = getString(key);
  174. if (null != value) {
  175. try {
  176. return Integer.parseInt(value);
  177. } catch (NumberFormatException e) {
  178. PROPS.setProperty(key, String.valueOf(defaultValue));
  179. return defaultValue;
  180. }
  181. }
  182. return defaultValue;
  183. }
  184. /**
  185. * @param key
  186. * @param defaultValue
  187. * @return
  188. */
  189. public static long getLong(String key, long defaultValue) {
  190. String value = getString(key);
  191. if (null != value) {
  192. try {
  193. return Long.parseLong(value);
  194. } catch (NumberFormatException e) {
  195. return defaultValue;
  196. }
  197. }
  198. return defaultValue;
  199. }
  200. /**
  201. * 获取boolean
  202. *
  203. * @author WANGWEI
  204. * @param key
  205. * @param defaultVale
  206. * @return
  207. */
  208. public static boolean getBoolean(String key, boolean defaultVale) {
  209. String value = getString(key);
  210. if (null == value) {
  211. return defaultVale;
  212. }
  213. if (value.equals("true")) {
  214. return true;
  215. } else if (value.equals("false")) {
  216. return false;
  217. } else {
  218. return defaultVale;
  219. }
  220. }
  221. }