1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /**
- *
- */
- package com.yihu.platform.utils;
- import java.io.InputStream;
- import org.dom4j.Document;
- import org.dom4j.io.SAXReader;
- /**
- * @author Administrator
- *
- */
- public class ConfigUtil {
- private static ConfigUtil instance = null;
- private static final String cfg = "sys.xml";
- private String centerServerUrl;
- private String appServerName;
- private int port;
- public String queueName;
- private ConfigUtil() throws Exception {
- init();
- }
- private void init() throws Exception {
- InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(cfg);
- SAXReader reader = new SAXReader();
- Document doc = reader.read(inputStream);
- String centerServerUrl = doc.getRootElement().elementTextTrim("CenterServerUrl");
- if (centerServerUrl != null && !centerServerUrl.equals("")) {
- this.centerServerUrl = centerServerUrl;
- }
-
- String portStr = doc.getRootElement().elementTextTrim("Port");
- if (portStr != null && !portStr.equals("")) {
- this.port = Integer.parseInt(portStr);
- }
- String queueName = doc.getRootElement().elementTextTrim("queueName");
- if (queueName != null && !queueName.equals("")) {
- this.queueName = queueName;
- } else {
- this.queueName = "baseinfo";
- }
- String appServerName = doc.getRootElement().elementTextTrim("AppServerName");
- if (appServerName != null && !appServerName.equals("") && !appServerName.equals(this.appServerName)) {
- this.appServerName = appServerName;
- }
- inputStream.close();
- }
- public static ConfigUtil getInstance() throws Exception {
- if (instance == null) {
- instance = new ConfigUtil();
- }
- return instance;
- }
- public static void create() throws Exception {
- instance = new ConfigUtil();
- }
- /**
- * 简化获取配置服务器方式
- *
- * @return
- */
- public static String getCSUrl() {
- try {
- return ConfigUtil.getInstance().getCenterServerUrl().toString();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public String getQueueName() {
- return queueName;
- }
- public String getAppServerName() {
- return appServerName;
- }
- public String getCenterServerUrl() {
- return centerServerUrl;
- }
- public int getPort() {
- return port;
- }
- }
|