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

📄 configimpl.java

📁 springGather 在JAVA环境开发的电信运营支持系统
💻 JAVA
字号:
package org.com.gather;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;

public class ConfigImpl implements Config{
	private Properties pro = null;
	private Document doc = null;
	private static ConfigImpl instance = null;
	private static String key = "abc";

	private ConfigImpl(Properties pro){
		this.pro = pro;
		String config_path = pro.getProperty("config_path");

		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(config_path);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public static Config getInstance(Properties pro){
		if(instance == null){
			synchronized(key){
				if(instance == null){
					instance = new ConfigImpl(pro);
				}
			}
		}
		return instance;
	}

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

	public DB getDB(){
		return (DB)loadClass("db");
	}

	public Log getLog(){
		return null;
	}

	public BackUp getBackUp(){
		return null;
	}	

	public Client getClient(){
		return null;
	}	

	public Server getServer(){
		return null;
	}	

	private Object loadClass(String nodeName){
		Properties pro = parse(nodeName);

		String single = pro.getProperty("single");
		String methodName = pro.getProperty("methodName"); 
		//获取参数类型!
		String args = pro.getProperty("args");
		System.out.println("args is --->"+args);

		if(single.equals("true") && args != null){
			System.out.println("single---true && args != null");
			return ClassLoaderFactory.loadClassByMethodArgs(methodName,pro);
		}else if(single.equals("ture") && args == null){
			return null;
			//return ClassLoaderFactory.loadClassByMethodArgs(methodName);
		}else if(single.equals("false") && args != null){
			System.out.println("single---false && args != null");
			return ClassLoaderFactory.loadClassByConstructorArgs(pro);
		}else if(single.equals("false") && args == null){
			return null;
			//return ClassLoaderFactory.loadClassByConstructor();
		}else{
			return null;
		}
	}

	public Properties parse(String nodeName){
		Properties p = new Properties();

		Node node = doc.getElementsByTagName(nodeName).item(0);

		NamedNodeMap attributes = node.getAttributes();

		for(int i = 0; i < attributes.getLength(); i++){
			Node attribute = attributes.item(i);
			String key = attribute.getNodeName();
			String value = attribute.getNodeValue();
			p.setProperty(key,value);
		}

		NodeList nodeList = node.getChildNodes();
		
		for(int i = 0; i < nodeList.getLength(); i++){
			Node element = nodeList.item(i);
			if(element instanceof Element){
				
				String key = element.getNodeName();
				String value = element.getFirstChild().getNodeValue();
				
				p.setProperty(key,value);
			}
		}
		
		return p;
	}

}


class ClassLoaderFactory{
	public static Object loadClassByMethodArgs(String methodName,Properties pro){
		try{
			ClassLoader cl = ClassLoader.getSystemClassLoader();
			String className = pro.getProperty("className");
			Class c = cl.loadClass(className);
			Method method = c.getMethod(methodName,new Class[]{Properties.class});

			return method.invoke(cl,new Object[]{pro});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public static Object loadClassByConstructorArgs(Properties pro){
		try{
			ClassLoader cl = ClassLoader.getSystemClassLoader();
			String className = pro.getProperty("className");
			Class c = cl.loadClass(className);
			Constructor ct = c.getConstructor(new Class[]{Properties.class});

			return ct.newInstance(new Object[]{pro});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}

⌨️ 快捷键说明

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