c073d25a93b18f8a0cb6f2f3b99dfe64237af269.svn-base 2.1 KB

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