用于读/写配置的工具,下面列出了各种配置(从最高优先级到最低优先级)

代码写法:

  1 import java.io.File;
  2 import java.io.FileInputStream;
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.util.Enumeration;
  6 import java.util.HashMap;
  7 import java.util.Properties;
  8 
  9 import org.slf4j.Logger;
 10 import org.slf4j.LoggerFactory;
 11 
 12 /**
 13  * 用于读/写配置的工具,下面列出了各种配置(从最高优先级到最低优先级):1。代码中明确的配置集。 2.从文件加载的配置。 3.默认配置。优先级较高的配置将覆盖优先级较低的配置。
 14  * 
 15  */
 16 public class ConfigurationUtils
 17 {
 18     
 19     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationUtils.class);
 20     
 21     private HashMap<String, String> propertyMapFromFile = new HashMap<String, String>();;
 22     
 23     private HashMap<String, String> propertyMapExplicitlyDefined = new HashMap<String, String>();;
 24     
 25     /**
 26      * 从文件加载配置
 27      * 
 28      * @param configurationFileName文件路径,如果为null则不执行任何操作
 29      * @throws 读取配置文件时抛出IOException IO异常
 30      */
 31     public ConfigurationUtils(String configurationFileName) throws IOException
 32     {
 33         
 34         if (configurationFileName == null)
 35         {
 36             return;
 37         }
 38         
 39         InputStream inputStream = null;
 40         try
 41         {
 42             ClassLoader classLoader = ConfigurationUtils.class.getClassLoader();
 43             if (null != classLoader)
 44             {
 45                 inputStream = classLoader.getResourceAsStream(configurationFileName);
 46                 if(inputStream != null){
 47                     LOGGER.debug("get from classLoader");
 48                 }
 49             }
 50             
 51             if (null == inputStream && this.getClass() != null)
 52             {
 53                 inputStream = this.getClass().getResourceAsStream(configurationFileName);
 54                 if(inputStream != null){
 55                     LOGGER.debug("get from class");
 56                 }
 57             }
 58             
 59             if (null == inputStream && configurationFileName.startsWith("/") && null != classLoader){
 60                 inputStream = classLoader.getResourceAsStream("." + configurationFileName);
 61                 if(inputStream != null){
 62                     LOGGER.debug("get from ./");
 63                 }
 64             }
 65             
 66             if (null == inputStream && configurationFileName.startsWith("/") && null != classLoader){
 67                 inputStream = classLoader.getResourceAsStream(configurationFileName.substring(1));
 68                 if(inputStream != null){
 69                     LOGGER.debug("get from no /");
 70                 }
 71             }
 72             
 73             if (null == inputStream)
 74             {
 75                 inputStream = ClassLoader.getSystemResourceAsStream(configurationFileName);
 76                 if(inputStream != null){
 77                     LOGGER.debug("get from ClassLoader");
 78                 }
 79             }
 80             
 81             if (null == inputStream)
 82             {
 83                 File file = new File(configurationFileName);
 84                 if(file.exists()){
 85                     inputStream = new FileInputStream(configurationFileName);    
 86                     if(inputStream != null){
 87                         LOGGER.debug("get from file.");
 88                     }
 89                 }
 90             }
 91             
 92             if (null == inputStream)
 93             {
 94                 LOGGER.warn("configuration file {} not found, ignore it.", configurationFileName);
 95                 return;
 96             }
 97             
 98             Properties props = new Properties();
 99             props.load(inputStream);
100             for (Enumeration propNames = props.propertyNames(); propNames.hasMoreElements();)
101             {
102                 String propName = (String)propNames.nextElement();
103                 propertyMapFromFile.put(propName, props.getProperty(propName));
104             }
105             LOGGER.debug("propertyMapFromFile size : {}", propertyMapFromFile.size());
106         }
107         finally
108         {
109             if (null != inputStream)
110             {
111                 try
112                 {
113                     inputStream.close();
114                 }
115                 catch (IOException e)
116                 {
117                     LOGGER.error("IOException: " + e);
118                 }
119             }
120         }
121         return;
122     }
123     
124     /**
125      * 获取属性的值字符串,如果该属性不存在,则返回defaultValue。
126      * 
127      * @param propertyName属性名称
128      * @param defaultValue默认值
129      * @return property value
130      */
131     
132     public String getProperty(String propertyName, String defaultValue)
133     {
134         if (propertyMapExplicitlyDefined.containsKey(propertyName))
135         {
136             return propertyMapExplicitlyDefined.get(propertyName);
137         }
138         else if (propertyMapFromFile.containsKey(propertyName))
139         {
140             return propertyMapFromFile.get(propertyName);
141         }
142         else
143         {
144             return defaultValue;
145         }
146     }
147     
148     public String getProperty(String propertyName)
149     {
150         return getProperty(propertyName, null);
151     }
152     
153     /**
154      * 设置属性的值。
155      * 
156      * @param propertyName属性名称
157      * @param value peoperty value
158      */
159     
160     public void setProperty(String propertyName, String value)
161     {
162         propertyMapExplicitlyDefined.put(propertyName, value);
163     }
164     
165 }
原文地址:https://www.cnblogs.com/wangquanyi/p/11348112.html