package com.platform.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

public class ConfigLoder {
	private String configPath;
	private volatile static ConfigLoder configLoder;

	private ConfigLoder(String configPath) {
		this.configPath = configPath;
	}

	public static ConfigLoder Builder(String configPtah) {
		if (null == configLoder) {
			synchronized (ConfigLoder.class) {
				if (null == configLoder)
					configLoder = new ConfigLoder(configPtah);
			}
		}
		return configLoder;
	}

	/**
	 * 读取文Property中的属性值
	 * 
	 * @param key
	 * @return
	 */
	public String getProperty(String key) {
		String value = null;
		if (StringUtils.isEmpty(key))
			return value;

		try {
			InputStream is = new BufferedInputStream(new FileInputStream(
					new File(configPath)));
			Properties properties = new Properties();
			properties.load(is);
			value = properties.getProperty(key);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return value;
	}

	public Properties getProperties() {
		Properties properties = null;
		try {
			InputStream is;
			properties = new Properties();
			is = new BufferedInputStream(new FileInputStream(new File(
					configPath)));
			properties.load(is);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return properties;
	}

	public String getConfigPath() {
		return configPath;
	}

	public void setConfigPath(String configPath) {
		this.configPath = configPath;
	}

}