d5d76ac07eda140ab4f151d401a0405b43e7f0c8.svn-base 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. import com.coreframework.util.AppConfig;
  9. /**
  10. * @author Administrator
  11. *
  12. */
  13. public class ConfigUtil {
  14. private static ConfigUtil instance = null;
  15. private static final String cfg = "sys.xml";
  16. private String centerServerUrl;
  17. private String appServerName;
  18. private int port;
  19. public String queueName;
  20. private ConfigUtil() throws Exception {
  21. init();
  22. }
  23. private void init() throws Exception {
  24. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(cfg);
  25. SAXReader reader = new SAXReader();
  26. Document doc = reader.read(inputStream);
  27. String centerServerUrl = doc.getRootElement().elementTextTrim("CenterServerUrl");
  28. if (centerServerUrl != null && !centerServerUrl.equals("")) {
  29. this.centerServerUrl = centerServerUrl;
  30. }
  31. String portStr = doc.getRootElement().elementTextTrim("Port");
  32. if (portStr != null && !portStr.equals("")) {
  33. this.port = Integer.parseInt(portStr);
  34. }
  35. String queueName = doc.getRootElement().elementTextTrim("queueName");
  36. if (queueName != null && !queueName.equals("")) {
  37. this.queueName = queueName;
  38. } else {
  39. this.queueName = "baseinfo";
  40. }
  41. String appServerName = doc.getRootElement().elementTextTrim("AppServerName");
  42. if (appServerName != null && !appServerName.equals("") && !appServerName.equals(this.appServerName)) {
  43. this.appServerName = appServerName;
  44. }
  45. inputStream.close();
  46. }
  47. public static ConfigUtil getInstance() throws Exception {
  48. if (instance == null) {
  49. instance = new ConfigUtil();
  50. }
  51. return instance;
  52. }
  53. public static void create() throws Exception {
  54. instance = new ConfigUtil();
  55. }
  56. /**
  57. * 简化获取配置服务器方式
  58. *
  59. * @return
  60. */
  61. public static String getCSUrl() {
  62. try {
  63. return ConfigUtil.getInstance().getCenterServerUrl().toString();
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. return null;
  68. }
  69. public String getQueueName() {
  70. return queueName;
  71. }
  72. public String getAppServerName() {
  73. return appServerName;
  74. }
  75. public String getCenterServerUrl() {
  76. return centerServerUrl;
  77. }
  78. public int getPort() {
  79. return port;
  80. }
  81. }