org.mbari.siam.configt
Class ConfigFile

java.lang.Object
  extended by org.mbari.siam.configt.ConfigFile
Direct Known Subclasses:
BinRoutingSh, HostName, Hosts, Inittab, Interfaces, Loadxr, Modules, PAPSecrets, RcLocal, SiamEnv, SiamPortCfg, TemplateConfigFile

public abstract class ConfigFile
extends java.lang.Object

The ConfigFile class is a base class representing common properties and behavior of a configuration file. ConfigFiles are created and managed by a PlatformConfigServlet, which gets the location and type of the config file from it's own properties file. The name and type information are used to get the appropriate ConfigFile subclass from a ConfigFileFactory instance. Each configuration file contains a collection (Vector) of ConfigItems (configuration items) that are managed within it. These configuration items are defined in a properties file. The ConfigFile is responsible for reading the config item definitions and creating the appropriate ConfigItem objects (see method loadProperties()). Once the configuration items are created, the readAll() method is used to initialize the ConfigItems with the values currently stored in the configuration file. The ConfigItem objects are requested by the PlatformConfigServlet, which manages their organization and presentation in the user interface. The ConfigFile is also responsible for using the present value of it's configuration items to generate the actual configuration file; subclasses generally override the default method for this, writeTemp(), to implement file-specific logic. When the PlatformConfigServlet is ready to write settings to the configuration files, it first calls the createBackup() method to backup the current contents of the configuration file. It then invokes writeTemp() for each ConfigFile, which writes a temporary copy of the config file, followed by replaceFile(), which replaces the original file with the temporary copy just created. The configuration files may be generated in different ways. Some files (siamPort.cfg, siamEnv) are edited in place, using regular expressions to find and replace configuration items with their current values. Other files (system files like rc.local, hosts, etc.) are generated using templates, with HTML-style tags that represent places where configuration item values should be inserted. This allows the content of these files to be changed without recompiling the configuration tool.


Field Summary
 boolean _appendUnwrittenItems
          used by writeTempFindReplace() to look for unwritten config items and append them to the file
protected  java.util.Vector _configItems
          Collection of configuration items managed through this ConfigFile
 boolean _getDisplayValue
          used by writeTempFindReplace() to use the display value for config items or use mapped (itemRef) values of configItems
protected  java.util.Hashtable _layoutParameters
          Used to set up parameters for laying out HTML controls (e.g.
protected static org.apache.log4j.Logger _log4j
          Log4J logger
 boolean _mapNull
          used by writeTempFindReplace() to map null characters to _nullMapString if true
 java.lang.String _nullMapString
           
protected  ConfigTool _parent
          PlatformConfigServlet managing this ConfigFile.
protected  java.util.Properties _properties
          Java Properties
protected  java.lang.String _propertiesPath
          Location of properties file defining
protected  java.lang.String _templatePath
          Location of template used to regenerate this config file
protected  java.io.File _theFile
          The actual file represented by this ConfigFile.
static int FIND_REPLACE
           
static int SIMPLE
          WriteTemp variant selectors
static int TAG_REPLACE
           
 
Constructor Summary
ConfigFile(java.lang.String path, java.lang.String propertiesPath)
          Constructor
 
Method Summary
protected  void appendTempFile(java.io.BufferedWriter bw)
          Append additional contents, templates, etc.
 boolean createBackup(java.lang.String backupType, java.io.File backupDir)
          Backup this file to a new name, optionally overwrite the existing backup, or keep multiple copies (file.bak.mmddyy[-n])
 java.util.Vector getAll()
          Return a vector of all ConfigItems
 java.util.Vector getAttributes(java.util.Properties properties, HTMLControl htc, java.lang.String itemName)
          getAttributes - get attributes for the specified ConfigItem
 int getCols(java.lang.String group)
          Return max number of columns called for by ConfigItem's layout parameters
 ConfigItem getItem(java.lang.String name)
          Return an configuration item with the specified name; note that names may not be shared among config files, i.e., the same item may not be managed by more than one config file.
 java.lang.String getItemValue(java.lang.String name)
          Get the current value of the specified ConfigItem
protected  int getLayoutParameter(java.lang.String key)
           
 java.lang.String getName()
          Return configuration file name
 java.util.Hashtable getOutputMappings(java.util.Properties properties, HTMLControl htc, java.lang.String itemName)
          getOutputMappings - get output value mappings for the specified ConfigItem
 java.lang.String getPath()
          Return path to this ConfigFile
 int getRows(java.lang.String group)
          Return max number of rows called for by ConfigItem's layout parameters
 boolean initFile(java.io.File file)
          Create a file if it doesn't already exist
 boolean initFile(java.lang.String fileName)
          Wrapper for initFile; creates a new file from fileName and passes it to initFile
protected  void initWriteFlags(boolean value)
          Intialize write flags for all config items
protected  void loadProperties()
          Load properties and ConfigItem definitions from properties file.
static void main(java.lang.String[] args)
           
protected  java.lang.String mapOutputValue(java.lang.String s)
          mapOutputValue() allows subclasses a mechanism for mapping the value of Strings used for presentation to a different value for output(e.g.
 void newLine(java.io.BufferedWriter bw)
          Override BufferedWriter's newLine() method, which varies the newline character(s) according to platform.
 java.util.Vector readAll()
          Read the configuration file and assign the values read from the file to the ConfigItems managed by this ConfigFile.
 void replaceFile(java.io.File tempFile)
          replaceFile replaces the configuration file with the new version (created by a call to writeTemp()).
 void setParent(ConfigTool parent)
          Set parent PlatformConfigServlet
protected  void updateConfigItems()
          Update value of ConfigItems, possibly using values of global items.
 boolean validateItem(java.lang.String name)
          Validates a configuration item (not implemented)
protected  void writeLine(java.lang.String line, java.io.BufferedWriter bw)
          Write line, newline and flush
abstract  java.io.File writeTemp()
          Write the current value of all configuration items to a temporary file.
 java.io.File writeTemp(int method)
           
 java.io.File writeTempFindReplace()
          there are a couple of parameters that must be set before calling this this method:getDisplayValue and appendUnwrittenItems
 java.io.File writeTempSimple()
           
 java.io.File writeTempTagReplace()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_log4j

protected static org.apache.log4j.Logger _log4j
Log4J logger


_configItems

protected java.util.Vector _configItems
Collection of configuration items managed through this ConfigFile


_properties

protected java.util.Properties _properties
Java Properties


_propertiesPath

protected java.lang.String _propertiesPath
Location of properties file defining


_templatePath

protected java.lang.String _templatePath
Location of template used to regenerate this config file


_layoutParameters

protected java.util.Hashtable _layoutParameters
Used to set up parameters for laying out HTML controls (e.g. number of rows, columns required for a specific group of HTML controls)


SIMPLE

public static final int SIMPLE
WriteTemp variant selectors

See Also:
Constant Field Values

TAG_REPLACE

public static final int TAG_REPLACE
See Also:
Constant Field Values

FIND_REPLACE

public static final int FIND_REPLACE
See Also:
Constant Field Values

_appendUnwrittenItems

public boolean _appendUnwrittenItems
used by writeTempFindReplace() to look for unwritten config items and append them to the file


_getDisplayValue

public boolean _getDisplayValue
used by writeTempFindReplace() to use the display value for config items or use mapped (itemRef) values of configItems


_mapNull

public boolean _mapNull
used by writeTempFindReplace() to map null characters to _nullMapString if true


_nullMapString

public java.lang.String _nullMapString

_parent

protected ConfigTool _parent
PlatformConfigServlet managing this ConfigFile. The parent servlet has a global view of all ConfigItems, allowing this ConfigFile to gain visibility into other configuration parameter values upon which it depends.


_theFile

protected java.io.File _theFile
The actual file represented by this ConfigFile.

Constructor Detail

ConfigFile

public ConfigFile(java.lang.String path,
                  java.lang.String propertiesPath)
Constructor

Method Detail

setParent

public void setParent(ConfigTool parent)
Set parent PlatformConfigServlet


getItem

public ConfigItem getItem(java.lang.String name)
Return an configuration item with the specified name; note that names may not be shared among config files, i.e., the same item may not be managed by more than one config file. (though it's value may appear in more than one config file)


getItemValue

public java.lang.String getItemValue(java.lang.String name)
Get the current value of the specified ConfigItem


readAll

public java.util.Vector readAll()
Read the configuration file and assign the values read from the file to the ConfigItems managed by this ConfigFile.


getRows

public int getRows(java.lang.String group)
Return max number of rows called for by ConfigItem's layout parameters


getCols

public int getCols(java.lang.String group)
Return max number of columns called for by ConfigItem's layout parameters


getLayoutParameter

protected int getLayoutParameter(java.lang.String key)

createBackup

public boolean createBackup(java.lang.String backupType,
                            java.io.File backupDir)
Backup this file to a new name, optionally overwrite the existing backup, or keep multiple copies (file.bak.mmddyy[-n])


replaceFile

public void replaceFile(java.io.File tempFile)
replaceFile replaces the configuration file with the new version (created by a call to writeTemp()). See also backupFile().


writeTemp

public abstract java.io.File writeTemp()
Write the current value of all configuration items to a temporary file. This default method is generally overridden by subclasses; the default behaviour is to search each line for the configuration items using the importRegexp. When a match is found, the exportRegexp is used to format the line for writing, inserting the current value of the configuration item. Note that this assumes that only one configuration item appears on each line (as in Java properties files). Configuration files that contain more than one item per line should use a template and/or override this method.


writeTemp

public java.io.File writeTemp(int method)

writeTempSimple

public java.io.File writeTempSimple()

writeTempTagReplace

public java.io.File writeTempTagReplace()

writeTempFindReplace

public java.io.File writeTempFindReplace()
there are a couple of parameters that must be set before calling this this method:getDisplayValue and appendUnwrittenItems


updateConfigItems

protected void updateConfigItems()
                          throws java.lang.Exception
Update value of ConfigItems, possibly using values of global items. Usually this is called before writing the files using writeTemp(). Does nothing by default;

Throws:
java.lang.Exception

appendTempFile

protected void appendTempFile(java.io.BufferedWriter bw)
                       throws java.io.IOException
Append additional contents, templates, etc. before closing the temp config file. Called by several writeTemp() variants. Does nothing by default; subclasses may override.

Throws:
java.io.IOException

initWriteFlags

protected void initWriteFlags(boolean value)
Intialize write flags for all config items


getAll

public java.util.Vector getAll()
Return a vector of all ConfigItems


writeLine

protected void writeLine(java.lang.String line,
                         java.io.BufferedWriter bw)
                  throws java.io.IOException
Write line, newline and flush

Throws:
java.io.IOException

newLine

public void newLine(java.io.BufferedWriter bw)
             throws java.io.IOException
Override BufferedWriter's newLine() method, which varies the newline character(s) according to platform. ConfigFiles should always use UNIX (\n) newline.

Throws:
java.io.IOException

initFile

public boolean initFile(java.io.File file)
                 throws java.io.IOException
Create a file if it doesn't already exist

Throws:
java.io.IOException

initFile

public boolean initFile(java.lang.String fileName)
                 throws java.io.IOException
Wrapper for initFile; creates a new file from fileName and passes it to initFile

Throws:
java.io.IOException

validateItem

public boolean validateItem(java.lang.String name)
Validates a configuration item (not implemented)


getAttributes

public java.util.Vector getAttributes(java.util.Properties properties,
                                      HTMLControl htc,
                                      java.lang.String itemName)
getAttributes - get attributes for the specified ConfigItem


getOutputMappings

public java.util.Hashtable getOutputMappings(java.util.Properties properties,
                                             HTMLControl htc,
                                             java.lang.String itemName)
getOutputMappings - get output value mappings for the specified ConfigItem


loadProperties

protected void loadProperties()
                       throws java.lang.Exception
Load properties and ConfigItem definitions from properties file. ConfigItems are defined using a property naming convention: ..=

Throws:
java.lang.Exception

getPath

public java.lang.String getPath()
Return path to this ConfigFile


getName

public java.lang.String getName()
Return configuration file name


main

public static void main(java.lang.String[] args)

mapOutputValue

protected java.lang.String mapOutputValue(java.lang.String s)
mapOutputValue() allows subclasses a mechanism for mapping the value of Strings used for presentation to a different value for output(e.g. NULL->"\0"). Subclasses may override this method to provide their own logic and values; Returns input string by default;



Copyright © 2003 MBARI.

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement.