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

📄 configimpl.java

📁 一个信息采集系统...对刚接触JSP和JAVA的人来说还有用
💻 JAVA
字号:
package com.briup.inf.impl;

import java.io.File;
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.Node;
import org.w3c.dom.NodeList;

import com.briup.exception.ConfigException;
import com.briup.inf.Backup;
import com.briup.inf.Config;
import com.briup.inf.DBStore;
import com.briup.inf.Gather;
import com.briup.inf.Log;
import com.briup.inf.NetClient;
import com.briup.inf.NetServer;

/**
 * class ConfigImpl
 *
 * @author Jimmy Zhou
 * @Date 2008-2-3 上午08:48:34
 */
public class ConfigImpl implements Config {

	private static Config config; // 提供一个私有的静态的本类应用

	private String config_file_path = "src/com/briup/config.xml";

	private Document document;

	private ConfigImpl(Properties pro) { // 提供一个私有构造器
		String configFilePath = pro.getProperty("config_file_path");
		if (configFilePath != null) {
			config_file_path = configFilePath;
		}
		// 创建(生成)一个解析器工厂,这个对象专门用来创建解析器
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 通过上面的解析器工厂来创建解析器
		DocumentBuilder builder = null; // 生成一个解析器引用
		try {
			builder = factory.newDocumentBuilder(); // 通过解析器工厂创建解析器
			// 解析文件,产生document对象,然后利用doucument的api完成对文件的解析,具体实现见parse方法
			document = builder.parse(new File(config_file_path)); // 此方法返回一个Docuement类型的对象,里面包含这个xml文件的所有配置信息;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Config newInstance(Properties pro)// 提供一个共有的静态方法调用本类私有构造器,创建实例返回
	{
		if (config == null)
			config = new ConfigImpl(pro);
		return config;
	}

	public Backup getBackup() throws ConfigException {

		return (Backup) getModuleObjectByName("backup");
	}

	public DBStore getDBStore() throws ConfigException {

		return (DBStore) getModuleObjectByName("dbstore");
	}

	public Gather getGather() throws ConfigException {

		return (Gather) getModuleObjectByName("gather");
	}

	public Log getLog() throws ConfigException {

		return (Log) getModuleObjectByName("log");
	}

	public NetClient getNetClient() throws ConfigException {

		return (NetClient) getModuleObjectByName("netclient");
	}

	public NetServer getNetServer() throws ConfigException {

		return (NetServer) getModuleObjectByName("netserver");
	}

	/**
	 * 根据moudle名字返回配置信息
	 */	 
	private Properties parse(String moduleName) { 
		Properties pro = new Properties();
		// 通过传进来的module名字,获取这个module的node对象(元素节点)
		NodeList moduleNodes = document.getElementsByTagName(moduleName);
		//针对每个节点,只拿到一个节点
		Node node = moduleNodes.item(0);
		// 获取这个module的属性的node对象
		Node attrNode = node.getAttributes().item(0); 
		String attrName = attrNode.getNodeName();
		String attrValue = attrNode.getNodeValue();
		// pro.put(attrName, attrValue);
		//HashTable中的方法,即Properties父类中的方法,一般建议不去使用
		pro.setProperty(attrName, attrValue);
		// 获取这个module的所有子元素的node对象
		NodeList childNodes = node.getChildNodes();
		// 遍历子节点,将其放入properties
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node childNode = childNodes.item(i);
			if (childNode instanceof Element) {
				Element element = (Element) childNode;
				String elementName = element.getNodeName();
				String elementValue = element.getFirstChild().getNodeValue();
				pro.put(elementName, elementValue);
			}
		}
		return pro;
	}

	public Object getModuleObjectByName(String moduleName)
			throws ConfigException {
		Object obj = null;
		Properties pro = parse(moduleName);
		String class_name = pro.getProperty("class_name");
		String constructor_method = pro.getProperty("constructor_method");
		String single = pro.getProperty("single");
		Class clazz = null;
		try {
			// 获取这个模块的Class对象。其实这个Class对象就是对一个类的描述
			clazz = Class.forName(class_name);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		// clazz.newInstance();//调用无参构造器,在此无用
		if (single.equals("true")) {
			try {
				// 获取newInstance这个方法的对应的method对象, 第一个参数就是配置文件中的方法,第二个参数是参数类型的镜象
				Method method = clazz.getMethod(constructor_method,new Class[] { Properties.class });
				
				// Method这个类提供一个invoke方法,等同于去调用newInstance方法,
				//这个调用过程与类名+方法名调用产生的结果一样,但内部机制不同
				obj = method.invoke(clazz, new Object[] { pro });
					/*
					 * 调用invoke方法相当于调用newInstance方法,
					 * 第一个参数“obj”对象为class对象, 第二个参数是要传入一个对象数组(为了可以具有通用性),在此使用properyies对象
					 */
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (single.equals("false")) {
			try {
				Constructor constructor = clazz.getConstructor(new Class[] { Properties.class });
				obj = constructor.newInstance(new Object[] { pro });
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return obj;
	}

	public static void main(String[] args) {
		ConfigImpl config = new ConfigImpl(new Properties());
		Properties pro = config.parse("gather");
		System.out.println(pro);
		try {
			System.out.println("backup address is " + config.getBackup());
			System.out.println("log address is " + config.getLog());
			System.out.println("gather address is " + config.getGather());
			System.out.println("dbstrore address is " + config.getDBStore());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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