12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /**
- *
- */
- 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;
- 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 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 getAppServerName() {
- return appServerName;
- }
- public String getCenterServerUrl() {
- return centerServerUrl;
- }
- public int getPort() {
- return port;
- }
- }
|