Top Description Fields Constructors Methods
com.openhtmltopdf.util

public Class Configuration

extends Object
Class Inheritance
Imports
java.io.*, java.util.*, java.util.logging.Level, .LogRecord, .Logger, java.lang.reflect.Field, java.net.URL, .MalformedURLException

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 Configuration#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.

Author
Patrick Wright

Field Summary

Modifier and TypeField and Description
private Logger
configLogger

Logger we use internally related to configuration.

private Level
logLevel

The log Level for Configuration messages; taken from show-config System property.

private Properties
properties

Our backing data store of properties.

private static final 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 List<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

AccessConstructor and Description
private
Configuration()

Default constructor.

Method Summary

Modifier and TypeMethod and Description
private void
fine(String
message to log
msg
)

Used internally to log a message about the class at level FINE

private void
finer(String
message to log
msg
)

Used internally to log a message about the class at level FINER

private String
private String
private void
public static boolean
private void
info(String
message to log
msg
)

Used internally to log a message about the class at level INFO

private static synchronized Configuration

Returns:

The singleton instance of the class.
instance
()

public static boolean

Returns:

Value assigned to the key, as a String.
isFalse
(String
Name of the property.
key
,
boolean
PARAM
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).

public static boolean

Returns:

Value assigned to the key, as a String.
isTrue
(String
Name of the property.
key
,
boolean
PARAM
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).

public static Iterator<String>

Returns:

Returns Iterator, see description.
keysByPrefix
(String
Prefix to filter on. No regex.
prefix
)

Returns all configuration keys that start with prefix.

private void
loadDefaultProperties()

Loads the default set of properties.

private void
loadOverrideProperties(String
Path to the file, or classpath URL, where properties are defined.
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 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(Level
the logging level to record the message at
level
,
String
the message to log
msg
)

Used internally for logging status/info about the class.

public static void
setConfigLogger(Logger
Logger used for Configuration-related messages
logger
)

Sets the logger which we use for Configuration-related logging.

public static int

Returns:

Value assigned to the key, as a String.
valueAsByte
(String
Name of the property.
key
,
byte
PARAM
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.

public static double

Returns:

Value assigned to the key, as a String.
valueAsDouble
(String
Name of the property.
key
,
double
PARAM
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.

public static float

Returns:

Value assigned to the key, as a String.
valueAsFloat
(String
Name of the property.
key
,
float
PARAM
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.

public static int

Returns:

Value assigned to the key, as a String.
valueAsInt
(String
Name of the property.
key
,
int
PARAM
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.

public static long

Returns:

Value assigned to the key, as a String.
valueAsLong
(String
Name of the property.
key
,
long
PARAM
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.

public static int

Returns:

Value assigned to the key, as a String.
valueAsShort
(String
Name of the property.
key
,
short
PARAM
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.

public static String

Returns:

Value assigned to the key, as a String.
valueFor
(String
Name of the property.
key
)

Returns the value for key in the Configuration.

public static String

Returns:

Value assigned to the key, as a String.
valueFor
(String
Name of the property.
key
,
String
PARAM
defaultVal
)

Returns the value for key in the Configuration, or the default provided value if not found.

public static Object

Returns:

Value of the constant, or defaultValue in case of error.
valueFromClassConstant
(String
Name of the property
key
,
Object
Returned in case of error.
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(String
message to log
msg
)

Used internally to log a message about the class at level WARNING

private void
warning(String
message to log
msg
,
Throwable
the exception to report
th
)

Used internally to log a message about the class at level WARNING, in case an exception was thrown

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configLoggerback to summary
private Logger configLogger

Logger we use internally related to configuration.

logLevelback to summary
private Level logLevel

The log Level for Configuration messages; taken from show-config System property.

propertiesback to summary
private Properties properties

Our backing data store of properties.

SF_FILE_NAMEback to summary
private static final String SF_FILE_NAME

The location of our default properties file; must be on the CLASSPATH.

sInstanceback to summary
private static Configuration sInstance

The Singleton instance of the class.

startupLogRecordsback to summary
private List<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 Detail

Configurationback to summary
private Configuration()

Default constructor. Will parse default configuration file, system properties, override properties, etc. and result in a usable Configuration instance.

Exceptions
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

fineback to summary
private void fine(String msg)

Used internally to log a message about the class at level FINE

Parameters
msg:String

message to log

finerback to summary
private void finer(String msg)

Used internally to log a message about the class at level FINER

Parameters
msg:String

message to log

getSystemPropertyOverrideFileNameback to summary
private String getSystemPropertyOverrideFileName()
getUserHomeOverrideFileNameback to summary
private String getUserHomeOverrideFileName()
handleUnexpectedExceptionOnInitback to summary
private void handleUnexpectedExceptionOnInit(Exception e)
hasValueback to summary
public static boolean hasValue(String key)
infoback to summary
private void info(String msg)

Used internally to log a message about the class at level INFO

Parameters
msg:String

message to log

instanceback to summary
private static synchronized Configuration instance()
Returns:Configuration

The singleton instance of the class.

isFalseback to summary
public static boolean isFalse(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:String

Name of the property.

defaultVal:boolean

PARAM

Returns:boolean

Value assigned to the key, as a String.

isTrueback to summary
public static boolean isTrue(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:String

Name of the property.

defaultVal:boolean

PARAM

Returns:boolean

Value assigned to the key, as a String.

keysByPrefixback to summary
public static Iterator<String> keysByPrefix(String prefix)

Returns all configuration keys that start with prefix. Iterator will be empty if no such keys are found.

Parameters
prefix:String

Prefix to filter on. No regex.

Returns:Iterator<String>

Returns Iterator, see description.

loadDefaultPropertiesback to summary
private void loadDefaultProperties()

Loads the default set of properties.

loadOverridePropertiesback to summary
private void loadOverrideProperties(String uri)

Loads overriding property values from a second configuration file; this is optional. See class documentation.

Parameters
uri:String

Path to the file, or classpath URL, where properties are defined.

loadSystemPropertiesback to summary
private void loadSystemProperties()

Loads overriding property values from a System properties; this is optional. See class documentation.

logAfterLoadback to summary
private void logAfterLoad()

Writes a log of loaded properties to the plumbing.init Logger.

newFallbackPropertiesback to summary
private 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.

printlnback to summary
private void println(Level level, String msg)

Used internally for logging status/info about the class.

Parameters
level:Level

the logging level to record the message at

msg:String

the message to log

setConfigLoggerback to summary
public static void setConfigLogger(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

Logger used for Configuration-related messages

valueAsByteback to summary
public static int valueAsByte(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:String

Name of the property.

defaultVal:byte

PARAM

Returns:int

Value assigned to the key, as a String.

valueAsDoubleback to summary
public static double valueAsDouble(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:String

Name of the property.

defaultVal:double

PARAM

Returns:double

Value assigned to the key, as a String.

valueAsFloatback to summary
public static float valueAsFloat(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:String

Name of the property.

defaultVal:float

PARAM

Returns:float

Value assigned to the key, as a String.

valueAsIntback to summary
public static int valueAsInt(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:String

Name of the property.

defaultVal:int

PARAM

Returns:int

Value assigned to the key, as a String.

valueAsLongback to summary
public static long valueAsLong(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:String

Name of the property.

defaultVal:long

PARAM

Returns:long

Value assigned to the key, as a String.

valueAsShortback to summary
public static int valueAsShort(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:String

Name of the property.

defaultVal:short

PARAM

Returns:int

Value assigned to the key, as a String.

valueForback to summary
public static String valueFor(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:String

Name of the property.

Returns:String

Value assigned to the key, as a String.

valueForback to summary
public static String valueFor(String key, 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:String

Name of the property.

defaultVal:String

PARAM

Returns:String

Value assigned to the key, as a String.

valueFromClassConstantback to summary
public static Object valueFromClassConstant(String key, 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:String

Name of the property

defaultValue:Object

Returned in case of error.

Returns:Object

Value of the constant, or defaultValue in case of error.

warningback to summary
private void warning(String msg)

Used internally to log a message about the class at level WARNING

Parameters
msg:String

message to log

warningback to summary
private void warning(String msg, Throwable th)

Used internally to log a message about the class at level WARNING, in case an exception was thrown

Parameters
msg:String

message to log

th:Throwable

the exception to report