`

Properties工具类

 
阅读更多
package org.jasig.cas.a4.tools;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UtilProperties {
	private static Map propsCache=new HashMap();
	private static Log logger=LogFactory.getLog(UtilProperties.class);
	public static Properties getProperties(String resourceName){
		Properties props=(Properties)propsCache.get(resourceName);
		UtilProperties utilProperties = new UtilProperties();
		if(props==null){
			props = new Properties();
			ClassLoader loader=null;
			if (loader == null) {
	            try {
	                loader = Thread.currentThread().getContextClassLoader();
	            } catch (SecurityException e) {
	                loader = utilProperties.getClass().getClassLoader();
	            }
	        }
			URL url=loader.getSystemResource(resourceName);
			InputStream in = null;

	        try {
	            in = url.openStream();
	            props.load(in);
	        } catch (Exception e) {
	        	try{
	        	in=utilProperties.getClass().getResourceAsStream("/"+resourceName);
	        	props.load(in);
	        	}catch(Exception e1){
	        		 logger.error(" Couldn't find the URL: " + resourceName+e1);  
	 	            return props;
	        	}
	        }
		}
		return props;
	}
	public static String getProperty(String resourceName,String key,String defaultValue){
		Properties props=getProperties(resourceName);
		return props.getProperty(key, defaultValue);
	}
	public static String getProperty(String resourceName,String key){
		return getProperty(resourceName,key, "");
	}
	public static Map getMatchPropertys(String resourceName,String inkey){
		Map result = new HashMap();
		Properties props=(Properties)propsCache.get(resourceName);
		UtilProperties utilProperties = new UtilProperties();
		if(props==null){
			props = new Properties();
			ClassLoader loader=null;
			if (loader == null) {
	            try {
	                loader = Thread.currentThread().getContextClassLoader();
	            } catch (SecurityException e) {
	                loader = utilProperties.getClass().getClassLoader();
	            }
	        }
			URL url=loader.getSystemResource(resourceName);
			InputStream in = null;

	        try {
	            in = url.openStream();
	            props.load(in);

	        } catch (Exception e) {
	        	try{
	        	in=utilProperties.getClass().getResourceAsStream("/"+resourceName);
	        	props.load(in);
	        	}catch(Exception e1){
	        		 logger.error(" Couldn't find the URL: " + resourceName+e1);  
	 	            return result;
	        	}
	        }
		}
        Enumeration en = props.propertyNames();   
        while (en.hasMoreElements()) {   
               String key = (String) en.nextElement();   
               if(key.contains(inkey)){
            	   String Property = props.getProperty (key);  
            	   result.put(key, props.getProperty(key, ""));
               }    
        }   
		return result;	
	}	
}

 

调用:

private final String baseuri = UtilProperties.getProperty("runtime.properties", "cu.cloud.v1");

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics