com.tonbeller.tbutils.res
Class Resources

java.lang.Object
  extended bycom.tonbeller.tbutils.res.Resources

public class Resources
extends java.lang.Object

Facade to access key/value pairs and application home directory.

Author:
av

Method Summary
 void dump(Dumper d)
          returns all key/value pairs for logging / debug
 void flush()
          commits changes into the storage.
 boolean getBoolean(java.lang.String key)
          looks up a boolean value
 double getDouble(java.lang.String key)
          looks up a double value
 java.io.File getHomeDir()
          returns the applications "home" directory which may contain properties files and other resources.
 int getInteger(java.lang.String key)
          looks up an integervalue
 java.util.Locale getLocale()
          returns the locale for this instance
 long getLong(java.lang.String key)
          looks up a long value
 boolean getOptionalBoolean(java.lang.String key, boolean defaultValue)
           
 double getOptionalDouble(java.lang.String key, double defaultValue)
           
 int getOptionalInteger(java.lang.String key, int defaultValue)
           
 long getOptionalLong(java.lang.String key, long defaultValue)
           
 java.lang.String getOptionalString(java.lang.String key, java.lang.String defaultValue)
          looksup a string value.
 java.util.List getProviders()
          returns the list of ResourceProvider's that this instance searches for properties.
 java.lang.String getString(java.lang.String key)
          looks up a string value.
 java.lang.String getString(java.lang.String key, java.lang.Object arg)
          looks up a string value and formats via MessageFormat
 java.lang.String getString(java.lang.String key, java.lang.Object[] args)
          looks up a string value and formats via MessageFormat
 java.lang.String getString(java.lang.String key, java.lang.Object arg0, java.lang.Object arg1)
          looks up a string value and formats via MessageFormat
static Resources instance()
          returns a Resources instance in the default locale of the VM.
static Resources instance(java.lang.Class clazz)
          returns a Resources instance in the default locale.
static Resources instance(java.util.Locale browserLocale)
          returns a Resources instance in the configured locale, or if no locale was configured, in the browsers locale.
static Resources instance(java.util.Locale browserLocale, java.lang.Class clazz)
          returns a Resources instance in the configured locale, or if no locale was configured, in the browsers locale.
static Resources instance(java.util.Locale browserLocale, java.lang.String bundleName)
          returns a Resources instance in the configured locale, or if no locale was configured, in the browsers locale.
 java.util.Collection keySet()
          returns a collection of all known keys.
 void removePersistent(java.lang.String key)
           
 java.lang.String replace(java.lang.String s)
          scans s for resource keys and replaces the keys by their values.
 void setPersistentBoolean(java.lang.String key, boolean value)
          stores a key/value pair that will be persisted in the filesystem.
 void setPersistentDouble(java.lang.String key, double value)
          stores a key/value pair that will be persisted in the filesystem.
 void setPersistentInteger(java.lang.String key, int value)
          stores a key/value pair that will be persisted in the filesystem.
 void setPersistentLong(java.lang.String key, long value)
          stores a key/value pair that will be persisted in the filesystem.
 void setPersistentString(java.lang.String key, java.lang.String value)
          stores a key/value pair that will be persisted in the filesystem.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

instance

public static Resources instance()
returns a Resources instance in the default locale of the VM. This is NOT the locale of the users browser etc.


instance

public static Resources instance(java.util.Locale browserLocale)
returns a Resources instance in the configured locale, or if no locale was configured, in the browsers locale.


instance

public static Resources instance(java.util.Locale browserLocale,
                                 java.lang.Class clazz)
returns a Resources instance in the configured locale, or if no locale was configured, in the browsers locale.

If the package of clazz contains a ResourceBundle named resources.properties that bundle will be added to the returned Resources.


instance

public static Resources instance(java.lang.Class clazz)
returns a Resources instance in the default locale.

If the package of clazz contains a ResourceBundle named resources.properties that bundle will be added to the returned Resources.


instance

public static Resources instance(java.util.Locale browserLocale,
                                 java.lang.String bundleName)
returns a Resources instance in the configured locale, or if no locale was configured, in the browsers locale.

The named ResourceBundle will be added to the returned Resources.


getOptionalString

public java.lang.String getOptionalString(java.lang.String key,
                                          java.lang.String defaultValue)
looksup a string value.

Parameters:
key - key in resourcebundle
defaultValue - returnvalue if key was not found
Returns:
resource value or defaultValue

getString

public java.lang.String getString(java.lang.String key)
                           throws java.util.MissingResourceException
looks up a string value.

Throws:
java.util.MissingResourceException - if key was not found

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.Object arg)
                           throws java.util.MissingResourceException
looks up a string value and formats via MessageFormat

Throws:
java.util.MissingResourceException

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.Object arg0,
                                  java.lang.Object arg1)
                           throws java.util.MissingResourceException
looks up a string value and formats via MessageFormat

Throws:
java.util.MissingResourceException

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.Object[] args)
                           throws java.util.MissingResourceException
looks up a string value and formats via MessageFormat

Throws:
java.util.MissingResourceException

getBoolean

public boolean getBoolean(java.lang.String key)
                   throws java.util.MissingResourceException
looks up a boolean value

Throws:
java.util.MissingResourceException

getOptionalBoolean

public boolean getOptionalBoolean(java.lang.String key,
                                  boolean defaultValue)

getInteger

public int getInteger(java.lang.String key)
               throws java.util.MissingResourceException
looks up an integervalue

Throws:
java.util.MissingResourceException

getOptionalInteger

public int getOptionalInteger(java.lang.String key,
                              int defaultValue)

getLong

public long getLong(java.lang.String key)
             throws java.util.MissingResourceException
looks up a long value

Throws:
java.util.MissingResourceException

getOptionalLong

public long getOptionalLong(java.lang.String key,
                            long defaultValue)

getDouble

public double getDouble(java.lang.String key)
                 throws java.util.MissingResourceException
looks up a double value

Throws:
java.util.MissingResourceException

getOptionalDouble

public double getOptionalDouble(java.lang.String key,
                                double defaultValue)

keySet

public java.util.Collection keySet()
returns a collection of all known keys. The list maybe incomplete eg. JNDI keys are not returned.


getLocale

public java.util.Locale getLocale()
returns the locale for this instance


getHomeDir

public java.io.File getHomeDir()
returns the applications "home" directory which may contain properties files and other resources.


setPersistentString

public void setPersistentString(java.lang.String key,
                                java.lang.String value)
stores a key/value pair that will be persisted in the filesystem.

See Also:
flush()

setPersistentBoolean

public void setPersistentBoolean(java.lang.String key,
                                 boolean value)
stores a key/value pair that will be persisted in the filesystem.

See Also:
flush()

setPersistentInteger

public void setPersistentInteger(java.lang.String key,
                                 int value)
stores a key/value pair that will be persisted in the filesystem.

See Also:
flush()

setPersistentLong

public void setPersistentLong(java.lang.String key,
                              long value)
stores a key/value pair that will be persisted in the filesystem.

See Also:
flush()

setPersistentDouble

public void setPersistentDouble(java.lang.String key,
                                double value)
stores a key/value pair that will be persisted in the filesystem.

See Also:
flush()

removePersistent

public void removePersistent(java.lang.String key)

flush

public void flush()
commits changes into the storage.


dump

public void dump(Dumper d)
returns all key/value pairs for logging / debug

See Also:
StringDumper

replace

public java.lang.String replace(java.lang.String s)
scans s for resource keys and replaces the keys by their values.

s may contain resource keys in $-notation like ant, for example ${java.io.tmpdir}. If the property exists, its replaced by its value, e.g. /tmp. If the property does not exsist, the string is not changed, e.g. it remains ${java.io.tmpdir}. A literal ${ is written as $${ (like ant), i.e. $${ is replaced by ${

Example:

   Resources res = Resources.instance();
   String s = res.replace("${java.io.tmpdir}/mylogs");
   // s = "c:\\temp/mylogs" for example
   File myLogs = new File(s);
 

Property values are replaced automatically, e.g. if the properties file contains

 mylogs = ${java.io.tmpdir}/mylogs
 
then Resources.instance().getString("mylogs") will return "c:\\temp/mylogs".


getProviders

public java.util.List getProviders()
returns the list of ResourceProvider's that this instance searches for properties. The list may be modified.

See Also:
ResourceProvider