PropertiesUtil.java 4.7 KB

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