Package lazyj

Class ExtProperties

All Implemented Interfaces:
Observer
Direct Known Subclasses:
DBProperties, FallbackProperties

public class ExtProperties extends Observable implements Observer
Utility class to process enhanced .properties file.

Such a file can have a special "include" key, which is a list of comma-separated file names (without the .properties extension) relative to the same base folder.

Each value can contain constructions like:
  • ${other_key} , to include the value of another key in that place
This object can monitor an underlying file for modifications and automatically reload the contents of the file when it detects changes.
Since:
2006-09-29
Author:
costing
  • Constructor Details

    • ExtProperties

      public ExtProperties()
      Default constructor, creates a configuration dictionary that has no file backing.
    • ExtProperties

      public ExtProperties(InputStream is) throws IOException
      Load the configuration from an InputStream (such as the one returned by ClassLoader.getResourceAsStream(String)
      Parameters:
      is - input stream to read from
      Throws:
      IOException - in case of problems while reading from the stream
    • ExtProperties

      public ExtProperties(String sBaseConfigDir, String sConfigFilename)
      Load the contents of a .properties file from the sConfDir path.
      Parameters:
      sBaseConfigDir - base folder for the configuration files
      sConfigFilename - file to load, without the ".properties" extension, that will be automatically added
    • ExtProperties

      public ExtProperties(String sBaseConfigDir, String sConfigFilename, ExtProperties superProperties)
      Load the contents of a .properties file from the sConfDir path. If the pSuper parameter is not null then the values from the given ExtProperties are inherited. Any key defined in this class will override any inherited key.
      Parameters:
      sBaseConfigDir - base folder for all the configuration files
      sConfigFilename - file to load (without the ".properties" extension)
      superProperties - other configuration to use as default (the keys from the current file will override the ones given as defaults)
    • ExtProperties

      public ExtProperties(String sBaseConfigDir, String sConfigFilename, ExtProperties superProperties, boolean ignoreMissingIncludes)
      Load the contents of a .properties file from the sConfDir path. If the pSuper parameter is not null then the values from the given ExtProperties are inherited. Any key defined in this class will override any inherited key.
      Parameters:
      sBaseConfigDir - base folder for all the configuration files
      sConfigFilename - file to load (without the ".properties" extension)
      superProperties - other configuration to use as default (the keys from the current file will override the ones given as defaults)
      ignoreMissingIncludes - whether or not to ignore missing include files
    • ExtProperties

      public ExtProperties(Properties inherit)
      Copy constructor
      Parameters:
      inherit - key-value pairs to copy
  • Method Details

    • size

      public int size()
      Get the number of elements in this dictionary
      Returns:
      number of entries
    • setAutoReload

      public void setAutoReload(long lReload)
      Make this object check for changes in the base file and reload the contents when it changes.
      Parameters:
      lReload -
    • getConfigFileName

      public String getConfigFileName()
      Get the name of the file that was loaded. It will return the full path to the file name, eg. the base folder + file name + ".properties".
      Returns:
      the file name that was loaded
    • reload

      public void reload()
      Re-read the same configuration file. Also clears the cache of parsed keys.
    • makeReadOnly

      public void makeReadOnly()
      Make this dictionary read-only, to prevent writes from other pieces of code that see this object.
    • parseOption

      public String parseOption(String sKey, String sValue, String sDefault, boolean bProcessQueries)
      Parse a value to include other keys
      Parameters:
      sKey - the key that is parsed
      sValue - original value
      sDefault - default value, in case of an error in parsing
      bProcessQueries - whether or not to execute database queries / cache lookups
      Returns:
      the processed value
    • gets

      public String gets(String sKey)
      Get the String value for a given key. If the key is not defined then the empty string is returned. Queries will be processed.
      Parameters:
      sKey - the key to get the value for
      Returns:
      the value
      See Also:
    • gets

      public String gets(String sKey, String sDefault)
      Get the String value for a given key, returning the given default value if the key is not defined. Queries will be processed.
      Parameters:
      sKey - the key to get the value for
      sDefault - default value to return in case the key is not defined
      Returns:
      the value
      See Also:
    • gets

      public String gets(String sKey, String sDefault, boolean bProcessQueries)
      Get the String value for a given key, returning the given default value if the key is not defined. The value that is returned is also stored in a cache so that future requests to the same key will return the value from the cache. This also means that if the key is not defined then the given default value will be cached and returned the next time this function is called. null values are not cached.
      Parameters:
      sKey - the key to get the value for
      sDefault - default value to return in case the key is not defined.
      bProcessQueries - flag to process or not process the database/memory cache queries
      Returns:
      value for this key
    • getb

      public boolean getb(String sKey, boolean bDefault)
      Parse an option to return the boolean value. It returns true if the value starts with t,T,y,Y or 1 and false if the value starts with f,F,n,N or 0. In any other case it returns the given default value.
      Parameters:
      sKey - the key to get the value for
      bDefault - default value
      Returns:
      a boolean
    • geti

      public int geti(String sKey, int iDefault)
      Get the integer value for a key. Returns the given default value if the key is not defined or the value is not an integer representation.
      Parameters:
      sKey - the key to get the value for
      iDefault - default value
      Returns:
      an integer
    • getl

      public long getl(String sKey, long lDefault)
      Get the long value for a key. Returns the given default value if the key is not defined or the value is not a long reprezentation.
      Parameters:
      sKey - the key to get the value for
      lDefault - default value
      Returns:
      a long
    • getd

      public double getd(String sKey, double dDefault)
      Get the double value for a key. Returns the given default value if the key is not defined or the value is not a double reprezentation.
      Parameters:
      sKey - the key to get the value for
      dDefault - default value
      Returns:
      a double
    • toVector

      public Vector<String> toVector(String sKey)
      Split a value by "," and return a Vector of String parts.
      Parameters:
      sKey - the key to get the values for
      Returns:
      a Vector of String parts
    • clearCache

      public void clearCache()
      Method to clear the cache in order to force the evaluation of the keys once again.
    • toString

      public String toString()
      Get some debugging info for this object
      Overrides:
      toString in class Object
      Returns:
      the original keys dump + cached values
    • isReadOnly

      public boolean isReadOnly()
      Check if the configuration object is read only or can accept modifications to it
      Returns:
      true if changes are blocked
    • set

      public void set(String sKey, String sValue)
      Modify an entry of the dictionary.
      Parameters:
      sKey - key to change
      sValue - new value
    • getProperties

      public Properties getProperties()
      Get a Properties view of this object. This will force a parsing of all the defined keys.
      Returns:
      a Properties view of this object
    • addPropertyChangeListener

      public void addPropertyChangeListener(String key, PropertyChangeListener listener)
      Listen for particular changes in the set
      Parameters:
      key - a particular property key to subscribe to, or null to receive all updates
      listener - object to notify
    • removePropertyChangeListener

      public void removePropertyChangeListener(String key, PropertyChangeListener listener)
      Parameters:
      key -
      listener -
    • update

      public void update(Observable o, Object arg)
      Implementation of the Observer interface. Since this object can monitor the original file for changes, it will be notified by a call to this method when this happens.
      Specified by:
      update in interface Observer
      Parameters:
      o - ignored
      arg - ignored