javatools.util
Class Props

java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--java.util.Hashtable
              |
              +--java.util.Properties
                    |
                    +--javatools.util.Props
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, Resetable, java.io.Serializable

public class Props
extends java.util.Properties
implements Resetable

An extended version of Properties class. It allows reading Properties files from the classpath or resource file. It uses ClassLoader.getResourceAsStream to find a properties file, so that means it either has to be on the classpath or bundled into the jar.

It is intended that you only pass a single string to find the properties like "foo". That will cause Props to search for a file called foo.properties. The directory it searches in will be by default the "res/" directory. That means that it will search your classpath or jar file for a file res/foo.properties. If you run your software in different environments you can pass a system property to the java virtual machine called "environment". In that case the searching will descend another directory level. e.g. if you pass -Denvironment=development, then it will first look for a file res/development/foo.properties. If you are using servlets your servlet engine should have a means to pass in system variables.

This makes it easy to integrate properties into your code. All you do is use Props to find a certain class of property, like "foo" and it will take care of where to physically find the file. And you can set up different properties for different environments.

TODO: make it combine the environment and non-environment specific properties into the one set.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.Properties
defaults
 
Method Summary
 java.lang.String getFullName()
          Returns the full name for this Props.
static java.lang.String getLocalConfigDir()
          Returns the local configuration directory.
 void load()
          Loads properties.
 void loadLocalConfig()
          Loads local configuration file.
 void loadResources()
          Loads resources (aka properties).
 void reset()
          Resets all this Props.
static Props singleton(java.lang.String name)
          Returns a brand new Props object referencing properties of the given name.
 void store()
          Stores properties in a file.
 
Methods inherited from class java.util.Properties
getProperty, getProperty, list, list, load, propertyNames, save, setProperty, store
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, rehash, remove, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

singleton

public static Props singleton(java.lang.String name)
                       throws java.io.IOException
Returns a brand new Props object referencing properties of the given name.

Parameters:
name - The name of properties file to load.
Returns:
The Props object.
Throws:
java.io.IOException - If they cannot be loaded.

getLocalConfigDir

public static java.lang.String getLocalConfigDir()
Returns the local configuration directory.

Returns:
The local configuration directory path.

getFullName

public java.lang.String getFullName()
Returns the full name for this Props.

Returns:
The full name.

reset

public void reset()
           throws ResetException
Resets all this Props.

Specified by:
reset in interface Resetable
Throws:
ResetException - If something goes wrong.

load

public void load()
          throws java.io.IOException
Loads properties.

Throws:
java.io.IOException - If properties cannot be loaded.

store

public void store()
           throws java.io.IOException
Stores properties in a file.

Throws:
java.io.IOException - If properties cannot be stored.

loadLocalConfig

public void loadLocalConfig()
                     throws java.io.IOException
Loads local configuration file.

Throws:
java.io.IOException - If file cannot be found.

loadResources

public void loadResources()
                   throws java.io.IOException
Loads resources (aka properties).

Throws:
java.io.IOException - If properties cannot be loaded.