Class Configuration
- java.lang.Object
-
- com.openhtmltopdf.util.Configuration
-
public class Configuration extends java.lang.Object
Stores runtime configuration information for application parameters that may vary on restarting. This implements the Singleton pattern, but through static methods. That is, the first time Configuration is used, the properties are loaded into the Singleton instance. Subsequent calls to valueFor() retrieve values from the Singleton. To look up a property, use
valueFor(String)
.Properties may be overridden using a second properties file, or individually using System properties specified on the command line. To override using a second properties file, specify the System property xr-conf. This should be the location of the second file relative to the CLASSPATH, or else a file path, e.g.
java -Dxr-conf=resources/conf/myprops.conf
You can also place your override properties file in your user home directory, in
${user.home}/.flyingsaucer/local.xhtmlrenderer.conf
To override a property using the System properties, just re-define the property on the command line. e.g.
java -Dxr.property-name=new_value
The order in which these will be read is: default properties (bundled with the core, in the jar; override configuration properties; properties file in user.home; and system properties.
You can override as many properties as you like.
Note that overrides are driven by the property names in the default configuration file. Specifying a property name not in that file will have no effect--the property will not be loaded or available for lookup. Configuration is NOT used to control logging levels or output; see LogStartupConfig.
There are convenience converstion method for all the primitive types, in methods like
valueAsInt(String, int)
. A default must always be provided for these methods. The default is returned if the value is not found, or if the conversion from String fails. If the value is not present, or the conversion fails, a warning message is written to the log.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.logging.Logger
configLogger
Logger we use internally related to configuration.private java.util.logging.Level
logLevel
The log Level for Configuration messages; taken from show-config System property.private java.util.Properties
properties
Our backing data store of properties.private static java.lang.String
SF_FILE_NAME
The location of our default properties file; must be on the CLASSPATH.private static Configuration
sInstance
The Singleton instance of the class.private java.util.List<java.util.logging.LogRecord>
startupLogRecords
List of LogRecords for messages from Configuration startup; used to hold these temporarily as we can't use XRLog while starting up, as it depends on Configuration.
-
Constructor Summary
Constructors Modifier Constructor Description private
Configuration()
Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
fine(java.lang.String msg)
Used internally to log a message about the class at level FINEprivate void
finer(java.lang.String msg)
Used internally to log a message about the class at level FINERprivate java.lang.String
getSystemPropertyOverrideFileName()
private java.lang.String
getUserHomeOverrideFileName()
private void
handleUnexpectedExceptionOnInit(java.lang.Exception e)
static boolean
hasValue(java.lang.String key)
private void
info(java.lang.String msg)
Used internally to log a message about the class at level INFOprivate static Configuration
instance()
static boolean
isFalse(java.lang.String key, boolean defaultVal)
Returns true if the value is not "true" (ignores case), or the default provided value if not found or if the value is not a valid boolean (true or false, ignores case).static boolean
isTrue(java.lang.String key, boolean defaultVal)
Returns true if the value is "true" (ignores case), or the default provided value if not found or if the value is not a valid boolean (true or false, ignores case).static java.util.Iterator<java.lang.String>
keysByPrefix(java.lang.String prefix)
Returns all configuration keys that start with prefix.private void
loadDefaultProperties()
Loads the default set of properties.private void
loadOverrideProperties(java.lang.String uri)
Loads overriding property values from a second configuration file; this is optional.private void
loadSystemProperties()
Loads overriding property values from a System properties; this is optional.private void
logAfterLoad()
Writes a log of loaded properties to the plumbing.init Logger.private java.util.Properties
newFallbackProperties()
Returns a Properties instance filled with values of last resort--in case we can't read default properties file for some reason; this is to prevent Configuration init from throwing any exceptions, or ending up with a completely empty configuration instance.private void
println(java.util.logging.Level level, java.lang.String msg)
Used internally for logging status/info about the class.static void
setConfigLogger(java.util.logging.Logger logger)
Sets the logger which we use for Configuration-related logging.static int
valueAsByte(java.lang.String key, byte defaultVal)
Returns the value for key in the Configuration as a byte, or the default provided value if not found or if the value is not a valid byte.static double
valueAsDouble(java.lang.String key, double defaultVal)
Returns the value for key in the Configuration as a double, or the default provided value if not found or if the value is not a valid double.static float
valueAsFloat(java.lang.String key, float defaultVal)
Returns the value for key in the Configuration as a float, or the default provided value if not found or if the value is not a valid float.static int
valueAsInt(java.lang.String key, int defaultVal)
Returns the value for key in the Configuration as an integer, or a default value if not found or if the value is not a valid integer.static long
valueAsLong(java.lang.String key, long defaultVal)
Returns the value for key in the Configurationas a long, or the default provided value if not found or if the value is not a valid long.static int
valueAsShort(java.lang.String key, short defaultVal)
Returns the value for key in the Configuration as a short, or the default provided value if not found or if the value is not a valid short.static java.lang.String
valueFor(java.lang.String key)
Returns the value for key in the Configuration.static java.lang.String
valueFor(java.lang.String key, java.lang.String defaultVal)
Returns the value for key in the Configuration, or the default provided value if not found.static java.lang.Object
valueFromClassConstant(java.lang.String key, java.lang.Object defaultValue)
Given a property, resolves the value to a public constant field on some class, where the field is of type Object.private void
warning(java.lang.String msg)
Used internally to log a message about the class at level WARNINGprivate void
warning(java.lang.String msg, java.lang.Throwable th)
Used internally to log a message about the class at level WARNING, in case an exception was thrown
-
-
-
Field Detail
-
properties
private java.util.Properties properties
Our backing data store of properties.
-
logLevel
private java.util.logging.Level logLevel
The log Level for Configuration messages; taken from show-config System property.
-
sInstance
private static Configuration sInstance
The Singleton instance of the class.
-
startupLogRecords
private java.util.List<java.util.logging.LogRecord> startupLogRecords
List of LogRecords for messages from Configuration startup; used to hold these temporarily as we can't use XRLog while starting up, as it depends on Configuration.
-
configLogger
private java.util.logging.Logger configLogger
Logger we use internally related to configuration.
-
SF_FILE_NAME
private static final java.lang.String SF_FILE_NAME
The location of our default properties file; must be on the CLASSPATH.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Configuration
private Configuration()
Default constructor. Will parse default configuration file, system properties, override properties, etc. and result in a usable Configuration instance.- Throws:
java.lang.RuntimeException
- if any stage of loading configuration results in an Exception. This could happen, for example, if the default configuration file was not readable.
-
-
Method Detail
-
handleUnexpectedExceptionOnInit
private void handleUnexpectedExceptionOnInit(java.lang.Exception e)
-
setConfigLogger
public static void setConfigLogger(java.util.logging.Logger logger)
Sets the logger which we use for Configuration-related logging. Before this is called the first time, all internal log records are queued up; they are flushed to the logger when this method is first called. Afterwards, all log events are written to this logger. This queueing behavior helps avoid order-of-operations bugs related to loading configuration information related to logging.- Parameters:
logger
- Logger used for Configuration-related messages
-
println
private void println(java.util.logging.Level level, java.lang.String msg)
Used internally for logging status/info about the class.- Parameters:
level
- the logging level to record the message atmsg
- the message to log
-
info
private void info(java.lang.String msg)
Used internally to log a message about the class at level INFO- Parameters:
msg
- message to log
-
warning
private void warning(java.lang.String msg)
Used internally to log a message about the class at level WARNING- Parameters:
msg
- message to log
-
warning
private void warning(java.lang.String msg, java.lang.Throwable th)
Used internally to log a message about the class at level WARNING, in case an exception was thrown- Parameters:
msg
- message to logth
- the exception to report
-
fine
private void fine(java.lang.String msg)
Used internally to log a message about the class at level FINE- Parameters:
msg
- message to log
-
finer
private void finer(java.lang.String msg)
Used internally to log a message about the class at level FINER- Parameters:
msg
- message to log
-
loadDefaultProperties
private void loadDefaultProperties()
Loads the default set of properties.
-
loadOverrideProperties
private void loadOverrideProperties(java.lang.String uri)
Loads overriding property values from a second configuration file; this is optional. See class documentation.- Parameters:
uri
- Path to the file, or classpath URL, where properties are defined.
-
getSystemPropertyOverrideFileName
private java.lang.String getSystemPropertyOverrideFileName()
-
getUserHomeOverrideFileName
private java.lang.String getUserHomeOverrideFileName()
-
loadSystemProperties
private void loadSystemProperties()
Loads overriding property values from a System properties; this is optional. See class documentation.
-
logAfterLoad
private void logAfterLoad()
Writes a log of loaded properties to the plumbing.init Logger.
-
valueFor
public static java.lang.String valueFor(java.lang.String key)
Returns the value for key in the Configuration. A warning is issued to the log if the property is not defined.- Parameters:
key
- Name of the property.- Returns:
- Value assigned to the key, as a String.
-
hasValue
public static boolean hasValue(java.lang.String key)
-
valueAsByte
public static int valueAsByte(java.lang.String key, byte defaultVal)
Returns the value for key in the Configuration as a byte, or the default provided value if not found or if the value is not a valid byte. A warning is issued to the log if the property is not defined, or if the conversion from String fails.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
valueAsShort
public static int valueAsShort(java.lang.String key, short defaultVal)
Returns the value for key in the Configuration as a short, or the default provided value if not found or if the value is not a valid short. A warning is issued to the log if the property is not defined, or if the conversion from String fails.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
valueAsInt
public static int valueAsInt(java.lang.String key, int defaultVal)
Returns the value for key in the Configuration as an integer, or a default value if not found or if the value is not a valid integer. A warning is issued to the log if the property is not defined, or if the conversion from String fails.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
valueAsLong
public static long valueAsLong(java.lang.String key, long defaultVal)
Returns the value for key in the Configurationas a long, or the default provided value if not found or if the value is not a valid long. A warning is issued to the log if the property is not defined, or if the conversion from String fails.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
valueAsFloat
public static float valueAsFloat(java.lang.String key, float defaultVal)
Returns the value for key in the Configuration as a float, or the default provided value if not found or if the value is not a valid float. A warning is issued to the log if the property is not defined, or if the conversion from String fails.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
valueAsDouble
public static double valueAsDouble(java.lang.String key, double defaultVal)
Returns the value for key in the Configuration as a double, or the default provided value if not found or if the value is not a valid double. A warning is issued to the log if the property is not defined, or if the conversion from String fails.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
valueFor
public static java.lang.String valueFor(java.lang.String key, java.lang.String defaultVal)
Returns the value for key in the Configuration, or the default provided value if not found. A warning is issued to the log if the property is not defined, and if the default is null.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
keysByPrefix
public static java.util.Iterator<java.lang.String> keysByPrefix(java.lang.String prefix)
Returns all configuration keys that start with prefix. Iterator will be empty if no such keys are found.- Parameters:
prefix
- Prefix to filter on. No regex.- Returns:
- Returns Iterator, see description.
-
isTrue
public static boolean isTrue(java.lang.String key, boolean defaultVal)
Returns true if the value is "true" (ignores case), or the default provided value if not found or if the value is not a valid boolean (true or false, ignores case). A warning is issued to the log if the property is not defined, and if the default is null.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
isFalse
public static boolean isFalse(java.lang.String key, boolean defaultVal)
Returns true if the value is not "true" (ignores case), or the default provided value if not found or if the value is not a valid boolean (true or false, ignores case). A warning is issued to the log if the property is not defined, or the value is not a valid boolean.- Parameters:
key
- Name of the property.defaultVal
- PARAM- Returns:
- Value assigned to the key, as a String.
-
instance
private static Configuration instance()
- Returns:
- The singleton instance of the class.
-
valueFromClassConstant
public static java.lang.Object valueFromClassConstant(java.lang.String key, java.lang.Object defaultValue)
Given a property, resolves the value to a public constant field on some class, where the field is of type Object. The property value must the the FQN of the class and field, e.g. aKey=java.awt.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR will return the value of the VALUE_INTERPOLATION_NEAREST_NEIGHBOR constant on the RendingHints class.- Parameters:
key
- Name of the propertydefaultValue
- Returned in case of error.- Returns:
- Value of the constant, or defaultValue in case of error.
-
newFallbackProperties
private java.util.Properties newFallbackProperties()
Returns a Properties instance filled with values of last resort--in case we can't read default properties file for some reason; this is to prevent Configuration init from throwing any exceptions, or ending up with a completely empty configuration instance.
-
-