c130aa8e4e5744025a0b60ecd4529bcb3f3915f8.svn-base 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. *
  3. */
  4. package com.yihu.platform.utils;
  5. import java.io.InputStream;
  6. import org.dom4j.Document;
  7. import org.dom4j.io.SAXReader;
  8. /**
  9. * @author Administrator
  10. *
  11. */
  12. public class ConfigUtil {
  13. private static ConfigUtil instance = null;
  14. private static final String cfg = "sys.xml";
  15. private String centerServerUrl;
  16. private String appServerName;
  17. private int port;
  18. private ConfigUtil() throws Exception {
  19. init();
  20. }
  21. private void init() throws Exception {
  22. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(cfg);
  23. SAXReader reader = new SAXReader();
  24. Document doc = reader.read(inputStream);
  25. String centerServerUrl = doc.getRootElement().elementTextTrim("CenterServerUrl");
  26. if (centerServerUrl != null && !centerServerUrl.equals("")) {
  27. this.centerServerUrl = centerServerUrl;
  28. }
  29. String portStr = doc.getRootElement().elementTextTrim("Port");
  30. if (portStr != null && !portStr.equals("")) {
  31. this.port = Integer.parseInt(portStr);
  32. }
  33. String appServerName = doc.getRootElement().elementTextTrim("AppServerName");
  34. if (appServerName != null && !appServerName.equals("") && !appServerName.equals(this.appServerName)) {
  35. this.appServerName = appServerName;
  36. }
  37. inputStream.close();
  38. }
  39. public static ConfigUtil getInstance() throws Exception {
  40. if (instance == null) {
  41. instance = new ConfigUtil();
  42. }
  43. return instance;
  44. }
  45. public static void create() throws Exception {
  46. instance = new ConfigUtil();
  47. }
  48. /**
  49. * 简化获取配置服务器方式
  50. *
  51. * @return
  52. */
  53. public static String getCSUrl() {
  54. try {
  55. return ConfigUtil.getInstance().getCenterServerUrl().toString();
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. return null;
  60. }
  61. public String getAppServerName() {
  62. return appServerName;
  63. }
  64. public String getCenterServerUrl() {
  65. return centerServerUrl;
  66. }
  67. public int getPort() {
  68. return port;
  69. }
  70. }