public final class PropertiesUtil extends Object
Provides utility methods for managing Properties
instances as well as access to the global configuration
system. Properties by default are loaded from the system properties, system environment, and a classpath resource
file named . Additional properties can be loaded by implementing a custom
PropertySource
service and specifying it via a ServiceLoader
file called
META-INF/services/org.apache.logging.log4j.util.PropertySource
with a list of fully qualified class names
implementing that interface.
PropertySource
Constructor and Description |
---|
PropertiesUtil(Properties props)
Constructs a PropertiesUtil using a given Properties object as its source of defined properties.
|
PropertiesUtil(String propertiesFileName)
Constructs a PropertiesUtil for a given properties file name on the classpath.
|
Modifier and Type | Method and Description |
---|---|
static Properties |
extractSubset(Properties properties,
String prefix)
Extracts properties that start with or are equals to the specific prefix and returns them in a new Properties
object with the prefix removed.
|
boolean |
getBooleanProperty(String name)
Gets the named property as a boolean value.
|
Boolean |
getBooleanProperty(String[] prefixes,
String key,
Supplier<Boolean> supplier)
Retrieves a property that may be prefixed by more than one string.
|
boolean |
getBooleanProperty(String name,
boolean defaultValue)
Gets the named property as a boolean value.
|
boolean |
getBooleanProperty(String name,
boolean defaultValueIfAbsent,
boolean defaultValueIfPresent)
Gets the named property as a boolean value.
|
Charset |
getCharsetProperty(String name)
Gets the named property as a Charset value.
|
Charset |
getCharsetProperty(String name,
Charset defaultValue)
Gets the named property as a Charset value.
|
double |
getDoubleProperty(String name,
double defaultValue)
Gets the named property as a double.
|
Duration |
getDurationProperty(String[] prefixes,
String key,
Supplier<Duration> supplier)
Retrieves a property that may be prefixed by more than one string.
|
Duration |
getDurationProperty(String name,
Duration defaultValue)
Retrieves a Duration where the String is of the format nnn[unit] where nnn represents an integer value
and unit represents a time unit.
|
Integer |
getIntegerProperty(String[] prefixes,
String key,
Supplier<Integer> supplier)
Retrieves a property that may be prefixed by more than one string.
|
int |
getIntegerProperty(String name,
int defaultValue)
Gets the named property as an integer.
|
Long |
getLongProperty(String[] prefixes,
String key,
Supplier<Long> supplier)
Retrieves a property that may be prefixed by more than one string.
|
long |
getLongProperty(String name,
long defaultValue)
Gets the named property as a long.
|
static PropertiesUtil |
getProperties()
Returns the PropertiesUtil used by Log4j.
|
String |
getStringProperty(String name)
Gets the named property as a String.
|
String |
getStringProperty(String[] prefixes,
String key,
Supplier<String> supplier)
Retrieves a property that may be prefixed by more than one string.
|
String |
getStringProperty(String name,
String defaultValue)
Gets the named property as a String.
|
static Properties |
getSystemProperties()
Return the system properties or an empty Properties object if an error occurs.
|
boolean |
hasProperty(String name)
Returns
true if the specified property is defined, regardless of its value (it may not have a value). |
boolean |
isOsWindows()
Returns true if system properties tell us we are running on Windows.
|
static Map<String,Properties> |
partitionOnCommonPrefixes(Properties properties)
Partitions a properties map based on common key prefixes up to the first period.
|
void |
reload()
Reloads all properties.
|
public PropertiesUtil(Properties props)
props
- the Properties to use by defaultpublic PropertiesUtil(String propertiesFileName)
propertiesFileName
- the location of properties file to loadpublic static PropertiesUtil getProperties()
public boolean hasProperty(String name)
true
if the specified property is defined, regardless of its value (it may not have a value).name
- the name of the property to verifytrue
if the specified property is defined, regardless of its valuepublic boolean getBooleanProperty(String name)
"true"
(case-insensitive),
then it is returned as the boolean value true
. Any other non-null
text in the property is
considered false
.name
- the name of the property to look upfalse
if undefined.public boolean getBooleanProperty(String name, boolean defaultValue)
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefineddefaultValue
if undefined.public boolean getBooleanProperty(String name, boolean defaultValueIfAbsent, boolean defaultValueIfPresent)
name
- the name of the property to look updefaultValueIfAbsent
- the default value to use if the property is undefineddefaultValueIfPresent
- the default value to use if the property is defined but not assigneddefaultValue
if undefined.public Boolean getBooleanProperty(String[] prefixes, String key, Supplier<Boolean> supplier)
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned
if no property is found.public Charset getCharsetProperty(String name)
name
- the name of the property to look upCharset.defaultCharset()
if undefined.public Charset getCharsetProperty(String name, Charset defaultValue)
Log4j-charsets.properties
on the class path.name
- the name of the property to look updefaultValue
- the default value to use if the property is undefineddefaultValue
if undefined.public double getDoubleProperty(String name, double defaultValue)
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefineddefaultValue
if it was undefined or could not be parsed.public int getIntegerProperty(String name, int defaultValue)
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefineddefaultValue
if it was undefined or could not be
parsed.public Integer getIntegerProperty(String[] prefixes, String key, Supplier<Integer> supplier)
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned
if no property is found.public long getLongProperty(String name, long defaultValue)
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefineddefaultValue
if it was undefined or could not be parsed.public Long getLongProperty(String[] prefixes, String key, Supplier<Long> supplier)
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned
if no property is found.public Duration getDurationProperty(String name, Duration defaultValue)
name
- The property name.defaultValue
- The default value.public Duration getDurationProperty(String[] prefixes, String key, Supplier<Duration> supplier)
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned
if no property is found.public String getStringProperty(String[] prefixes, String key, Supplier<String> supplier)
prefixes
- The array of prefixes.key
- The key to locate.supplier
- The method to call to derive the default value. If the value is null, null will be returned
if no property is found.public String getStringProperty(String name)
name
- the name of the property to look upnull
if undefined.public String getStringProperty(String name, String defaultValue)
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefineddefaultValue
if undefined.public static Properties getSystemProperties()
public void reload()
public static Properties extractSubset(Properties properties, String prefix)
properties
- The Properties to evaluate.prefix
- The prefix to extract.public static Map<String,Properties> partitionOnCommonPrefixes(Properties properties)
properties
- properties to partitionpublic boolean isOsWindows()
Copyright © 1999-1969 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.