uPortal 2.4.1
API Documentation

org.jasig.portal.properties
Class PropertiesManager

java.lang.Object
  extended byorg.jasig.portal.properties.PropertiesManager

public class PropertiesManager
extends java.lang.Object

Provides access to properties.

It is important to understand that usage of this class is different from what you might be used to in java.util.Properties. Specifically, when you get a Properties property, if that property is not set, the return value is NULL. However, when you call the basic getters here, if the property is not set, a RuntimeException is thrown. These methods will never return null (except if you pass in null as the default return value for the methods that take a default).

There are methods to get properties as various primitive types, int, double, float, etc. When you invoke one of these methods on a property that is found but cannot be parsed as your desired type, a RuntimeException is thrown.

There are corresponding methods which take as a second parameter a default value. These methods, instead of throwing a RuntimeException when the property cannot be found, return the default value. You can use the default value "null" to invoke getProperty() with semantics more like the java.util.Properties object. These augmented accessors which take defaults will be, I hope, especially useful in static initializers. Providing a default in your static initializer will keep your class from blowing up at initialization when your property cannot be found. This seems especially advantageous when there is a plausible default value.

This class has a comprehensive JUnit testcase. Please keep the testcase up to date with any changes you make to this class.

Since:
uPortal 2.0
Version:
$Revision: 1.3 $ $Date: 2004/09/22 19:49:42 $
Author:
Ken Weiner, kweiner@unicon.net, howard.gilbert@yale.edu, andrew.petro@yale.edu

Field Summary
static java.lang.String PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE
           
 
Constructor Summary
PropertiesManager()
           
 
Method Summary
static java.util.Set getMissingProperties()
          Get a Set of the names of properties that have been requested but were not set.
static java.lang.String getProperty(java.lang.String name)
          Returns the value of a property for a given name.
static java.lang.String getProperty(java.lang.String name, java.lang.String defaultValue)
          Get the value of the property with the given name.
static boolean getPropertyAsBoolean(java.lang.String name)
          Returns the value of a property for a given name.
static boolean getPropertyAsBoolean(java.lang.String name, boolean defaultValue)
          Get a property as a boolean, specifying a default value.
static byte getPropertyAsByte(java.lang.String name)
          Returns the value of a property for a given name as a byte
static byte getPropertyAsByte(java.lang.String name, byte defaultValue)
          Get the value of the given property as a byte, specifying a fallback default value.
static double getPropertyAsDouble(java.lang.String name)
          Returns the value of a property for a given name as a long
static double getPropertyAsDouble(java.lang.String name, double defaultValue)
          Get the value of the given property as a double.
static float getPropertyAsFloat(java.lang.String name)
          Returns the value of a property for a given name as a float
static float getPropertyAsFloat(java.lang.String name, float defaultValue)
          Get the value of the given property as a float.
static int getPropertyAsInt(java.lang.String name)
          Returns the value of a property for a given name as an int
static int getPropertyAsInt(java.lang.String name, int defaultValue)
          Get the value of a given property as an int.
static long getPropertyAsLong(java.lang.String name)
          Returns the value of a property for a given name as a long
static long getPropertyAsLong(java.lang.String name, long defaultValue)
          Get the value of the given property as a long.
static short getPropertyAsShort(java.lang.String name)
          Returns the value of a property for a given name as a short
static short getPropertyAsShort(java.lang.String name, short defaultValue)
          Returns the value of a property for a given name as a short.
static java.lang.String getPropertyUntrimmed(java.lang.String name)
          Returns the value of a property for a given name including whitespace trailing the property value, but not including whitespace leading the property value.
static java.lang.String getPropertyUntrimmed(java.lang.String name, java.lang.String defaultValue)
          Get the value of a property for the given name including any whitespace that may be at the beginning or end of the property value.
static void setProperties(java.util.Properties props)
          Setter method to set the underlying Properties.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE

public static final java.lang.String PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE
See Also:
Constant Field Values
Constructor Detail

PropertiesManager

public PropertiesManager()
Method Detail

setProperties

public static void setProperties(java.util.Properties props)
Setter method to set the underlying Properties. This is a public method to allow poor-man's static dependency injection of the Properties from wherever you want to get them. If Properties have not been injected before any accessor method is invoked, PropertiesManager will invoke loadProperties() to attempt to load its own properties. You might call this from a context listener, say. If Properties have already been loaded or injected, this method will overwrite them.

Parameters:
props - - Properties to be injected.

getProperty

public static java.lang.String getProperty(java.lang.String name)
                                    throws MissingPropertyException
Returns the value of a property for a given name. Any whitespace is trimmed off the beginning and end of the property value. Note that this method will never return null. If the requested property cannot be found, this method throws an UndeclaredPortalException.

Parameters:
name - the name of the requested property
Returns:
value the value of the property matching the requested name
Throws:
MissingPropertyException - - if the requested property cannot be found

getPropertyUntrimmed

public static java.lang.String getPropertyUntrimmed(java.lang.String name)
                                             throws MissingPropertyException
Returns the value of a property for a given name including whitespace trailing the property value, but not including whitespace leading the property value. An UndeclaredPortalException is thrown if the property cannot be found. This method will never return null.

Parameters:
name - the name of the requested property
Returns:
value the value of the property matching the requested name
Throws:
MissingPropertyException - - (undeclared) if the requested property is not found

getPropertyAsBoolean

public static boolean getPropertyAsBoolean(java.lang.String name)
                                    throws MissingPropertyException
Returns the value of a property for a given name. This method can be used if the property is boolean in nature and you want to make sure that true is returned if the property is set to "true", "yes", "y", or "on" (regardless of case), and false is returned in all other cases.

Parameters:
name - the name of the requested property
Returns:
value true if property is set to "true", "yes", "y", or "on" regardless of case, otherwise false
Throws:
MissingPropertyException - - when no property of the given name is declared.

getPropertyAsByte

public static byte getPropertyAsByte(java.lang.String name)
                              throws MissingPropertyException,
                                     BadPropertyException
Returns the value of a property for a given name as a byte

Parameters:
name - the name of the requested property
Returns:
value the property's value as a byte
Throws:
MissingPropertyException - - if the property is not set
BadPropertyException - - if the property cannot be parsed as a byte

getPropertyAsShort

public static short getPropertyAsShort(java.lang.String name)
                                throws MissingPropertyException,
                                       BadPropertyException
Returns the value of a property for a given name as a short

Parameters:
name - the name of the requested property
Returns:
value the property's value as a short
Throws:
MissingPropertyException - - if the property is not set
BadPropertyException - - if the property cannot be parsed as a short or is not set.

getPropertyAsInt

public static int getPropertyAsInt(java.lang.String name)
                            throws MissingPropertyException,
                                   BadPropertyException
Returns the value of a property for a given name as an int

Parameters:
name - the name of the requested property
Returns:
value the property's value as an int
Throws:
MissingPropertyException - - if the property is not set
BadPropertyException - - if the property cannot be parsed as an int

getPropertyAsLong

public static long getPropertyAsLong(java.lang.String name)
                              throws MissingPropertyException,
                                     BadPropertyException
Returns the value of a property for a given name as a long

Parameters:
name - the name of the requested property
Returns:
value the property's value as a long
Throws:
MissingPropertyException - - if the property is not set
BadPropertyException - - if the property cannot be parsed as a long

getPropertyAsFloat

public static float getPropertyAsFloat(java.lang.String name)
                                throws MissingPropertyException,
                                       BadPropertyException
Returns the value of a property for a given name as a float

Parameters:
name - the name of the requested property
Returns:
value the property's value as a float
Throws:
MissingPropertyException - - if the property is not set
BadPropertyException - - if the property cannot be parsed as a float

getPropertyAsDouble

public static double getPropertyAsDouble(java.lang.String name)
                                  throws MissingPropertyException,
                                         BadPropertyException
Returns the value of a property for a given name as a long

Parameters:
name - the name of the requested property
Returns:
value the property's value as a double
Throws:
MissingPropertyException - - if the property has not been set
BadPropertyException - - if the property cannot be parsed as a double or is not set.

getProperty

public static java.lang.String getProperty(java.lang.String name,
                                           java.lang.String defaultValue)
Get the value of the property with the given name. If the named property is not found, returns the supplied default value. This error handling behavior makes this method attractive for use in static initializers.

Parameters:
name - - the name of the property to be retrieved.
defaultValue - - a fallback default value which will be returned if the property cannot be found.
Returns:
the value of the requested property, or the supplied default value if the named property cannot be found.
Since:
uPortal 2.4

getPropertyUntrimmed

public static java.lang.String getPropertyUntrimmed(java.lang.String name,
                                                    java.lang.String defaultValue)
Get the value of a property for the given name including any whitespace that may be at the beginning or end of the property value. This method returns the supplied default value if the requested property cannot be found. This error handling behavior makes this method attractive for use in static initializers.

Parameters:
name - - the name of the requested property
defaultValue - - a default value to fall back on if the property cannot be found
Returns:
the value of the property with the given name, or the supplied default value if the property could not be found.
Since:
uPortal 2.4

getPropertyAsBoolean

public static boolean getPropertyAsBoolean(java.lang.String name,
                                           boolean defaultValue)
Get a property as a boolean, specifying a default value. If for any reason we are unable to lookup the desired property, this method returns the supplied default value. This error handling behavior makes this method suitable for calling from static initializers.

Parameters:
name - - the name of the property to be accessed
defaultValue - - default value that will be returned in the event of any error
Returns:
the looked up property value, or the defaultValue if any problem.
Since:
uPortal 2.4

getPropertyAsByte

public static byte getPropertyAsByte(java.lang.String name,
                                     byte defaultValue)
Get the value of the given property as a byte, specifying a fallback default value. If for any reason we are unable to lookup the desired property, this method returns the supplied default value. This error handling behavior makes this method suitable for calling from static initializers.

Parameters:
name - - the name of the property to be accessed
defaultValue - - the default value that will be returned in the event of any error
Returns:
the looked up property value, or the defaultValue if any problem.
Since:
uPortal 2.4

getPropertyAsShort

public static short getPropertyAsShort(java.lang.String name,
                                       short defaultValue)
Returns the value of a property for a given name as a short. If for any reason the property cannot be looked up as a short, returns the supplied default value. This error handling makes this method a good choice for static initializer calls.

Parameters:
name - - the name of the requested property
defaultValue - - a default value that will be returned in the event of any error
Returns:
the property value as a short or the default value in the event of any error
Since:
uPortal 2.4

getPropertyAsInt

public static int getPropertyAsInt(java.lang.String name,
                                   int defaultValue)
Get the value of a given property as an int. If for any reason the property cannot be looked up as an int, returns the supplied default value. This error handling makes this method a good choice for static initializer calls.

Parameters:
name - - the name of the requested property
defaultValue - - a fallback default value for the property
Returns:
the value of the property as an int, or the supplied default value in the event of any problem.
Since:
uPortal 2.4

getPropertyAsLong

public static long getPropertyAsLong(java.lang.String name,
                                     long defaultValue)
Get the value of the given property as a long. If for any reason the property cannot be looked up as a long, returns the supplied default value. This error handling makes this method a good choice for static initializer calls.

Parameters:
name - - the name of the requested property
defaultValue - - a fallback default value that will be returned if there is any problem
Returns:
the value of the property as a long, or the supplied default value if there is any problem.
Since:
uPortal 2.4

getPropertyAsFloat

public static float getPropertyAsFloat(java.lang.String name,
                                       float defaultValue)
Get the value of the given property as a float. If for any reason the property cannot be looked up as a float, returns the supplied default value. This error handling makes this method a good choice for static initializer calls.

Parameters:
name - - the name of the requested property
defaultValue - - a fallback default value that will be returned if there is any problem
Returns:
the value of the property as a float, or the supplied default value if there is any problem.
Since:
uPortal 2.4

getPropertyAsDouble

public static double getPropertyAsDouble(java.lang.String name,
                                         double defaultValue)
Get the value of the given property as a double. If for any reason the property cannot be looked up as a double, returns the specified default value. This error handling makes this method a good choice for static initializer calls.

Parameters:
name - - the name of the requested property
defaultValue - - a fallback default value that will be returned if there is any problem
Returns:
the value of the property as a double, or the supplied default value if there is any problem.
Since:
uPortal 2.4

getMissingProperties

public static java.util.Set getMissingProperties()
Get a Set of the names of properties that have been requested but were not set.

Returns:
a Set of the String names of missing properties.
Since:
uPortal 2.4

uPortal 2.4.1
API Documentation