public class ConfigFileReader
extends java.lang.Object
An additional goal of Config is for all params to be processed when the config file is read, both at startup and when file is reread. This is important because then if there are any processing problems those problems are noticed and must be dealt with immediately (instead of them occurring in an lazy fashion, perhaps when no one is available to deal with them). But this goal would mean that the params would need to be defined in config files so they would all be dealt with at startup. For many situations want the params to be defined where they are used since that would make them easier to support. But then they wouldn't get initialized until the Java class is actually accessed.
For each parameter configured in the file the property is written as a system property if that system property is not yet defined. This way all parameters are available as system properties, even non-transitime ones such as for hibernate or logback. Java properties set on the command line take precedence since the properties are not overwritten if already set.
The params are configured in an XML or a properties file. The member processConfig() first process a file named transitime.properties if one exists in the classpath. Then files specified by the Java system property transitime.configFiles . If the file has a .xml suffix then it will be processed as XML. Otherwise it will be processed as a regular properties file. Multiple config files can be used. Each XML file should have a corresponding configuration class such as CoreConfig.java. In the XXXConfig.java class the params are specified along with default values.
Multiple threads can read parameters simultaneously. The threads should call readLock() if it is important to update the parameters in a consistent way, such as when more than a single parameter are interrelated.
Logback error logging is not used in this class so that logback can be not accessed until after the configuration is processed. In this way logback can be configured using a configuration file to set any logback Java system properties such as logback.configurationFile .
Modifier and Type | Class and Description |
---|---|
static class |
ConfigFileReader.ConfigException
An exception for when a parameter is being read in
|
Constructor and Description |
---|
ConfigFileReader() |
Modifier and Type | Method and Description |
---|---|
static void |
processConfig()
Process the configuration file specified by a file named
transitime.properties that is in the classpath.
|
static void |
processConfig(java.lang.String fileName)
Processes specified config file and overrides the config parameters
accordingly.
|
static void |
readLock()
When reading config params should lock up the system so that data is
consistent.
|
static void |
readUnlock()
Every time readLock() is called there needs to be a call to readUnlock().
|
static void |
readXmlConfigFile(java.lang.String fileName)
Reads the specified XML file and stores the results into a HashMap configFileData.
|
public static void readXmlConfigFile(java.lang.String fileName) throws ConfigFileReader.ConfigException
<transitime>
<predictor>
<allowableDistanceFromPath>
75.0
</allowableDistanceFromPath>
</predictor>
</transitime>
fileName
- ConfigFileReader.ConfigException
public static void readLock()
public static void readUnlock()
public static void processConfig(java.lang.String fileName) throws ConfigFileReader.ConfigException, ConfigValue.ConfigParamException
Reads in data and stores it in the config objects so that it is programmatically accessible. Does a write lock so that readers will only execute when data is not being modified.
fileName
- Name of the config file. If null then default values will be
used for each parameter.ConfigFileReader.ConfigException
ConfigValue.ConfigParamException
public static void processConfig()
All parameters found will be set as system properties unless the system property was already set. This way Java system properties set on the command line have precedence.
By making all read parameters Java system properties they can be used for external libraries such as for specifying hibernate or logback config files.