PropertiesLoader.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2005-2011 springside.org.cn
  3. *
  4. * $Id: PropertiesLoader.java 1690 2012-02-22 13:42:00Z calvinxiu $
  5. */
  6. package com.thinkgem.jeesite.common.utils;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.util.NoSuchElementException;
  12. import java.util.Properties;
  13. import org.apache.commons.io.IOUtils;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.core.io.DefaultResourceLoader;
  17. import org.springframework.core.io.Resource;
  18. import org.springframework.core.io.ResourceLoader;
  19. /**
  20. * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
  21. * @author calvin
  22. * @version 2013-05-15
  23. */
  24. public class PropertiesLoader {
  25. private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
  26. private static ResourceLoader resourceLoader = new DefaultResourceLoader();
  27. private final Properties properties;
  28. public PropertiesLoader(String... resourcesPaths) {
  29. properties = loadProperties(resourcesPaths);
  30. }
  31. public Properties getProperties() {
  32. return properties;
  33. }
  34. /**
  35. * 取出Property,但以System的Property优先,取不到返回空字符串.
  36. */
  37. private String getValue(String key) {
  38. String systemProperty = System.getProperty(key);
  39. if (systemProperty != null) {
  40. return systemProperty;
  41. }
  42. if (properties.containsKey(key)) {
  43. return properties.getProperty(key);
  44. }
  45. return "";
  46. }
  47. /**
  48. * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
  49. */
  50. public String getProperty(String key) {
  51. String value = getValue(key);
  52. if (value == null) {
  53. throw new NoSuchElementException();
  54. }
  55. return value;
  56. }
  57. /**
  58. * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
  59. */
  60. public String getProperty(String key, String defaultValue) {
  61. String value = getValue(key);
  62. return value != null ? value : defaultValue;
  63. }
  64. /**
  65. * 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
  66. */
  67. public Integer getInteger(String key) {
  68. String value = getValue(key);
  69. if (value == null) {
  70. throw new NoSuchElementException();
  71. }
  72. return Integer.valueOf(value);
  73. }
  74. /**
  75. * 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
  76. */
  77. public Integer getInteger(String key, Integer defaultValue) {
  78. String value = getValue(key);
  79. return value != null ? Integer.valueOf(value) : defaultValue;
  80. }
  81. /**
  82. * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
  83. */
  84. public Double getDouble(String key) {
  85. String value = getValue(key);
  86. if (value == null) {
  87. throw new NoSuchElementException();
  88. }
  89. return Double.valueOf(value);
  90. }
  91. /**
  92. * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
  93. */
  94. public Double getDouble(String key, Integer defaultValue) {
  95. String value = getValue(key);
  96. return value != null ? Double.valueOf(value) : defaultValue;
  97. }
  98. /**
  99. * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
  100. */
  101. public Boolean getBoolean(String key) {
  102. String value = getValue(key);
  103. if (value == null) {
  104. throw new NoSuchElementException();
  105. }
  106. return Boolean.valueOf(value);
  107. }
  108. /**
  109. * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
  110. */
  111. public Boolean getBoolean(String key, boolean defaultValue) {
  112. String value = getValue(key);
  113. return value != null ? Boolean.valueOf(value) : defaultValue;
  114. }
  115. /**
  116. * 载入多个文件, 文件路径使用Spring Resource格式.
  117. */
  118. private Properties loadProperties(String... resourcesPaths) {
  119. Properties props = new Properties();
  120. for (String location : resourcesPaths) {
  121. // logger.debug("Loading properties file from:" + location);
  122. InputStream is = null;
  123. try {
  124. Resource resource = resourceLoader.getResource(location);
  125. is = resource.getInputStream();
  126. props.load(new BufferedReader(new InputStreamReader(is,"utf-8")));
  127. } catch (IOException ex) {
  128. logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
  129. } finally {
  130. IOUtils.closeQuietly(is);
  131. }
  132. }
  133. return props;
  134. }
  135. }