public final class Configuration
extends java.lang.Object
cfg
subdirectory of the P2J home path. The home path is defined by the
required P2J_HOME
system property.
Subsystem configurations are java objects returned by the getSchemaConfig()
method. They are created from XML configuration
information by a SAX parser using the GenericSAXHandler
and
Mapper
classes in package com.goldencode.p2j.util
. XML configuration data is stored in a file named p2j.cfg.xml
.
This class also provides services to get and store individual parameter entries which are read from the main configuration file upon initialization (from cfg/global/parameter XML elements).
Variable substitution is supported in these parameters, in the form
${key_name}
, where key_name
is the name of a
parameter previously defined by this mechanism. The replacement value
applied to the substitution variable is the value of the parameter
identified by key_name
. For example,
<cfg> <global> <parameter name="user.home" value="/home/user" /> <parameter name="subdir" value="${user.home}/mysubdir" /> </global> ... </cfg>results in the following parameter definitions after substitution:
user.home = /home/user subdir = /home/user/mysubdirNote: the following restrictions apply to this mechanism:
P2J_HOME
is a reserved parameter key. It may not be
defined in the configuration file, but it is available for use as a
substitution variable.key_name
must not include any
characters which have special syntax meaning in regular expressions.
Regular expressions are used internally for the replacement; special
characters in key_name
are not explicitly escaped,
which could cause unexpected results.Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
CFG_DIR
Configuration subdirectory name
|
private static java.lang.String |
CFG_EXTENSION
Configuration file name extension
|
private static java.lang.String |
DEF_CFG_FILE
Default configuration filename
|
private static Configuration |
instance
Singleton instance.
|
private static java.util.Set<java.lang.String> |
OS_DEPENDENT_VALUES
Holds a set of configuration parameter names whose values are OS dependent.
|
static char |
P2J_FILE_SEP
System property name for P2J file separator used internally.
|
static char |
P2J_PATH_SEP
System property name for P2J path separator used internally.
|
private java.util.Map<java.lang.String,java.lang.String> |
parmMap
Map of parameters
|
private static java.lang.String |
PROP_HOME_DIR
System property name for P2J home path
|
private static boolean |
runtimeConfig
If
true then it is a server run time mode when configuration is NOT read from
the configuration directory. |
private GenericSAXHandler |
saxHandler
The generic SAX handler that will be used by the parser after it's
configured to work with P2J config files.
|
private javax.xml.parsers.SAXParser |
saxParser
The parser instance.
|
private javax.xml.parsers.SAXParserFactory |
saxParserFactory
Factory providing the SAX parser instance that will be used to parse the config file.
|
static java.lang.String |
TAG_CFG
Constant denoting the
<cfg> XML tag |
static java.lang.String |
TAG_DIALECTSPECIFIC
Constant denoting the
<dialect-specific> XML tag |
static java.lang.String |
TAG_GLOBAL
Constant denoting the
<global> XML tag |
static java.lang.String |
TAG_METADATA
Constant denoting the
<metadata> XML tag |
static java.lang.String |
TAG_NAMESPACE
Constant denoting the
<namespace> XML tag |
static java.lang.String |
TAG_PARAMETER
Constant denoting the
<parameter> XML tag |
static java.lang.String |
TAG_SCHEMA
Constant denoting the
<schema> XML tag |
static java.lang.String |
TAG_TABLE
Constant denoting the
<table> XML tag |
Modifier | Constructor and Description |
---|---|
private |
Configuration()
Private default constructor to prevent creation of instances of this class.
|
Modifier and Type | Method and Description |
---|---|
private void |
computeDerivedValues()
Computes missing derived parameter values based on previously set ones.
|
private static Configuration |
createConfiguration()
Create a Configuration instance.
|
static java.lang.String |
forceHome(java.lang.String filename)
Given a filename that assumes it is relative to the current directory, check if the
project home directory is not "." and if so prepend the filename with the project
home value.
|
private static java.io.File |
getConfigDirectory()
Get the configuration directory, which must be a
cfg
subdirectory beneath the P2J home path. |
private static java.io.File |
getConfigFile(java.io.File dir,
java.lang.String root)
Get the configuration file for the subsystem associated with
root , which is either <root>.cfg.xml
or, if this file is missing, is the default configuration file,
p2j.cfg.xml . |
static Configuration |
getInstance()
Get the singleton instance of this class.
|
static java.lang.String |
getParameter(java.lang.String key)
Get the configuration parameter with the specified key.
|
static boolean |
getParameter(java.lang.String key,
boolean defaultValue)
Get the configuration parameter with the specified key as a boolean value.
|
static java.lang.String |
getParameter(java.lang.String key,
java.lang.String defaultValue)
Get the configuration parameter with the specified key.
|
static SchemaConfig |
getSchemaConfig()
Return a schema configuration object.
|
static java.lang.String |
home()
Get the P2J home path defined for the application or "." if no such
path is defined.
|
static boolean |
isRuntimeConfig()
Determines whether the configuration is in conversion mode (configuration is read from the
configuration directory) or server run time mode.
|
private void |
loadXml(java.io.InputStream cfgStream)
Parses a given XML data stream using the SAX parser and handler that were
already prepared.
|
static void |
main(java.lang.String[] args)
Simple schema loader test: the XML file passed as an argument is parsed
and the resulting
SchemaConfig together with all global
parameters are printed to standard output. |
static java.lang.String |
normalizeFilename(java.lang.String filename)
Converts an absolute or relative filename (relative to the current
directory) into a filename that is relative to the project home.
|
private static java.lang.String |
normalizePath(java.io.File file,
boolean verify,
boolean isDirectory)
Normalize and optionally verify the specified file system object.
|
private java.lang.String |
putParameter(java.lang.String key,
java.lang.String value)
Associate a configuration parameter value with
key . |
static void |
setRuntimeConfig(boolean isRuntime)
Set whether the configuration should be in conversion mode (configuration is read from the
configuration directory) or server run time mode.
|
private static void |
setupHandler(GenericSAXHandler saxHandler)
Prepare the general purpose SAX handler for working with P2J XML config
file syntax.
|
java.lang.String |
substituteParms(java.lang.String value)
Variable substitution is performed on
value before it is
returned. |
static java.io.File |
toFile(java.lang.String path)
If
path represents a relative path, generate a
File object for this path, relative to the home directory
defined for the application. |
public static final java.lang.String TAG_CFG
<cfg>
XML tagpublic static final java.lang.String TAG_GLOBAL
<global>
XML tagpublic static final java.lang.String TAG_PARAMETER
<parameter>
XML tagpublic static final java.lang.String TAG_SCHEMA
<schema>
XML tagpublic static final java.lang.String TAG_NAMESPACE
<namespace>
XML tagpublic static final java.lang.String TAG_DIALECTSPECIFIC
<dialect-specific>
XML tagpublic static final java.lang.String TAG_METADATA
<metadata>
XML tagpublic static final java.lang.String TAG_TABLE
<table>
XML tagprivate static final java.lang.String PROP_HOME_DIR
public static final char P2J_FILE_SEP
public static final char P2J_PATH_SEP
private static final java.lang.String CFG_DIR
private static final java.lang.String CFG_EXTENSION
private static final java.lang.String DEF_CFG_FILE
private static volatile Configuration instance
private static boolean runtimeConfig
true
then it is a server run time mode when configuration is NOT read from
the configuration directory.private final java.util.Map<java.lang.String,java.lang.String> parmMap
private javax.xml.parsers.SAXParserFactory saxParserFactory
private javax.xml.parsers.SAXParser saxParser
private GenericSAXHandler saxHandler
private static final java.util.Set<java.lang.String> OS_DEPENDENT_VALUES
p2j.cfg.xml
are encoded with internal P2J_FILE_SEP
and
P2J_PATH_SEP
separators, but they need to be switched when configuration file is
loaded, on the fly, to actual separators for the OS we are running on.
We exclude the PROP_HOME_DIR
from this list because it is internally computed and
the result uses the correct path separator.private Configuration()
public static boolean isRuntimeConfig()
true
if it is a server run time mode (when configuration is NOT read
from the configuration directory).public static void setRuntimeConfig(boolean isRuntime)
isRuntime
- true
if it is a server run time mode (when configuration is NOT read
from the configuration directory).java.lang.IllegalStateException
- If the configuration instance was already initialized.public static SchemaConfig getSchemaConfig()
p2j.cfg.xml
that was parsed when this class was loaded.public static java.lang.String getParameter(java.lang.String key)
putParameter(java.lang.String, java.lang.String)
method.key
- Name of parameter to retrieve.null
if
key
is an invalid identifier.public static java.lang.String getParameter(java.lang.String key, java.lang.String defaultValue)
putParameter(java.lang.String, java.lang.String)
method. Values for
parameters known to be OS filesystem dependent (files, paths and their lists) will be
returned already converted to current OS.key
- Name of parameter to retrieve.defaultValue
- Value to be returned if key
cannot be found in the map of parameters.
May be null
.defaultValue
if key
is not
found.public static boolean getParameter(java.lang.String key, boolean defaultValue)
putParameter(java.lang.String, java.lang.String)
method. Values for parameters known to be OS filesystem dependent (files, paths and their
lists) will be returned already converted to current OS.key
- Name of parameter to retrieve.defaultValue
- Value to be returned if key
cannot be found in the map of parameters.defaultValue
if key
is not
found.private java.lang.String putParameter(java.lang.String key, java.lang.String value) throws ConfigurationException
key
. Variable substitution is
performed on value
before it is added to the map.
In the case of OS dependent values, they are adjusted to match the current OS (operation only needed for Windows OS. For Linux/Unix this operation is a no-op because the neutral separators were chosen to match the separators for them).
key
- A name which uniquely identifies the parameter.value
- The parameter's value.value
after variable substitution has been performed.ConfigurationException
- if key
already identifies a configuration parameter.private void computeDerivedValues()
public java.lang.String substituteParms(java.lang.String value)
value
before it is
returned. Any portions of the string matching a ${SYMBOL} syntax will
have the parameter map searched for SYMBOL. If it is found, the value
of that parameter will be inserted in place of ${SYMBOL}.
Replacements occur in the same order they were defined in the main configuration file, and then in the order in which they were added by external callers of this method.
value
- The value on which to operate.value
after variable substitution
has been performed.public static java.lang.String home() throws ConfigurationException
ConfigurationException
- If there is any problem with normalizing the specified path.public static java.lang.String forceHome(java.lang.String filename)
filename
- Filename that is relative to the project root.public static java.io.File toFile(java.lang.String path) throws ConfigurationException
path
represents a relative path, generate a
File
object for this path, relative to the home directory
defined for the application. If filename
is an absolute
path, simply use it to construct the File
.path
- A relative or absolute file or directory path.ConfigurationException
- if path
is relative, but no home path has been
defined for the application.public static java.lang.String normalizeFilename(java.lang.String filename)
filename
- Absolute or current directory relative filename.public static Configuration getInstance()
private static Configuration createConfiguration()
private static java.io.File getConfigDirectory() throws ConfigurationException
cfg
subdirectory beneath the P2J home path.ConfigurationException
- if configuration directory does not exist or is not a
directory.private static java.io.File getConfigFile(java.io.File dir, java.lang.String root) throws ConfigurationException
root
, which is either <root>.cfg.xml
or, if this file is missing, is the default configuration file,
p2j.cfg.xml
.dir
- Configuration directory.root
- Name associated with the target subsystem.ConfigurationException
- if configuration file cannot be found.private static java.lang.String normalizePath(java.io.File file, boolean verify, boolean isDirectory) throws ConfigurationException
Normalizing the path converts relative paths to absolute paths and attempts to remove any
redundant, relative constructs, such as references to the current directory (.
) and
to the parent directory (..
).
Verifying the path checks that the object exists in the file system and that it is the expected type (file vs. directory).
file
- File whose path is to be manipulated.verify
- true
to verify the path, else false
.isDirectory
- true
if file
is supposed to represent a directory, false
if it should represent a file. This argument is only relevant if verify
is
set to true
.ConfigurationException
- if verification fails.private static void setupHandler(GenericSAXHandler saxHandler)
saxHandler
- The SAX handler to be configured.private void loadXml(java.io.InputStream cfgStream) throws org.xml.sax.SAXException, java.io.IOException, java.lang.RuntimeException
GenericSAXHandler.getGlobalObjects(String)
.cfgStream
- Input stream with data to be parsed.java.io.IOException
- If any IO errors occur in reading the file.org.xml.sax.SAXException
- If any SAX errors occur during processing.java.lang.RuntimeException
- wrapping a ConfigurationException
if any error
occurs reading the XML data or creating the configuration
object. This includes missing configuration file or file format
constraint violations detected during parsing.GenericSAXHandler.getGlobalObjects(String)
public static void main(java.lang.String[] args) throws org.xml.sax.SAXException, java.io.IOException, java.lang.RuntimeException
SchemaConfig
together with all global
parameters are printed to standard output. Variable substitution is not
performed on parameters, their values are shown as they were read from
XML.args
- The first element is the absolute path of the XML file to parse.java.io.IOException
- If IO errors occur.org.xml.sax.SAXException
- If the SAX parser encounters problems while processing the XML
content.java.lang.RuntimeException
- wrapping a ConfigurationException
if any error
occurs reading the XML data or creating the configuration
objects. This includes missing configuration file or file format
constraint violations detected during parsing.