/** * */ package com.yihu.platform.utils; import java.io.InputStream; import org.dom4j.Document; import org.dom4j.io.SAXReader; import com.coreframework.util.AppConfig; /** * @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; } }