EAS配置文件Configuration

bosconfig.xml在profiles/server1/config下面     配置監聽listenerconfig.data
package com.kingdee.util;

import java.io.File;
import java.io.PrintStream;
import java.net.URL;
import java.util.List;

public class Configuration {
	public static final String SERVER_ROOT_PROP = "bos.root";
	public static final String SERVER_CONFIG_PROP = "bos.config";
	public static final String DEFAULT_SERVER_CONFIG = "bosconfig.xml";
	private XMLConfiguration xmlConfig;
	private File file;
	private URL url;
	private static Configuration myself;

	public static File getServerRoot() {
		String root = System.getProperty("bos.root");
		if (root == null)
			root = System.getProperty("user.dir");
		return new File(root);
	}

	public static File getFile(String name) {
		if (File.separatorChar != '/')
			name = name.replace('/', File.separatorChar);
		File file = new File(name);
		if (!(file.isAbsolute()))
			file = new File(getServerRoot(), name);
		return file;
	}

	private Configuration(String fileName) {
		this.file = getFile(fileName);
	}

	private Configuration(URL url) {
		this.url = url;
	}

	public static Configuration getInstance() throws ConfigurationException {
		String fileName = System.getProperty("bos.config", "bosconfig.xml");

		return getInstance(fileName);
	}

	public static Configuration getInstance(String fileName) throws ConfigurationException {
		if (myself == null) {
			synchronized (Configuration.class) {
				if (myself == null) {
					Configuration config = new Configuration(fileName);
					config.load();
					myself = config;
				}
			}
		}
		return myself;
	}

	public static Configuration getInstance(URL url) throws ConfigurationException {
		if (myself == null) {
			synchronized (Configuration.class) {
				if (myself == null) {
					Configuration config = new Configuration(url);
					config.load();
					myself = config;
				}
			}
		}
		return myself;
	}

	public void addConfigItem(ConfigurationItem item) {
		this.xmlConfig.addConfigItem(item);
	}

	public ConfigurationItem getConfigItem(String name) {
		return this.xmlConfig.getConfigItem(name);
	}

	public List getConfigItemList(String name) {
		return this.xmlConfig.getConfigItemList(name);
	}

	public ConfigurationItem getConfigItemByPath(String path) {
		return this.xmlConfig.getConfigItemByPath(path);
	}

	public List getConfigItemListByPath(String path) {
		return this.xmlConfig.getConfigItemListByPath(path);
	}

	public void removeConfigItem(String name) {
		this.xmlConfig.removeConfigItem(name);
	}

	public void removeAll() {
		this.xmlConfig.removeAll();
	}

	public void store() throws ConfigurationException {
		this.xmlConfig.store();
	}

	private void load() throws ConfigurationException {
		if ((this.file == null) && (this.url == null)) {
			throw new ConfigurationException("config file not exists.");
		}

		if (this.file != null) {
			if (this.file.exists()) {
				this.xmlConfig = XMLConfiguration.getInstance(this.file);
				return;
			}
			throw new ConfigurationException("config file " + this.file.getAbsolutePath() + " not exists.");
		}

		if (this.url != null) {
			this.xmlConfig = XMLConfiguration.getInstance(this.url);
		} else
			throw new ConfigurationException("config file not exists.");
	}

	public void dump(PrintStream ps) {
		this.xmlConfig.dump(ps);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章