⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configimpl.java

📁 java的团队合作代码
💻 JAVA
字号:
package com.briup.impl.config;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.briup.Backup;
import com.briup.Config;
import com.briup.DBStore;
import com.briup.Gather;
import com.briup.Log;
import com.briup.NetClient;
import com.briup.NetServer;
import com.briup.exception.ConfigException;

/**
 * @author renqs
 * @company Briup Technology Inc,.(Shanghai)
 * @date JUL 2, 2008 2:40:38 AM
 */

public class ConfigImpl implements Config {

	private static Config config;
	private String configFile = "../resource/config.xml";
	private Document document;

	private ConfigImpl(Properties props) throws Exception {
		String pConfigFile = null;
		pConfigFile = props.getProperty("config_file_path");
		if (pConfigFile != null)
			configFile = pConfigFile;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		document = builder.parse(configFile);
	}

	public static synchronized Config newInstance(Properties props)
			throws Exception {
		if (config == null)
			config = new ConfigImpl(props);
		return config;
	}

	public Backup getBackup() throws ConfigException {
		return (Backup) loadClass("backup");
	}

	public DBStore getDBStore() throws ConfigException {
		return (DBStore) loadClass("dbstore");
	}

	public Gather getGather() throws ConfigException {
		return (Gather) loadClass("gather");
	}

	public Log getLog() throws ConfigException {
		return (Log) loadClass("log");
	}

	public NetClient getNetClient() throws ConfigException {
		return (NetClient) loadClass("netclient");
	}

	public NetServer getNetServer() throws ConfigException {
		return (NetServer) loadClass("netserver");
	}

	private Object loadClass(String moduleName) throws ConfigException {
		Properties pro = parse(moduleName);
		String single = pro.getProperty("single");
		try {
			if ("true".equals(single)) {
				return getInstanceByMethod(pro);
			} else {
				return getInstanceByConstructor(pro);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConfigException(e.getMessage());
		}
	}

	
	private Object getInstanceByConstructor(Properties props)
			throws ConfigException {
		String className = props.getProperty("class_name");
		Class c = null;
		Constructor con = null;
		try {
			c = Class.forName(className);
			con = c.getConstructor(new Class[] { Properties.class });
			return con.newInstance(new Object[] { props });
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConfigException("constructor instance error!!");
		}
	}

	
	private Object getInstanceByMethod(Properties props) throws ConfigException {
		String className = props.getProperty("class_name");
		String methodName = props.getProperty("constructor_method");
		try {
			Class c = Class.forName(className);
			Method method = c.getMethod(methodName, Properties.class);
			return method.invoke(null, new Object[] { props });
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConfigException("constructor instance error!!");
		}
	}

	private Properties parse(String moduleName) {
		Properties props = new Properties();
		Node node = document.getElementsByTagName(moduleName).item(0);
		NamedNodeMap attrs = node.getAttributes();
		for (int i = 0; i < attrs.getLength(); i++) {
			Node attribute = attrs.item(i);
			String key = attribute.getNodeName();
			String value = attribute.getNodeValue().trim();
			props.setProperty(key, value);
		}
		NodeList elements = node.getChildNodes();
		for (int j = 0; j < elements.getLength(); j++) {
			Node childElement = elements.item(j);
			if (childElement instanceof Element) {
				String key = childElement.getNodeName();
				String value = childElement.getFirstChild().getNodeValue()
						.trim();
				props.setProperty(key, value);
			}
		}
		return props;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -