java Properties工具

来源:互联网 发布:linux数据库备份软件 编辑:程序博客网 时间:2024/06/17 03:34
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PropertiesUtil {

    private static final Logger log = LoggerFactory.getLogger(PropertiesUtil.class);

    public static ConcurrentMap< String, String >    appExceptionMessageMap    = new ConcurrentHashMap< String, String >();

    public static ConcurrentMap<String,String> responseMessageMap = new ConcurrentHashMap<String, String>();

    public static ConcurrentMap<String,String> interfaceAuthorityMap = new ConcurrentHashMap<String, String>();

    public static ConcurrentMap<String,String> configMap = new ConcurrentHashMap<String, String>();

    static {
        init();
    }
    private static void init(){
        try{
            ResourceBundle exceptionMessage = ResourceBundle.getBundle("props/error-message");
            ResourceBundle responseMessage = ResourceBundle.getBundle("props/response-message");
            ResourceBundle interfaceAuthority = ResourceBundle.getBundle("props/interface-authority");
            ResourceBundle config = ResourceBundle.getBundle("props/config");

            initConfig(exceptionMessage,appExceptionMessageMap);
            initConfig(responseMessage,responseMessageMap);
            initConfig(interfaceAuthority,interfaceAuthorityMap);
            initConfig(config, configMap);
            
        } catch (Throwable t){
            log.error("cofig init error .............",t);
        }

    }

    private static void initConfig(ResourceBundle resourceBundle,ConcurrentMap map){
        Enumeration< String > e = resourceBundle.getKeys();
        while( e.hasMoreElements() ) {
            String key = e.nextElement();
            map.put( key, resourceBundle.getString( key ) != null ? resourceBundle.getString( key )
                    .trim() : null );
        }
    }

}
0 0
原创粉丝点击