package com.winsun.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * .properties文件解析工具
 * 
 * 
 * 
 */
@Slf4j
public class CommonPropertiesUtil {
    private static CommonPropertiesUtil instance = null;

    private CommonPropertiesUtil() {

    }

    public static CommonPropertiesUtil getInstance() {
        if (instance == null) {
            instance = new CommonPropertiesUtil();
        }
        return instance;
    }

    /**
     * 解析传入的文件
     * 
     * @param ls
     * @return
     */
    public static Properties parseProp(String ls) {
        Properties properties = new Properties();
        // 20081105 直接使用getInstance()取得文件的路径
        InputStream is = getInstance().getClass().getResourceAsStream(ls);
        try {
            properties.load(is);
        } catch (IOException e) {

        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {

            }
        }
        return properties;
    }

    /**
     * 直接解析.properties文件返回文件的数据结构
     * 
     * @param ls
     * @return
     */
    public static Set<java.util.Map.Entry<Object, Object>> getEntrySet(String ls) {
        return parseProp(ls).entrySet();
    }
    
    public static void main(String[] args) {
    	Set<java.util.Map.Entry<Object, Object>>  set = getEntrySet("/log4j.properties");
    	Iterator<java.util.Map.Entry<Object, Object>> itr = set.iterator();
    	while(itr.hasNext()){
    		java.util.Map.Entry<Object, Object>  obj = itr.next();
    		log.info(obj.getValue().toString());
    	}
	}

}
