自己写的一个SystemConfig,备份下

  • Post author:
  • Post category:其他


java 代码

  1. package

    org.wanwei.test.xmlreader;

  2. import

    java.io.InputStream;

  3. import

    java.util.HashMap;

  4. import

    java.util.Iterator;

  5. import

    java.util.List;

  6. import

    java.util.Map;

  7. import

    java.util.Set;

  8. import

    org.apache.log4j.Logger;

  9. import

    org.jdom.Document;

  10. import

    org.jdom.Element;

  11. import

    org.jdom.input.SAXBuilder;

  12. /**

  13. * 读取系统配置文件systemConfig.xml.


  14. * 只支持读取简单的XML配置:


  15. * eg:<assocication>


  16. *                <bj_comp_eff_date>2007-06-20</bj_comp_eff_date>


  17. *       </assocication>


  18. * 不支持带属性的XML读取:


  19. *   <Controler type=”1″ factoryClass=”com.elinksoft.bsp.action.ActionBeanFactory” />

  20. * @author wanwei

  21. * @since 2007-7-4

  22. */

  23. public


    class

    SystemConfig
  24. {

  25. private


    static


    final

    Logger logger = Logger.getLogger(SystemConfig.

    class

    );

  26. private


    static


    final

    String DEFAULT_SPLIT =

    “/”

    ;

  27. private


    static


    final

    Map cacheMap =

    new

    HashMap();

  28. private


    static


    boolean

    initialize =

    false

    ;

  29. /**

  30. * 从systemConfig.xml文件中读取相应配置


  31. * note:第一次读取需要初始化,以后都从缓存中读取.

  32. * @param configPath eg:/config/assocication/bj_comp_eff_date

  33. * @return String not null or empty

  34. * @throws Exception “unmatching configPath” will throw if no matching text found

  35. */

  36. public


    static

    String getConfigTextValue(String configPath)

    throws

    Exception
  37. {

  38. if

    ( !initialize )
  39. {
  40. initialize();
  41. }
  42. String value = (String) cacheMap.get(configPath);

  43. if

    ( value ==

    null

    || value.length() ==

    0

    )
  44. {
  45. getConfigTextValue(configPath,

    true

    );
  46. }

  47. return

    value;
  48. }

  49. /**

  50. * 从systemConfig.xml文件中读取相应配置

  51. * @param configPath eg:/config/assocication/bj_comp_eff_date

  52. * @param reinitialize if true ,system will read the xml files again.

  53. * @return String not null or empty

  54. * @throws Exception Exception “unmatching configPath” will throw if no matching text found

  55. */

  56. public


    static


    final

    String getConfigTextValue(String configPath,

    boolean

    reinitialize)

  57. throws

    Exception
  58. {

  59. if

    ( reinitialize )
  60. {
  61. initialize();
  62. }
  63. String value = (String) cacheMap.get(configPath);

  64. if

    ( value ==

    null

    || value.length() ==

    0

    )

  65. throw


    new

    Exception(

    “unmatching configPath:”

    + configPath);

  66. return

    value;
  67. }

  68. /**

  69. * read xml files and cache them.

  70. * @throws Exception

  71. */

  72. private


    static


    void

    initialize()

    throws

    Exception
  73. {
  74. SAXBuilder sb =

    new

    SAXBuilder();
  75. InputStream is = ClassLoader.getSystemResourceAsStream(

    “org/wanwei/test/xmlreader/systemconfig.xml”

    );
  76. Document doc = sb.build(is);

    // 构造文档对象
  77. Element root = doc.getRootElement();

    // 获取根元素
  78. recursiveReadElement(root,

    “”

    );

    // 递归读取xml元素
  79. initialize =

    true

    ;

  80. // logger the cacheMap.

  81. if

    ( logger.isInfoEnabled() )
  82. logger.info(

    “initialize system config:\n”

    +mapToString(cacheMap));

  83. //System.out.println(mapToString(cacheMap));
  84. }

  85. private


    static


    void

    recursiveReadElement(Element root, String parentName)
  86. {
  87. List list = root.getChildren();
  88. String text = root.getTextTrim();
  89. String url = parentName + DEFAULT_SPLIT + root.getName();

  90. if

    ( text !=

    null

    && text.length() >

    0

    )
  91. {
  92. cacheMap.put(url, text);
  93. }

  94. if

    ( list !=

    null

    && list.size() >=

    0

    )
  95. {

  96. for

    (

    int

    i =

    0

    ; i < list.size(); i++ )
  97. {

  98. if

    ( list.get(i)

    instanceof

    Element )
  99. {
  100. recursiveReadElement((Element) list.get(i), url);
  101. }
  102. }
  103. }
  104. }

  105. private


    static

    String mapToString(Map map)
  106. {
  107. StringBuffer mapBuffer =

    new

    StringBuffer();

  108. if

    ( map ==

    null

    || map.isEmpty() )

  109. return


    “”

    ;
  110. Set KeySet = map.keySet();
  111. Iterator iterator = KeySet.iterator();

  112. while

    ( iterator.hasNext() )
  113. {
  114. String key = (String) iterator.next();
  115. String value = (String) map.get(key);
  116. mapBuffer.append(key +

    “=”

    + value);

  117. if

    ( iterator.hasNext() )
  118. mapBuffer.append(

    “;\n”

    );
  119. }

  120. return

    mapBuffer.toString();
  121. }

  122. public


    static


    void

    main(String[] arg0)
  123. {

  124. try
  125. {
  126. System.out.println(SystemConfig.getConfigTextValue(

    “/config/assocication/bj_comp_eff_date”

    ));
  127. System.out.println(

    “OK”

    );
  128. }

    catch

    ( Exception e )
  129. {
  130. e.printStackTrace();
  131. System.out.println(

    “ERROR”

    );
  132. }
  133. }
  134. }