VMwareConnectionProperty.java 4.81 KB
package com.sitech.ismp.coll.host;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 
 * <p>
 * Title: VMwareConnectionProperty
 * </p>
 * <p>
 * Description: VMware连接-xml文件加载工具类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2013
 * </p>
 * <p>
 * Company: SI-TECH
 * </p>
 * 
 * @author huojla
 * @version 1.0
 * @createtime 2013-7-8 上午11:01:01
 * 
 */
public class VMwareConnectionProperty {
//	private final static Logger logger = LoggerFactory.getLogger(VMwareConnectionProperty.class.getName());
    private Logger logger = Logger.getLogger(CollEsxiWithCMD.class);
	private Map<String, String> map = new HashMap<String, String>();
	private Properties prop = new Properties();
	private String resourceFile;

	public VMwareConnectionProperty(String resourceFile) {
		this.resourceFile = resourceFile;
		try {
//            System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"\").toString() is: " + Thread.currentThread().getContextClassLoader().getResource("").toString());
//            logger.info("Thread.currentThread().getContextClassLoader().getResource(\"\").toString() is: " + Thread.currentThread().getContextClassLoader().getResource("").toString());
            prop.loadFromXML(Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceFile));
//            prop.loadFromXML(new FileInputStream(new File(resourceFile)));
			Enumeration<?> enumeration = prop.propertyNames();
			while (enumeration.hasMoreElements()) {
				String key = (String) enumeration.nextElement();
				map.put(key, (String) prop.get(key));
			}
		} catch (FileNotFoundException e) {
			logger.error("FileNotFoundException:" + e.getMessage());
		} catch (IOException e) {
			logger.error("IOException:" + e.getMessage());
		}
	}

	/**
	 * 
	 * @Title: getString
	 * @Description: 通过key值获取配置文件中对应的值
	 * @param
	 * @return String
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-7-7 下午3:28:02
	 */
	public String getString(String key) {
		return getString(key, null);
	}

	/**
	 * 
	 * @Title: getString
	 * @Description: 通过key值获取配置文件中对应的值,若不存在,则给予默认值
	 * @param
	 * @return String
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-7-7 下午3:27:50
	 */
	public String getString(String key, String defaultValue) {
		String value = (String) map.get(key);
		if (value != null) {
			return value;
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 * @Title: getInt
	 * @Description: 获取整数值
	 * @param
	 * @return int
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-7-7 下午3:27:20
	 */
	public int getInt(String key) {
		return getInt(key, 0);
	}

	/**
	 * 
	 * @Title: getInt
	 * @Description: 获取整数值,若无,则赋予默认值
	 * @param
	 * @return int
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-7-7 下午3:27:08
	 */
	public int getInt(String key, int defaultValue) {
		String value = (String) map.get(key);
		if (value != null) {
			return Integer.parseInt(value);
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 * @Title: getInt
	 * @Description: 获取整数值
	 * @param
	 * @return int
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-7-7 下午3:27:20
	 */
	public Boolean getBoolean(String key) {
		return getBoolean(key, false);
	}

	/**
	 * 
	 * @Title: getBoolean
	 * @Description: 获取布尔值,若无,则赋予默认值
	 * @param
	 * @return int
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-7-7 下午3:27:08
	 */
	public Boolean getBoolean(String key, Boolean defaultValue) {
		String value = (String) map.get(key);
		if (value != null) {
			return Boolean.parseBoolean(value);
		} else {
			return defaultValue;
		}
	}

	/**
	 * 
	 * @Title: setPropValue
	 * @Description: 添加配置文件属性值
	 * @param
	 * @return void
	 * @throws
	 * @author huojla
	 * @version 1.0
	 * @createtime 2013-7-7 下午3:26:21
	 */
	public void setPropValue(String key, String value) {
		prop.setProperty(key, value);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(resourceFile);
			prop.store(fos, "Monitor Agent Configration");
			fos.close();
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
		} catch (IOException ie) {
			logger.error(ie.getMessage());
		}
		map.put(key, value);
	}

	/**
	 * @return the map
	 */
	public Map<String, String> getMap() {
		return map;
	}

	/**
	 * @return the resourceFile
	 */
	public String getResourceFile() {
		return resourceFile;
	}

    public static void main(String[] args) {
        VMwareConnectionProperty vm = new VMwareConnectionProperty("vcenter-config.xml");
        System.out.println(vm.getMap());
    }
}