org.locomotive.server
Class Config

java.lang.Object
  |
  +--org.locomotive.server.Config
Direct Known Subclasses:
HTTPServerConfig, LocoConfig

public class Config
extends java.lang.Object

The Config class provides a way to maintain configuration information. It contains it's own error checking, and can return configuration in either an integer or String form. Any configs in a config file must have defuaults specified in the setupDefaults() method. So, to create your own config, you would have to override this method to setup your own set of configs.

The config file should specify first the name of the config, followed by at least one space, and then the value. For example:

     SYSTEM_INSTANCE_ID                      sv_one
All lines which begin with an '#' are read as comments, i.e. they are not evaluated.


Field Summary
static java.lang.String BEAN
          When the config_file_path is equal to this, it indicates that the configuration was loaded from the BeanDirectory services, not a config file.
 java.lang.String forint
          a constraint for use in the addConfig method which will check to make sure the value of the config is not empty or null, and is an integer
 java.lang.String forstring
          a constraint for use in the addConfig method which will check to make sure the value of the config is neither empty nor null.
 
Constructor Summary
Config()
          Constructor for Bean Directory.
Config(java.lang.String[] args, java.lang.String defaultpath)
          Initializes a config object, with the specified arguments, from the specified file path.
 
Method Summary
protected  void addConfig(java.lang.String name, java.lang.String val, java.lang.String conslist, java.lang.String desc)
          This method will allow you to add a config, regardless of whether or not it exists in the defaults, or in the config file
 java.lang.String changeConfig(java.lang.String name, java.lang.String value)
          this method changes an already created config to a specified value.
 void doConfigDump()
          prints all the configs and their present values, along with their description, to standard out.
 java.lang.Integer genericGetInteger(java.lang.String name)
           
 java.lang.String genericGetString(java.lang.String name)
           
 void genericSet(java.lang.String name, java.lang.String val)
           
 void genericSetInteger(java.lang.String name, java.lang.Integer val)
           
 void genericSetString(java.lang.String name, java.lang.String val)
           
 java.lang.String getConfigFilePath()
          returns the path of the file the config is read in from.
 java.util.Enumeration getConfigNamesEnumeration()
          returns an enumeration of all the config names
 int getInt(java.lang.String name)
          returns the value of a config as an int, if possible
 java.lang.String getString(java.lang.String name)
          returns the value of a config as a string
 void init(java.lang.String[] args, java.lang.String defaultpath)
           
protected  boolean isComment(java.lang.String s)
          returns true if the first character of the string is '#'
protected  java.lang.String loadConfigsFromFile(java.lang.String filename)
          actually reads in the file and loads the configs.
static void printsyntax(java.lang.String s)
          prints the usage for arguments accepted by the constructor for this object
 void setupDefaults()
          Override this method to set up your own defaults.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BEAN

public static final java.lang.String BEAN
When the config_file_path is equal to this, it indicates that the configuration was loaded from the BeanDirectory services, not a config file.

forint

public java.lang.String forint
a constraint for use in the addConfig method which will check to make sure the value of the config is not empty or null, and is an integer

forstring

public java.lang.String forstring
a constraint for use in the addConfig method which will check to make sure the value of the config is neither empty nor null.
Constructor Detail

Config

public Config(java.lang.String[] args,
              java.lang.String defaultpath)
       throws java.lang.IllegalArgumentException
Initializes a config object, with the specified arguments, from the specified file path.
Parameters:
args - a series of srguments may be specified at construction. these include:
  • -h help- prints usage.
  • -f sets config file path. will override the specified defaultpath. May be used to override BeanDirectory-style configuration.
  • -d dumps configs
  • -dv dumps configs- verbose
  • -O enters any number of properties into Config file. Usage: NAME=VALUE
Only config properties which already exist in the default config tables can be added. If property is NAME="", then the value is entered as "", erasing any previous values.
defaultpath - the path of the config file to load. If equal to the constant BEAN, no config file is used.

Config

public Config()
Constructor for Bean Directory. Should be followed by a call to init(args, BEAN).
Method Detail

init

public void init(java.lang.String[] args,
                 java.lang.String defaultpath)
          throws java.lang.IllegalArgumentException

genericSetInteger

public void genericSetInteger(java.lang.String name,
                              java.lang.Integer val)

genericSetString

public void genericSetString(java.lang.String name,
                             java.lang.String val)

genericSet

public void genericSet(java.lang.String name,
                       java.lang.String val)

genericGetInteger

public java.lang.Integer genericGetInteger(java.lang.String name)

genericGetString

public java.lang.String genericGetString(java.lang.String name)

setupDefaults

public void setupDefaults()
Override this method to set up your own defaults. Any configs in the file which are not specified here will cause an error to be thrown when the file is loaded. to add a config to the defaults, use the addConfig method.
See Also:
addConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)

getConfigFilePath

public java.lang.String getConfigFilePath()
returns the path of the file the config is read in from. If return value is equal to constant BEAN, no config file was used.

addConfig

protected void addConfig(java.lang.String name,
                         java.lang.String val,
                         java.lang.String conslist,
                         java.lang.String desc)
This method will allow you to add a config, regardless of whether or not it exists in the defaults, or in the config file
Parameters:
name - the name of the config
val - the value of the config
conslist - the list of constraints to apply to a config value when it is loaded. Constraint usage follows that for the org.locomotive.util.StringConstraint. Two values are provided in this class - forint and forstring.
desc - a short description of the config
See Also:
StringConstraint

changeConfig

public java.lang.String changeConfig(java.lang.String name,
                                     java.lang.String value)
                              throws ConfigNotFoundException,
                                     ConfigInitializationException
this method changes an already created config to a specified value.
Parameters:
name: - the name of the config
value - the new value for the config.
Throws:
ConfigNotFoundException - if the config does not exist.
ConfigInitializationException - if the config value does not meet the required contraints

getString

public java.lang.String getString(java.lang.String name)
                           throws ConfigNotFoundException
returns the value of a config as a string

getInt

public int getInt(java.lang.String name)
           throws ConfigNotFoundException
returns the value of a config as an int, if possible

doConfigDump

public void doConfigDump()
prints all the configs and their present values, along with their description, to standard out.

isComment

protected boolean isComment(java.lang.String s)
returns true if the first character of the string is '#'

loadConfigsFromFile

protected java.lang.String loadConfigsFromFile(java.lang.String filename)
actually reads in the file and loads the configs. Call this to reload a file during runtime.
Returns:
an error message if there were problems reading in the configs, or an empty string if all went well.

printsyntax

public static void printsyntax(java.lang.String s)
prints the usage for arguments accepted by the constructor for this object

getConfigNamesEnumeration

public java.util.Enumeration getConfigNamesEnumeration()
returns an enumeration of all the config names