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/mysubdir
Note: 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 | Class and Description |
|---|---|
static class |
Configuration.IncludeRemap
Mapping of include file path prefixes as found in source file include references, to new
prefixes, which will allow include files to be found in the conversion project directory
hierarchy.
|
| Modifier and Type | Field and Description |
|---|---|
private ProfileConfig |
activeProfile
The current active profile, in multi-profile mode.
|
private java.util.Set<java.lang.String> |
activeProfiles
The set of active profiles, in multi-profile mode.
|
private static java.lang.String |
CFG_DIR
Configuration subdirectory name
|
private static java.lang.String |
CFG_EXTENSION
Configuration file name extension
|
private java.lang.String |
cvtpath
The resolved
cvtpath parameter, as an absolute path. |
private java.util.Map<java.lang.String,java.util.Set<java.lang.String>> |
dbProfile
A mapping of each database to its profile, from where it was loaded.
|
static java.lang.String |
DEF_CFG_FILE
Default configuration filename
|
static java.lang.String |
DEF_UNIT_TEST_TYPE
Default legacy unit test type
|
private FileList |
fileList
The resolved files from the
fileSet, if available. |
private java.util.Map<java.lang.String,java.lang.String> |
fileProfile
A mapping of each processed file to its profile, from where it was loaded.
|
private java.util.Map<java.lang.String,FileSet> |
fileSet
The configured
file-set(s). |
private static ProfileConfig |
globalConfig
The pristine configuration instance loaded from the file.
|
private static boolean |
importMode
Flags the import mode (configuration is NOT read from the configuration directory) and the server does
not run but the schema information is read from the application's jar.
|
private java.util.Map<java.lang.String,java.lang.String> |
includeRemap
The
include remappings. |
private static Configuration |
instance
Singleton instance.
|
private static CentralLogger |
LOG
Logger
|
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 java.util.Map<java.lang.String,ProfileConfig> |
profiles
The map of defined profiles.
|
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.
|
private SchemaConfig |
schemaConfig
The loaded schema configuration.
|
static java.lang.String |
TAG_CFG
Constant denoting the
<cfg> XML tag. |
static java.lang.String |
TAG_CVTPATH
The parameter name for conversion root path.
|
static java.lang.String |
TAG_DIALECTSPECIFIC
Constant denoting the
dialect-specific> XML tag. |
static java.lang.String |
TAG_DIRECTORY
Constant denoting the
<directory> XML tag. |
static java.lang.String |
TAG_EXCLUDE
Constant denoting the
<exclude> XML tag. |
static java.lang.String |
TAG_FILE_SET
Constant denoting the
<file-set> XML tag. |
static java.lang.String |
TAG_FILENAME
Constant denoting the
<filename> XML tag. |
static java.lang.String |
TAG_GLOBAL
Constant denoting the
<global> XML tag. |
static java.lang.String |
TAG_INCLUDE
Constant denoting the
<include> XML tag. |
static java.lang.String |
TAG_INCLUDE_MAPPINGS
Constant denoting the
include-mappings> 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_PROFILE
Constant denoting the
profile> 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.
|
java.lang.String |
getActiveProfile()
Get the active profile name.
|
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 java.lang.String |
getConversionFolder()
Get the folder where conversion artifacts are being stored.
|
static FileList |
getFileList()
Resolve the configured
fileSet. |
static java.util.Map<java.lang.String,java.lang.String> |
getIncludeFilePrefixMap()
Get the custom mappings of include file prefixes to the values with which they should be
replaced, when resolving the name of an include file found in 4GL source code into a source
file on disk in the conversion project.
|
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 java.util.Map<java.lang.String,java.lang.String> |
getPathAliases()
Get the configured path aliases.
|
private java.lang.String[] |
getPaths(ProfileConfig p)
Resolve the profile's proPaths.
|
java.lang.Object |
getProfileState(java.lang.Object key)
Get a state saved at the
active profile, for 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 |
isCaseSensitive()
Check if the legacy operating system, from which the source code originates, is case-sensitive.
|
static boolean |
isImportMode()
Determines whether the configuration is in import mode (configuration is NOT read from the configuration
directory) and server does not run but the schema information is read from the application's jar.
|
boolean |
isMultiProfile()
Check if we are in
multi-profile mode. |
static boolean |
isRuntimeConfig()
Determines whether the configuration is in conversion mode (configuration is read from the
configuration directory) or server run time mode.
|
static void |
loadConfigProfiles(java.util.List<java.lang.String> profiles)
Load all configuration profiles.
|
private void |
loadConfigProfilesImpl(java.util.List<java.lang.String> profiles)
Load all configuration profiles.
|
private void |
loadProfile(java.lang.String name)
Load the specified profile.
|
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.
|
static java.lang.String |
normalizeFilename(java.lang.String home,
java.lang.String filename)
Converts an absolute or relative filename (relative to the current
directory) into a filename that is relative to the specified 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 static FileList |
processFileSet(FileSet fileSet,
boolean caseSens)
Resolve the file set, by using the initial files (loaded from the reference directory or explicit file
names), and applying the Include and eXclude operations.
|
private java.lang.String |
putParameter(java.lang.String key,
java.lang.String value)
Associate a configuration parameter value with
key. |
static void |
setDefaultProfile(java.lang.String name)
Set the default profile.
|
static void |
setDefaultProfiles(java.util.List<java.lang.String> profiles)
Set the default profiles.
|
static void |
setImportMode()
Entering import mode.
|
void |
setProfileState(java.lang.Object key,
java.lang.Object state)
Save a state at the
activeProfile. |
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. |
void |
withDbProfile(java.lang.String db,
java.util.function.Consumer<java.lang.String> code)
Switch the profile to the one associated with the specified database namespace.
|
void |
withFileProfile(java.lang.String file)
Switch the profile to the one associated with the specified file.
|
public static final java.lang.String TAG_CFG
<cfg> XML tag.public static final java.lang.String TAG_GLOBAL
<global> XML tag.public static final java.lang.String TAG_PARAMETER
<parameter> XML tag.public static final java.lang.String TAG_FILE_SET
<file-set> XML tag.public static final java.lang.String TAG_DIRECTORY
<directory> XML tag.public static final java.lang.String TAG_FILENAME
<filename> XML tag.public static final java.lang.String TAG_EXCLUDE
<exclude> XML tag.public static final java.lang.String TAG_INCLUDE
<include> XML tag.public static final java.lang.String TAG_INCLUDE_MAPPINGS
include-mappings> XML tag.public static final java.lang.String TAG_SCHEMA
schema> XML tag.public static final java.lang.String TAG_PROFILE
profile> XML tag.public static final java.lang.String TAG_NAMESPACE
namespace> XML tag.public static final java.lang.String TAG_DIALECTSPECIFIC
dialect-specific> XML tag.public static final java.lang.String TAG_METADATA
metadata> XML tag.public static final java.lang.String TAG_TABLE
table> XML tag.public static final java.lang.String TAG_CVTPATH
cvt/.public 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
public static final java.lang.String DEF_CFG_FILE
public static final java.lang.String DEF_UNIT_TEST_TYPE
private static final CentralLogger LOG
private static volatile Configuration instance
private static volatile ProfileConfig globalConfig
private static boolean runtimeConfig
true then it is a server run time mode when configuration is NOT read from
the configuration directory.private static boolean importMode
private final java.util.Map<java.lang.String,java.lang.String> includeRemap
include remappings.private final java.util.Map<java.lang.String,java.lang.String> parmMap
private final java.util.Map<java.lang.String,ProfileConfig> profiles
private javax.xml.parsers.SAXParserFactory saxParserFactory
private javax.xml.parsers.SAXParser saxParser
private GenericSAXHandler saxHandler
private java.lang.String cvtpath
cvtpath parameter, as an absolute path.private java.util.Map<java.lang.String,FileSet> fileSet
file-set(s).private java.util.Map<java.lang.String,java.lang.String> fileProfile
private java.util.Map<java.lang.String,java.util.Set<java.lang.String>> dbProfile
private java.util.Set<java.lang.String> activeProfiles
private ProfileConfig activeProfile
globalConfig is assumed.private SchemaConfig schemaConfig
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 boolean isImportMode()
true if now running in import mode as described above.public static void setImportMode()
public static java.lang.String getConversionFolder()
throws ConfigurationException
This is either a relative folder to the home() directory, or an absolute path.
cvtpath parameter from p2j.cfg.xml>.ConfigurationException - If the conversion artifacts folder could not be resolved.public static SchemaConfig getSchemaConfig()
p2j.cfg.xml
that was parsed when this class was loaded.public static FileList getFileList()
fileSet.public static java.util.Map<java.lang.String,java.lang.String> getPathAliases()
public static java.util.Map<java.lang.String,java.lang.String> getIncludeFilePrefixMap()
null. Remappings are iterated in the order in which they were defined.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.public java.lang.Object getProfileState(java.lang.Object key)
active profile, for the specified key.key - The state's key.public void setProfileState(java.lang.Object key,
java.lang.Object state)
activeProfile.key - The state's key.state - The state to save.public static void loadConfigProfiles(java.util.List<java.lang.String> profiles)
If the profiles are empty or null, nothing is done.
If it contains only one profile, then loadProfile(String) is used for single-profile mode.
Otherwise, multi-profile mode is activated.
profiles - The profile list.public java.lang.String getActiveProfile()
public boolean isMultiProfile()
multi-profile mode.true if activeProfiles is set.public void withDbProfile(java.lang.String db,
java.util.function.Consumer<java.lang.String> code)
null
or no profile exists for this namespace, then the globalConfig is activated.db - The database namespace.code - The code to run for each profile loading this namespace.public void withFileProfile(java.lang.String file)
null
or no profile exists for this file, then the globalConfig is activated.file - The file name.private java.lang.String[] getPaths(ProfileConfig p)
p - The profile.private void loadConfigProfilesImpl(java.util.List<java.lang.String> profiles)
If the profiles are empty or null, nothing is done.
If it contains only one profile, then loadProfile(String) is used for single-profile mode.
Otherwise, multi-profile mode is activated.
profiles - The profile list.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()
private void loadProfile(java.lang.String name)
globalConfig to create the parameter mapping
(the profile parameters overwrite the ones from the global instance).
The includeRemap and the fileSet will be copied from the specified profile.
If we are in multi-profile mode, and the profile is the same as the
activeProfile, then this will do nothing, as the profile is already loaded.
name - The profile name.java.lang.IllegalArgumentException - If the profile name is not registered at profiles.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 java.lang.String normalizeFilename(java.lang.String home,
java.lang.String filename)
home - The home folder to use.filename - Absolute or current directory relative filename.public static Configuration getInstance()
public static boolean isCaseSensitive()
public static void setDefaultProfile(java.lang.String name)
name - The profile name.public static void setDefaultProfiles(java.util.List<java.lang.String> profiles)
profiles - The profiles to be loaded.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 FileList processFileSet(FileSet fileSet, boolean caseSens) throws java.io.IOException
fileSet - The file set.caseSens - Flag indicating if the legacy operating system is case-sensitive or not.java.io.IOException - In case of problems reading files from disk.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.