| New file | 
 |  |  | 
 |  |  | package com.dy.common.util; | 
 |  |  |  | 
 |  |  | import java.io.InputStream; | 
 |  |  | import java.net.URL; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.Iterator; | 
 |  |  | import java.util.List; | 
 |  |  |  | 
 |  |  | import org.jdom2.Document; | 
 |  |  | import org.jdom2.Element; | 
 |  |  | import org.jdom2.input.SAXBuilder; | 
 |  |  |  | 
 |  |  | @SuppressWarnings("unused") | 
 |  |  | public class Config { | 
 |  |  |  | 
 |  |  |    private static HashMap<String, String> configs; | 
 |  |  |    private static Config config; | 
 |  |  | 	 | 
 |  |  |    /** | 
 |  |  |     * 得到唯一实例 | 
 |  |  |     * @return 返回实例 | 
 |  |  |     */ | 
 |  |  |    public static Config getInstance() { | 
 |  |  |       if (config == null) { | 
 |  |  |          config = new Config(); | 
 |  |  |          configs = new HashMap<>(); | 
 |  |  |       } | 
 |  |  |       return config; | 
 |  |  |    } | 
 |  |  | 	     | 
 |  |  |    private Config() { | 
 |  |  |    } | 
 |  |  |    /** | 
 |  |  |     * 根据配置名称得到配置值 | 
 |  |  |     * @param name 配置名称 | 
 |  |  |     * @return 配置值 | 
 |  |  |     */ | 
 |  |  |    public static String getConfig(String name) { | 
 |  |  |       if (configs == null) { | 
 |  |  |          return null; | 
 |  |  |       } | 
 |  |  |       if(name.trim().equals("")){ | 
 |  |  |          return "" ; | 
 |  |  |       } | 
 |  |  |       return configs.get(name); | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 初始化配置 | 
 |  |  |     * @param fileUrl 配置文件路径 | 
 |  |  |     * @throws Exception 抛出异常 | 
 |  |  |     */ | 
 |  |  |    public void init(URL fileUrl) throws Exception { | 
 |  |  |       parseConfig(createDom(fileUrl));		 | 
 |  |  |    } | 
 |  |  |    /** | 
 |  |  |     * 重新初始化 | 
 |  |  |     * @param fileUrl 配置文件路径 | 
 |  |  |     * @param propertiesInput 属性文件输入流 | 
 |  |  |     * @throws Exception 抛出异常 | 
 |  |  |     */ | 
 |  |  |    public void reInit(URL fileUrl, InputStream propertiesInput) throws Exception { | 
 |  |  |       configs = new HashMap<>(); | 
 |  |  |       parseConfig(createDom(fileUrl)); | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 创建dom对象 | 
 |  |  |     * @param configFileURL 配置文件路径 | 
 |  |  |     * @return dom对象 | 
 |  |  |     */ | 
 |  |  |    private Document createDom(URL configFileURL) throws Exception { | 
 |  |  |       if (configFileURL == null) { | 
 |  |  |          throw new Exception("没有得到配置文件!", null); | 
 |  |  |       } | 
 |  |  |       Document doc = null; | 
 |  |  |       try { | 
 |  |  |          SAXBuilder sb = new SAXBuilder(); | 
 |  |  |          doc = sb.build(configFileURL); | 
 |  |  |          if (doc == null) { | 
 |  |  |             throw new Exception("没有生成配置文件的DOM对象!", null); | 
 |  |  |          } | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          throw new Exception("生成配置文件的DOM对象失败!", e); | 
 |  |  |       } | 
 |  |  |       return doc; | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 分析服务器运行参数 | 
 |  |  |     * @param doc dom对象 | 
 |  |  |     * @throws Exception 抛出异常 | 
 |  |  |     */ | 
 |  |  |    private void parseConfig(Document doc) throws Exception { | 
 |  |  |       Element root = doc.getRootElement(); | 
 |  |  |       if (root == null) { | 
 |  |  |          throw new Exception("没有得到配置文件根元素config!"); | 
 |  |  |       } | 
 |  |  |       List<Element> list = root.getChildren(); | 
 |  |  |       if (list == null) { | 
 |  |  |          throw new Exception("配置文件没有配置元素!"); | 
 |  |  |       } | 
 |  |  |       Iterator<Element> it = list.iterator(); | 
 |  |  |       Element e ; | 
 |  |  |       String name ; | 
 |  |  |       String tx ; | 
 |  |  |       while (it.hasNext()) { | 
 |  |  |          e = it.next(); | 
 |  |  |          if (e == null) { | 
 |  |  |             throw new Exception("得到配置文件中元素配置出错!"); | 
 |  |  |          } | 
 |  |  |          name = e.getName(); | 
 |  |  |          if (configs.get(name) != null) { | 
 |  |  |             throw new Exception("配置文件中元素" + name + "重名!"); | 
 |  |  |          } | 
 |  |  |          tx = e.getText(); | 
 |  |  |  | 
 |  |  |          if(tx != null && tx.startsWith("${") && tx.endsWith("}")){ | 
 |  |  |             tx = tx.substring(2, tx.length() - 1) ; | 
 |  |  |             tx = ConfigProperties.getConfig(tx) ; | 
 |  |  |          } | 
 |  |  |          configs.put(name, tx);	 | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  | } |