| java.lang.Object org.jasig.portal.properties.PropertiesManager
PropertiesManager | public class PropertiesManager (Code) | | 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.
author: Ken Weiner, kweiner@unicon.net author: howard.gilbert@yale.edu author: andrew.petro@yale.edu version: $Revision: 35627 $ $Date: 2005-04-18 18:19:28 -0700 (Mon, 18 Apr 2005) $ since: uPortal 2.4, this class existed in the main package since uPortal 2.0 |
Method Summary | |
public static Set | getMissingProperties() Get a Set of the names of properties that have been requested but were not set. | public static String | getProperty(String name) Returns the value of a property for a given name. | public static String | getProperty(String name, 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. Parameters: defaultValue - - a fallback default value which will be returned if the property cannot be found. | public static boolean | getPropertyAsBoolean(String name) Returns the value of a property for a given name. | public static boolean | getPropertyAsBoolean(String name, boolean defaultValue) Get a property as a boolean, specifying a default value. | public static byte | getPropertyAsByte(String name) | public static byte | getPropertyAsByte(String name, byte defaultValue) Get the value of the given property as a byte, specifying a fallback default value. | public static double | getPropertyAsDouble(String name) | public static double | getPropertyAsDouble(String name, double defaultValue) Get the value of the given property as a double. | public static float | getPropertyAsFloat(String name) | public static float | getPropertyAsFloat(String name, float defaultValue) Get the value of the given property as a float. | public static int | getPropertyAsInt(String name) | public static int | getPropertyAsInt(String name, int defaultValue) Get the value of a given property as an int. | public static long | getPropertyAsLong(String name) | public static long | getPropertyAsLong(String name, long defaultValue) Get the value of the given property as a long. | public static short | getPropertyAsShort(String name) | public static short | getPropertyAsShort(String name, short defaultValue) Returns the value of a property for a given name as a short. | public static String | getPropertyUntrimmed(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. | public static String | getPropertyUntrimmed(String name, 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. | protected static void | loadProps() Load up the portal properties. | public static synchronized void | setProperties(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. |
PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE | final public static String PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE(Code) | | |
log | final protected static Log log(Code) | | |
getMissingProperties | public static Set getMissingProperties()(Code) | | Get a Set of the names of properties that have been requested but were not set.
a Set of the String names of missing properties. since: uPortal 2.4 |
getProperty | public static String getProperty(String name) throws MissingPropertyException(Code) | | 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 value the value of the property matching the requested name throws: MissingPropertyException - - if the requested property cannot be found |
getProperty | public static String getProperty(String name, String defaultValue)(Code) | | 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. Parameters: defaultValue - - a fallback default value which will be returned if the property cannot be found. the value of the requested property, or the supplied default value if the named property cannot be found. since: uPortal 2.4 |
getPropertyAsBoolean | public static boolean getPropertyAsBoolean(String name) throws MissingPropertyException(Code) | | 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 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. |
getPropertyAsBoolean | public static boolean getPropertyAsBoolean(String name, boolean defaultValue)(Code) | | 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 Parameters: defaultValue - - default value that will be returned in the event of any error the looked up property value, or the defaultValue if any problem. since: uPortal 2.4 |
getPropertyAsByte | public static byte getPropertyAsByte(String name, byte defaultValue)(Code) | | 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 Parameters: defaultValue - - the default value that will be returned in the event of any error the looked up property value, or the defaultValue if any problem. since: uPortal 2.4 |
getPropertyAsDouble | public static double getPropertyAsDouble(String name, double defaultValue)(Code) | | 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 Parameters: defaultValue - - a fallback default value that will be returned if there is any problem the value of the property as a double, or the supplied default value if there is any problem. since: uPortal 2.4 |
getPropertyAsFloat | public static float getPropertyAsFloat(String name, float defaultValue)(Code) | | 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 Parameters: defaultValue - - a fallback default value that will be returned if there is any problem the value of the property as a float, or the supplied default value if there is any problem. since: uPortal 2.4 |
getPropertyAsInt | public static int getPropertyAsInt(String name, int defaultValue)(Code) | | 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 Parameters: defaultValue - - a fallback default value for the property 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(String name, long defaultValue)(Code) | | 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 Parameters: defaultValue - - a fallback default value that will be returned if there is any problem the value of the property as a long, or the supplied default value if there is any problem. since: uPortal 2.4 |
getPropertyAsShort | public static short getPropertyAsShort(String name, short defaultValue)(Code) | | 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 Parameters: defaultValue - - a default value that will be returned in the event of any error the property value as a short or the default value in the event of any error since: uPortal 2.4 |
getPropertyUntrimmed | public static String getPropertyUntrimmed(String name) throws MissingPropertyException(Code) | | 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 value the value of the property matching the requested name throws: MissingPropertyException - - (undeclared) if the requested property is not found |
getPropertyUntrimmed | public static String getPropertyUntrimmed(String name, String defaultValue)(Code) | | 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 Parameters: defaultValue - - a default value to fall back on if the property cannot be found 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 |
loadProps | protected static void loadProps()(Code) | | Load up the portal properties. Right now the portal properties is a simple
.properties file with name value pairs. It may evolve to become an XML file
later on.
|
setProperties | public static synchronized void setProperties(Properties props)(Code) | | 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. |
|
|