JAVAプロジェクトはjdomを使用して、設定ファイル類を初期化します.


もっと読む
 単一例モードロードプロファイルクラス
 
使用フレームバッグ:jdom-2.05.jar、log 4 j-1.17.jar
コード:
package com.wjq.project.loadFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;


/**
 * @Title:JAVA     xml     
 * @Description:   jdom  xml    ;           ;           ,    
 * @author wjq
 * @date 2013-07-19
 */
public class LoadXmlFile_jdom {
	//  
	private static Logger log = Logger.getLogger(LoadXmlFile_jdom.class);
	//    
	private static final String CONFIG_FILE = "loadFile_loadXmlFile.xml";
	
	//dom4j   
	private Document document=null;
	
	//IP  
	private String ip = null;
	//   
	private String port = null;
	//          ,   :12,  :  
	private int reloadTime=12;
	//      
	private Map alarmLevelsMap=new HashMap();
	//        
	private Map sheetStatusTransMap=new HashMap();
	
	
	/////////////////////////////////////////////	     start	/////////////////////////////////////////////
	/**
	 *        
	 */
	private LoadXmlFile_jdom(){
		log.info("      : "+CONFIG_FILE);
		loadConfig();
		
		//             
		Thread reloadConfFile = new Thread(new ReloadConfFile());
		reloadConfFile.start();
	}

	/**
	 *      ,       
	 * @author wjq
	 *
	 */
	public static class ClassInstance{
		//          LoadXmlFile  
		private static final LoadXmlFile_jdom instance=new LoadXmlFile_jdom();
	}
	
	/**
	 *            
	 * @return
	 */
	public static LoadXmlFile_jdom getInstance(){
		return ClassInstance.instance;
	}
	/////////////////////////////////////////////	     end	/////////////////////////////////////////////
	
	

	/**
     *         
     */
    private void loadConfig() {
    	try {
    		InputStream inputStream = LoadXmlFile_jdom.class.getResourceAsStream("/project/"+CONFIG_FILE);
        	if(inputStream!=null){
        		SAXBuilder builder = new SAXBuilder();
        		this.document = builder.build(inputStream);
    			if(document==null){
    				return;
    			}
    			
    			this.ip=loadSingleNode("myProject/ip");
    			this.port=loadSingleNode("myProject/port");
    			this.reloadTime=Integer.valueOf(loadSingleNode("myProject/reloadTime"));
    			this.alarmLevelsMap=loadAttributeMap("myProject/alarmLevels");
    			this.sheetStatusTransMap=loadTextMap("myProject/sheetStatusTrans");
        	}else{
        		log.error("       ,          ["+CONFIG_FILE+"]    [project]   ");
        	}
		} catch (Exception e) {
			log.error("      ["+CONFIG_FILE+"]  ",e);
		}
    }
    
    /**
	 * ReloadConfFile thread
	 *       xml       
	 *     12      xml    ,
	 * @author wjq
	 */
	private class ReloadConfFile implements Runnable {
		public void run() {
			// TODO Auto-generated method stub
			while(true) {
				try {
					Thread.sleep(reloadTime*60*60*1000);
				} catch (InterruptedException e) {
				}
				log.info("        :"+CONFIG_FILE);
				loadConfig();
			}
		}
	}
	
    
    /**
     *           
     * @param nodePath
     * @return
     */
    private String loadSingleNode(String nodePath){
    	Element root = document.getRootElement();
		String[] elementsName = nodePath.split("\\/");
		for (int i = 0; i < elementsName.length; i++) {
			root = root.getChild(elementsName[i]);
		}
	
		String nodeText =root.getTextTrim();
		log.info(root.getName()+"="+nodeText);
		return nodeText;
    }
    
    
    
    /**
     *   from、to             
     * from key、to value
     * @param nodePath     
     * @return
     */
	private Map loadAttributeMap(String nodePath){
		Map fromToMap=new HashMap();
		//    
		String logMessage="";
		
		//   Map  
		fromToMap.clear();
		Element root = document.getRootElement();
		String[] elementsName = nodePath.split("\\/");
		for (int i = 0; i < elementsName.length; i++) {
			root = root.getChild(elementsName[i]);
		}
		logMessage=root.getName()+":[";
		
		List childrenList = root.getChildren();
		Iterator iter=childrenList.iterator();
		while(iter.hasNext()) {
			Element el=(Element)iter.next();
			String key= el.getAttributeValue("from").trim();
			String value= el.getAttributeValue("to").trim();
			fromToMap.put(key, value);
			logMessage=logMessage+";from="+key+",to="+value;
		}
		
		logMessage=logMessage.replaceFirst(";", "")+"]";
		log.info(logMessage);
		
		return fromToMap;
	}
	
	/**
	 *   from、to          
	 * from key、to value
	 * @param nodePath     
	 * @return
	 */
	private Map loadTextMap(String nodePath){
		Map fromToMap=new HashMap();
		//    
		String logMessage="";
		
		//   Map  
		fromToMap.clear();
		Element root = document.getRootElement();
		String[] elementsName = nodePath.split("\\/");
		for (int i = 0; i < elementsName.length; i++) {
			root = root.getChild(elementsName[i]);
		}
		logMessage=root.getName()+":[";
		
		List childrenList = root.getChildren();
		Iterator iter=childrenList.iterator();
		while(iter.hasNext()) {
			Element el=(Element)iter.next();
			String key= el.getChild("from").getTextTrim();
			String value= el.getChild("to").getTextTrim();
			fromToMap.put(key, value);
			logMessage=logMessage+";from="+key+",to="+value;
		}
		
		logMessage=logMessage.replaceFirst(";", "")+"]";
		log.info(logMessage);
		
		return fromToMap;
	}
    

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}

	public Map getAlarmLevelsMap() {
		return alarmLevelsMap;
	}

	public void setAlarmLevelsMap(Map alarmLevelsMap) {
		this.alarmLevelsMap = alarmLevelsMap;
	}

	public Map getSheetStatusTransMap() {
		return sheetStatusTransMap;
	}

	public void setSheetStatusTransMap(Map sheetStatusTransMap) {
		this.sheetStatusTransMap = sheetStatusTransMap;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("  jdom  xml    ");
		LoadXmlFile_jdom loadXmlFile=LoadXmlFile_jdom.getInstance();
	}
}
 
 
設定ファイル:


	
		
		127.0.0.1
		
		51088

		
		
			
			
			
			
		

		
		
			
				  1
			
			
				     2
			
			
				     3
			
			
				     4
			
		
		
		
		12