Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.jasig.portal.properties
Class PropertiesManager

    java.lang.Object
      org.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.4, this class existed in the main package since uPortal 2.0
Version:
  $Revision: 1.7 $ $Date: 2005/04/19 01:19:28 $
Author:
  Ken Weiner, kweiner@unicon.net, howard.gilbert@yale.edu, andrew.petro@yale.edu

Field Summary
`protected static org.apache.commons.logging.Log` | `log`
`private static java.util.Set` | `missingProperties` A set of the names of properties that clients of this class attempt to access but which were not set in the properties file.
`private static java.lang.String` | `PORTAL_PROPERTIES_FILE_NAME`
`static java.lang.String` | `PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE`
`private static java.util.Properties` | `props`

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.
`protected static void` | `loadProps()` Load up the portal properties.
`private static boolean` | `registerMissingProperty(java.lang.String name)` Registers that a given property was sought but not found.
`static void` | `setProperties(java.util.Properties props)` Setter method to set the underlying Properties.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### log

    protected static final org.apache.commons.logging.Log log

### PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE

    public static final java.lang.String PORTAL_PROPERTIES_FILE_SYSTEM_VARIABLE

See Also:
  Constant Field Values

### PORTAL_PROPERTIES_FILE_NAME

    private static final java.lang.String PORTAL_PROPERTIES_FILE_NAME

See Also:
  Constant Field Values

### props

    private static java.util.Properties props

### missingProperties

    private static final java.util.Set missingProperties

  A set of the names of properties that clients of this class attempt to access but which were not set in the properties file. This Set allows this class to report about missing properties and to log each missing property only the first time it is requested.

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.

### loadProps

    protected static void loadProps()

  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.

### 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.

### registerMissingProperty

    private static boolean registerMissingProperty(java.lang.String name)

  Registers that a given property was sought but not found. Currently adds the property to the set of missing properties and logs if this is the first time the property has been requested.

  Parameters:
    `name` - - the name of the missing property
  Returns:
    true if the property was previously registered, false otherwise

### 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

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD