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

FileManager类, 续:
//加载配置文件配置信息
	private Properties loadProperties()
	{
		Properties prop = new Properties();
		try
		{
			if (configFile != null)
			{
				FileInputStream in = new FileInputStream(configFile);
				prop.load(in);
			}
			else
			{
				configFile = defaultConfigFile;
				URL url = this.getClass().getClassLoader().getResource(defaultConfigFile);	
				prop.load(new InputStreamReader(url.openStream()));
			}
			
			//print all properties
			Enumeration<?> enu = prop.propertyNames();
			logger.info("Load configuration file[" + configFile + "] start...");
			while (enu.hasMoreElements())
			{
				String key = (String)enu.nextElement();
	        	logger.info(" key: " + key + ",     value: " + prop.getProperty(key));
			}
			logger.info("Load configuration file[" + configFile + "] success.");
			return prop;
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return prop;
	}
	
	//对外开放的方法,可以取到监听的文件配置数据
	public static String getProperty(String fileName, String key)
	{
        //如果size为0,则程序没有启动,需要先启动程序
        if (FMap.size() == 0)
        {
            synchronized(FileManager.class)
            {
                if (FMap.size() == 0)
                {
                    //启动监听
                    startService();
                }
            }
        }
		Object obj = FMap.get(fileName);
		if (obj != null)
		{
			FileListener listener = (FileListener)obj;
			
			if (listener.isFileFlag())
			{
				return listener.getProperties().getProperty(key);
			}
			else
			{
				return listener.getTransferValue(key);
			}
//			Properties prop = ((FileListener)obj).getProperties();
//			return prop.getProperty(key);
		}
		else
		{
			return null;
		}
	}
	
		
	//提供给ESB使用的方法,取ESB配置文件数据
	public static String getESBProperty(String key)
	{
		return getProperty("esb.properties", key);
	}
	
	public static String getTransferValue(String type,String value)
	{
		StringBuffer buf = new StringBuffer(type).append("||")
		                                         .append(XMLParserHelper.ORIGINALL_VALUE)
		                                         .append("||")
		                                         .append(value);
		return getProperty(ENUMERATE,buf.toString());
	}
	
	public static String getOriginallyValue(String type,String value)
	{
		StringBuffer buf = new StringBuffer(type).append("||")
											     .append(XMLParserHelper.TRANSFER_VALUE)
											     .append("||")
											     .append(value);
		return getProperty(ENUMERATE,buf.toString());
	}
	
	
	//启动监听服务
	private static void startService()
	{
		FileManager fm = new FileManager();
		fm.regFileListener();
	}
}

你可能感兴趣的:(xml)