.xml正向反向取值(之二)

FileManager类:
/* 
 * Class: FileManager
 * Description: 加载并启动文件监听,并对外开放接口访问
 * Version: 1.0 
 * Author: Carson.Huang
 * Created on Dec 1, 2010 
 */
public class FileManager
{
	//注册的监听文件列表
	private static HashMap<String, FileListener> FMap = new HashMap<String, FileListener>();
	//配置文件路径(从命令行传入)
	private String configFile = null;
	//默认配置文件(从jar包中加载)
	private String defaultConfigFile = "FileListener.properties";
	
    private Logger logger = Logger.getLogger(FileManager.class);
	//所有的枚举以这个为常量为标准
    public static final String ENUMERATE="enumerateTemplet.xml";
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		FileManager fm = new FileManager();
		if (args.length > 0)
		{
			fm.configFile = args[1];
		}
		if (FMap.size() == 0)
		{
			fm.regFileListener();
			System.out.println("Execute Command success.");
		}
		else
		{
			System.out.println("The program is running. cannot run again.");			
		}
		//enumerate_Eplus.xml
		System.out.println(getTransferValue("DealerCode","ECARE"));
		System.out.println(getOriginallyValue("DealerCode","03"));
		
		System.out.println(getTransferValue("DisplayName_lang","5555"));
		System.out.println(getOriginallyValue("DisplayName_lang","1"));
	}
	
	//注册需要监听的文件(从配置文件读取配置信息)
	/**
	 * 
	 */
	public void regFileListener()
	{
		Properties prop = loadProperties();
		String num = prop.getProperty("ListenerFileNum");
		String Interval = prop.getProperty("Interval");
		if (("".equals(num)) || num == null || ("".equals(Interval)) || Interval == null)
		{
			logger.error("Configuration error, please check it! Interval: " + Interval + ", ListenerFileNum: " + num);
		}
		else
		{
			int number = Integer.parseInt(num);
			int t = Integer.parseInt(Interval);
			for (int i = 0; i < number; i++)
			{
				String filePath = prop.getProperty("ListenerFile" + (i+1));
				if (filePath != null)
				{
					FileListener fileTask = new FileListener(filePath);
					
					//第一次调用,先同步加载配置文件属性,再启动监听线程
					fileTask.loadFile();
					
					Timer timer = new Timer();
					timer.schedule(fileTask, new Date(), t);
					
					//枚举文件需要统一为enumerateTemplet.xml  做为key
					String fileName=filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
					if(fileName.equalsIgnoreCase(ENUMERATE)){
						FMap.put(fileName, fileTask);	
					}else{
						FMap.put(fileTask.getFileName(), fileTask);	
					}
					
				}
				else
				{
					logger.error("Configuration item[ListenerFileNum" + (i+1) + "[ doesn't exist. ignore...");
				}
			}
		}
	}
	
	//停止监听
	public void desFileLister()
	{
		if (FMap.size() > 0)
		{
			Iterator<String> it = FMap.keySet().iterator();
			while (it.hasNext())
			{
				String key = (String)it.next();
				((FileListener)FMap.get(key)).cancel();
			}
		}
	}
	
	

你可能感兴趣的:(xml)