FileDocCategorySizeDatePackage
PropertyWrapper.javaAPI DocGlassfish v2 API12085Fri May 04 22:32:08 BST 2007com.sun.enterprise.util

PropertyWrapper

public class PropertyWrapper extends Object implements Serializable

$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/PropertyWrapper.java,v $
author
$Author: tcfujii $
version
$Revision: 1.4 $ $Date: 2007/05/05 05:32:07 $

Fields Summary
static Logger
_logger
protected boolean
bTryDefaultFileOnEnvFailure
try a defauly property file if env name fails?
private boolean
bLoaded
maintains a flag to know if the properties were loaded successfully
protected boolean
bDebug
debug flag
protected String
properties_file_name
The name of the properties file.
protected Properties
props
The bucket to hold the properties.
Constructors Summary
public PropertyWrapper(String propertiesFileName, String envPropName, boolean bTryDefaultFileOnEnvFailure)
The constructor loads the properties from a file. The name of the file can be specified in two ways. If an environment property name exists, it would be the first to be given a shot. if the properties are not loaded and flag 'bTryDefaultFileOnEnvFailure' is true, a default filename in the propertiesFileName parameter will be given a shot.

param
propertiesFileName The name of the file holding the properties
param
envPropName Environment name for the property
param
bTryDefaultFileOnEnvFailure try a defauly property file if env name fails?


		                                                                     			                               	
	      
	
	    // First try to see if you can load properties vai the env variable
	    try
	    {
	        if(null!=envPropName)
	        {
	            SecurityManager sm = System.getSecurityManager();
	            if(null!=sm)
	                sm.checkPropertyAccess(envPropName);
	            String envPropFile = System.getProperty(envPropName);
	            //if(bDebug) System.out.println("envname=" + envPropName + ",envPropName=" + envPropFile);
//Bug 4677074 begin
		    //if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"envname=" + envPropName + ",envPropName=" + envPropFile);
//Bug 4677074 end
	            if(null!=envPropFile)
	            {
	                properties_file_name = envPropFile;
                    try
		            {
			            loadProperties();
		            }catch(IOException ioe)
		            {
    		            if(com.sun.enterprise.util.logging.Debug.enabled)
//Bug 4677074    		                System.err.println("PropertyWrapper::PropertyWrapper() > " + ioe);
//Bug 4677074 begin
				_logger.log(Level.SEVERE,"enterprise_util.dbgcntl_ioexception",ioe);
//Bug 4677074 end
		            }	            
	            }
	        }
	    } catch(SecurityException se)
	    {
//Bug 4677074	        System.out.println("PropertyWrapper::PropertyWrapper() don not have security access to " + properties_file_name + " > " + se);
//Bug 4677074 begin
		_logger.log(Level.SEVERE,"iplanet_util.security_exception",new Object[]{properties_file_name,se});
//Bug 4677074 end
	    }
        // Try to load from a property file specified checking the 'bTryDefaultFileOnEnvFailure' flag
        if( !bLoaded && bTryDefaultFileOnEnvFailure && (null!=propertiesFileName) )
        {
	        try
	        {
		        properties_file_name = propertiesFileName;
		        loadProperties();
	        }catch(IOException ioe)
	        {
    		    if(com.sun.enterprise.util.logging.Debug.enabled)
//Bug 4677074    		        System.err.println("PropertyWrapper::PropertyWrapper() > " + ioe);
//Bug 4677074 begin
				_logger.log(Level.SEVERE,"enterprise_util.dbgcntl_ioexception",ioe);
//Bug 4677074 end
		    }
	    }

        if(bLoaded && bDebug)
//Bug 4677074            System.out.println("PropertyWrapper using " + properties_file_name);
//Bug 4677074 begin
	    _logger.log(Level.FINE,"PropertyWrapper using " + properties_file_name);
//Bug 4677074 end
        else if (!bLoaded && bDebug)
//Bug 4677074            System.out.println("PropertyWrapper reports properties could not be loaded for env=" + envPropName + " or filename=" + propertiesFileName);
//Bug 4677074 begin
	    _logger.log(Level.FINE,"PropertyWrapper reports properties could not be loaded for env=" + envPropName + " or filename=" + propertiesFileName);
//Bug 4677074 end
	
public PropertyWrapper(String propertiesFileName, String envPropName)
The constructor loads the properties from a file. The name of the file can be specified in two ways. If an environment property name exists, it would be the first to be given a shot, if the properties are not loaded, a default filename in the propertiesFileName parameter will be given a shot.

param
propertiesFileName The name of the file holding the properties
param
envPropName Environment name for the property

        this(propertiesFileName, envPropName, true);
    
public PropertyWrapper(String propertiesFileName)
The constructor loads the properties from a file from the classpath.

param
propertiesFileName The name of the file holding the properties

        this(propertiesFileName, null, true);
    
public PropertyWrapper(Properties props)
The constructor to initialize the object with a set of properties.

param
props The property to init the object.

		this.props=props;
	
Methods Summary
protected booleangetBooleanProperty(java.lang.String key, boolean defaultVal)
Read the string property [true | false] and convert it into a boolean.

return
boolean true or false, default if no property found

	    String str = getProperty(key);
	    if(null==str)
	        return defaultVal;
	    if(str.toLowerCase().startsWith("true"))
	        return true;
	    if(str.toLowerCase().startsWith("false"))
	        return false;
	    return defaultVal;
	
protected intgetIntProperty(java.lang.String key, int defaultVal)
Read the string property and convert it to an int.

return
int -1 on conversion failure, default if no property found, converted value otherwise

	    String str = getProperty(key);
	    if(null==str)
	        return defaultVal;
	    try
	    {
	        return Integer.parseInt(str);
	    }catch(NumberFormatException nfe)
	    {
	        return -1;
	    }
	
protected intgetIntProperty(java.lang.String key)
Read the string property and convert it to an int.

return
int -1 on failure

	    String str = getProperty(key);
	    if(null==str)
	        return -1;	  
	    try
	    {
	        return Integer.parseInt(str);
	    }catch(NumberFormatException nfe)
	    {
	        return -1;
	    }
	
protected longgetLongProperty(java.lang.String key)
Read the string property and convert it to an long.

return
long -1 on failure

	    String str = getProperty(key);
	    if (str == null)
	        return -1;	  
	    try {
	        return Long.parseLong(str);
	    } catch(NumberFormatException nfe) {
	        return -1;
	    }
	
protected longgetLongProperty(java.lang.String key, long defaultVal)
Read the string property and convert it to an long.

return
long -1 on conversion failure, default if no property found, converted value otherwise

	    String str = getProperty(key);
	    if (str == null)
	        return defaultVal;
	    try {
	        return Long.parseLong(str);
	    } catch(NumberFormatException nfe) {
	        return -1;
	    }
	
public java.lang.StringgetPropertiesFile()
Return the filename of the properties file.

return
the name of the properties file for the instance.

		return properties_file_name;
	
protected java.lang.StringgetProperty(java.lang.String key)
This method returns the value for a particular name of a property.

param
key The name stored in the properties file associated with a value.
return
The value of the key in the properties, null in case the key is not found.

		return getProperty(key, null);
	
protected java.lang.StringgetProperty(java.lang.String key, java.lang.String defaultVal)
This method returns the value for a particular name of a property with a default value supplied along.

param
key The name stored in the properties file associated with a value.
param
defaultVal The default value to be returned if the key is not found.
return
The value of the key.
see
#getProperty(java.lang.String)

		return props.getProperty(key, defaultVal);
	
protected voidloadProperties()
This method allows to load properties into the instance object, from the filename specified in the parameter of the constructor. Set flag bLoaded to true if the properties are loaded and are non empty.

exception
BaseException when an error is encountered in reading the file listing the properties.

        props = new Properties();

		try
		{
			InputStream is = ClassLoader.getSystemResourceAsStream(properties_file_name);
			props.load(is);
			if(!props.isEmpty())
			    bLoaded = true;
		}catch(IOException e)
		{
			throw e;
		}