www.pudn.com > dataswap.rar > ReadConfig.java


package com.gemt.dataswap.resources; 
 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.dom4j.Document; 
import org.dom4j.Element; 
import org.dom4j.Node; 
import org.dom4j.io.SAXReader; 
import com.gemt.dataswap.config.DataBaseConfig; 
import com.gemt.dataswap.config.Field; 
import com.gemt.dataswap.config.TableConfig; 
import com.gemt.dataswap.util.Constants; 
/** 
 * 用来读取三个配置文件(database-config.xml, tables-config.xml, system-config.xml) 
 * 并把读取的信息保存到这个类的静态变量中, 以便后续操作直接使用配置信息 
 * @author lgl 
 * 
 */ 
public class ReadConfig 
{ 
	private static final Log log = LogFactory.getLog(ReadConfig.class); 
	public static Map tableConfig; 
	public static Map dataBaseConfig; 
	public static String outfolder; //数据导出成XML文件的存放目录 
	public static String infolder; //需要导入数据的来源目录 
	public static String okfolder; //当文件成功导入到目标数据库,则把它移动到这个目录 
	public static String failfolder; //当文件导入到目标数据库失败时, 移动到这个目录 
	public static String security = "false"; //对导出的字符串字段是否使用加密,默认不加密 
	public static Collection loadorderList; 
	 
	static { 
		if(log.isDebugEnabled()) { 
			log.debug("read config file begin"); 
		} 
		InputStream in = null; 
		Document doc = null; 
		try { 
			SAXReader reader = new SAXReader(); 
			//读取数据库配置 
			in = ReadConfig.class.getResourceAsStream(Constants.DATABASE_CONFIG); 
			if(in == null) { 
				if(log.isErrorEnabled()) 
					log.error("failure to read database config file"); 
			} 
			doc = reader.read(in); 
			dataBaseConfig = putDataBaseConfig(doc); 
			/*if(log.isDebugEnabled()) { 
				log.debug("database config content is " + doc.asXML()); 
			}*/ 
			in.close(); 
			in = null; 
			 
			//读取表配置 
			in = ReadConfig.class.getResourceAsStream(Constants.TABLES_CONFIG); 
			doc = reader.read(in); 
			if(in == null) { 
				if(log.isErrorEnabled()) 
					log.error("can read tables config"); 
			} 
			tableConfig = putTableConfig(doc); 
			 
			if(log.isDebugEnabled()) { 
				log.debug("before read system config "); 
			} 
			//读取系统配置, 主要是目录存放位置以及表的 
			in = ReadConfig.class.getResourceAsStream(Constants.SYSTEM_CONFIG); 
			doc = reader.read(in); 
			if(in == null) { 
				if(log.isErrorEnabled()) 
					log.error("failure to read system-config.xml"); 
			} 
			loadorderList = readSystemConfig(doc); 
		} 
		catch(Exception e) { 
			if(log.isErrorEnabled()) 
				log.error("a kind of configuration file can not read successfully " + e); 
		} 
		finally { 
			try { 
				if(in != null) { 
					in.close(); 
				} 
			} 
			catch(java.io.IOException ioe) { 
				if(log.isErrorEnabled()) 
					log.error("error when close inputstream " + ioe); 
			} 
		} 
		if(log.isDebugEnabled()) { 
			log.debug("read config file end"); 
		} 
	} 
	 
	/** 
	 * 从database-config.xml文件中读取配置信息, 便于后续操作根据当前读取的信息去建立相应的数据库连接 
	 * @param doc 
	 * @return 
	 */ 
	public static Map putDataBaseConfig(Document doc) { 
		if(log.isDebugEnabled()) 
			log.debug("putDataBaseConfig begin ... "); 
		List databaseList = doc.selectNodes(Constants.DATABASE_CONFIG_PATH); 
		Map dbMap = new HashMap(); 
        for(Iterator it = databaseList.iterator(); it.hasNext(); ) { 
        	DataBaseConfig dataBaseConfig = new DataBaseConfig(); 
        	Element databaseEle = (Element)it.next(); 
        	/* 
        	if("name".equals(databaseEle.attribute("name"))) 
	        System.out.println(databaseEle.attribute("type").getText() + " "); 
	         
	        if("oracle".equals(databaseEle.attribute("type").getText())) { 
	            System.out.println(databaseEle.asXML()); 
	        } 
	        */ 
	        Iterator contentEle = databaseEle.nodeIterator(); 
	        while(contentEle.hasNext()) { 
	        	Node contentNode = (org.dom4j.Node)contentEle.next(); 
	        	if(contentNode.getName() != null && !"".equals(contentNode.getName())) { 
	        		if(log.isDebugEnabled()) 
	        			log.debug("name = " + contentNode.getName() + " value = " + contentNode.getText()); 
	        		//设置值到bean对应的属性 
	        		if(Constants.DATABASE_CONFIG_DRIVER.equals(contentNode.getName())) { 
	        			dataBaseConfig.setDriverClass(contentNode.getText()); 
	        		} 
	        		else if(Constants.DATABASE_CONFIG_HOST.equals(contentNode.getName())){ 
	        				dataBaseConfig.setHost(contentNode.getText()); 
	        			 } 
	        			 else if(Constants.DATABASE_CONFIG_PORT.equals(contentNode.getName())) { 
	        				 	  dataBaseConfig.setPort(Integer.parseInt(contentNode.getText())); 
	        			 	  } 
	        			 	  else if(Constants.DATABASE_CONFIG_DBNAME.equals(contentNode.getName())) { 
	        			 		       dataBaseConfig.setDbName(contentNode.getText()); 
	        			 	       } 
	        			 	       else if(Constants.DATABASE_CONFIG_USERNAME.equals(contentNode.getName())) { 
	        			 	    	        dataBaseConfig.setUserName(contentNode.getText()); 
	        			 	            } 
	        			 	            else if(Constants.DATABASE_CONFIG_PASSWORD.equals(contentNode.getName())) { 
	        			 	    	             dataBaseConfig.setPassword(contentNode.getText());  
	        			 	                 }	        		 
	        		 
	        	} 
	        } 
	         
	        if(databaseEle.attribute("type") == null  
	        		|| databaseEle.attribute("type").getText() == null) { 
	        	if(log.isErrorEnabled()) { 
	        		log.error("please set proper driver type for db :: " + databaseEle.attribute("name").getText()); 
	        		System.exit(1); 
	        	} 
	        } 
	         
	        dataBaseConfig.setDbDialect(databaseEle.attribute("type").getText()); 
	        if(dataBaseConfig.getDriverClass() == null || "".equals(dataBaseConfig.getDriverClass())) { 
	        	if(log.isWarnEnabled()) { 
	        		log.warn("no driver class set, get default instead"); 
	        	} 
	        	if(Constants.DATABASE_ORACLE.equals(dataBaseConfig.getDbDialect())) { 
	        		dataBaseConfig.setDriverClass(Constants.DEFAULT_DBDRIVER_ORACLE); 
	        	} 
	        	else if(Constants.DATABASE_SQL_SERVER.equals(dataBaseConfig.getDbDialect())) { 
	        		dataBaseConfig.setDriverClass(Constants.DEFAULT_DBDRIVER_SQLSERVER); 
	        	} 
	        	else { 
	        		if(log.isErrorEnabled()) { 
	        			log.error("do not support this type driver now for db :: " + databaseEle.attribute("name").getText()); 
	        		} 
	        	} 
	        } 
	         
	        dbMap.put(databaseEle.attribute("name").getText(), dataBaseConfig); 
        } 
		if(log.isDebugEnabled()) 
			log.debug("putDataBaseConfig end ... ");        
        return dbMap; 
	} 
	 
	/** 
	 * 从tables-config.xml中读取数据库表的配置信息, 然后根据配置信息去解析数据的导入与导出 
	 * @param doc 
	 * @return 
	 */ 
	public static Map putTableConfig(Document doc) { 
		if(log.isDebugEnabled()) 
			log.debug("putTableConfig begin ... "); 
		List tablesList = doc.selectNodes(Constants.TABLES_CONFIG_PATH); 
		Map tablesMap = new HashMap(); 
         
		for(Iterator it = tablesList.iterator(); it.hasNext(); ) { 
			TableConfig tableConfig = new TableConfig(); 
        	Element tableEle = (Element)it.next(); 
        	tableConfig.setFromDb(tableEle.attributeValue(Constants.TABLES_CONFIG_FROM_DB)); 
        	if(tableEle.attributeValue(Constants.TABLES_CONFIG_TO_DB) != null && !"".equals(Constants.TABLES_CONFIG_TO_DB) ) { 
        		tableConfig.setToDb(tableEle.attributeValue(Constants.TABLES_CONFIG_TO_DB).split(",")); 
        	} 
        	 
        	List contentlist = tableEle.elements(); 
    		List fieldList = new ArrayList(); 
    		StringBuffer commasb = new StringBuffer(); 
    		StringBuffer fieldsb = new StringBuffer(); 
    		 
    		int i =0; 
    		//读取表的每个字段,导出数据要用到的SQL条件,导入语句要用到的SQL插入语句 
        	for(Iterator contentEle = contentlist.iterator(); contentEle.hasNext();) { 
        		Element content = (Element)contentEle.next(); 
        		 
        		if(Constants.TABLES_CONFIG_FIELD.equals(content.getName())) { 
        			if(i == 0) { 
            			commasb.append("?");        				 
            			fieldsb.append(content.attributeValue("name")); 
        			} 
        			else { 
            			commasb.append(",?");        				 
            			fieldsb.append("," + content.attributeValue("name"));        				 
        			} 
        			Field tableField = new Field(content.attributeValue("name"), content.attributeValue("type")); 
        			fieldList.add(tableField); 
	        		/*if(log.isDebugEnabled()) { 
	        			 
	        			log.debug(content.getName() + "  " + tableField);	 
	        		}*/ 
        		} 
        		else if(Constants.TABLES_CONFIG_CONDITION.equals(content.getName())){ 
        			tableConfig.setCondition(content.getText()); 
        		} 
        		i++; 
        	} 
        	 
        	final String insertSql = "insert into " + tableEle.attributeValue("name") + " (" + fieldsb.toString() + ") values (" + commasb.toString() + ")"; 
        	Field[] fieldArr = fieldList.toArray(new Field[0]); 
        	if(fieldArr == null || fieldArr.length == 0) { 
        		if(log.isErrorEnabled()) { 
        			log.error("field configuration is error at table ::: " + tableEle.attributeValue("name")); 
        		} 
        		System.exit(1); 
        	} 
        	 
        	if(tableConfig.getCondition() != null && tableConfig.getCondition().toUpperCase().indexOf("SELECT") == -1) { 
        		tableConfig.setCondition("SELECT " + fieldsb.toString() + " " + tableConfig.getCondition()); 
        	} 
        	if(log.isDebugEnabled()) { 
        		log.debug("query sql is :: " + tableConfig.getCondition()); 
        		log.debug("insert sql is :: " + insertSql); 
        	} 
        	tableConfig.setInsertSql(insertSql); 
        	tableConfig.setField(fieldArr); 
        	 
        	tablesMap.put(tableEle.attributeValue("name"), tableConfig); 
		} 
		 
		//return (Column[])keyList.toArray(new Column[0]); 
		return tablesMap; 
	} 
	/** 
	 * 读取系统的配置属性 
	 * 目前需要读取的是文件的存放路径, 以及表的插入顺序(主要是考虑存在外键约束,导致数据无法插入的情况) 
	 * @param doc 
	 * @return 
	 */ 
	public static Collection readSystemConfig(Document doc) { 
		if(log.isDebugEnabled()) 
			log.debug("readSystemConfig begin..."); 
		outfolder = doc.selectSingleNode(Constants.SYSTEM_CONFIG_OUTFOLDER_PATH).getText(); 
		infolder = doc.selectSingleNode(Constants.SYSTEM_CONFIG_INFOLDER_PATH).getText(); 
		okfolder = doc.selectSingleNode(Constants.SYSTEM_CONFIG_OKFOLDER_PATH).getText(); 
		failfolder = doc.selectSingleNode(Constants.SYSTEM_CONFIG_FAILFOLDER_PATH).getText(); 
		security = doc.selectSingleNode(Constants.SYSTEM_CONFIG_SECURITY).getText(); 
		 
		 
		 
		if(log.isDebugEnabled()) { 
			log.debug("outfolder === " + outfolder); 
			log.debug("infolder === " + infolder); 
			log.debug("okfolder === " + okfolder); 
			log.debug("failfolder === " + failfolder); 
			log.debug("security === " + security); 
		} 
		Collection tablesCol = new ArrayList(); 
		List tablesList = doc.selectNodes(Constants.SYSTEM_CONFIG_LOADORDER_TABLE_PATH); 
		for(Iterator it = tablesList.iterator(); it.hasNext();) { 
			Element table = (Element)it.next(); 
			if(log.isDebugEnabled()) { 
				log.debug("table name is " + table.getText()); 
			} 
			tablesCol.add(table.getText()); 
		} 
 
		if(log.isDebugEnabled()) 
			log.debug("readSystemConfig end..."); 
		return tablesCol;		 
	} 
	 
	/** 
	 * @param args 
	 */ 
	public static String readConfig(String name) { 
		InputStream in = ReadConfig.class.getResourceAsStream(name); 
		if(in == null) { 
			System.out.println("inputstream is null"); 
		} 
		else { 
			System.out.println("input stream is not null"); 
		} 
		return null; 
	} 
	public static void main(String[] args) 
	{ 
		//ReadConfig.readConfig(Constants.TABLES_CONFIG); 
		// TODO Auto-generated method stub 
	} 
}