- java.lang.Object
-
- org.apache.logging.log4j.util.PropertiesUtil
-
- All Implemented Interfaces:
PropertyEnvironment
@InternalApi public class PropertiesUtil extends Object implements PropertyEnvironment
Consider this class private.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 customPropertySource
service and specifying it via aServiceLoader
file calledMETA-INF/services/org.apache.logging.log4j.util.PropertySource
with a list of fully qualified class names implementing that interface.- See Also:
PropertySource
-
-
Constructor Summary
Constructors Constructor Description PropertiesUtil(String propertiesFileName)
Constructs a PropertiesUtil for a given properties file name on the classpath.PropertiesUtil(String contextName, String propertiesFileName)
Constructs a PropertiesUtil for a given properties file name on the classpath.PropertiesUtil(Properties props)
Constructs a PropertiesUtil using a given Properties object as its source of defined properties.PropertiesUtil(Properties props, boolean includeInvalid)
Constructs a PropertiesUtil using a given Properties object as its source of defined properties.PropertiesUtil(PropertySource source)
Constructs a PropertiesUtil for a given property source as source of additional properties.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addPropertySource(PropertySource propertySource)
Allows a PropertySource to be added after PropertiesUtil has been created.static void
clearThreadProperties()
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.static ResourceBundle
getCharsetsResourceBundle()
static PropertiesUtil
getContextProperties(ClassLoader classLoader, String contextName)
Get the properties associated with a LoggerContext that is associated with a ClassLoader.static PropertiesUtil
getContextProperties(String contextName)
Get the properties for a LoggerContext just by the name.static PropertiesUtil
getContextProperties(String contextName, PropertiesUtil propertiesUtil)
Get the properties for a LoggerContext just by the name.double
getDoubleProperty(String name, double defaultValue)
Gets the named property as a double.static PropertiesUtil
getProperties()
Returns the PropertiesUtil used by Log4j.static PropertyEnvironment
getProperties(String namespace)
String
getStringProperty(String name)
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)
Returnstrue
if the specified property is defined, regardless of its value (it may not have a value).static boolean
hasThreadProperties()
static Map<String,Properties>
partitionOnCommonPrefixes(Properties properties)
Partitions a properties map based on common key prefixes up to the first period.static Map<String,Properties>
partitionOnCommonPrefixes(Properties properties, boolean includeBaseKey)
Partitions a properties map based on common key prefixes up to the first period.void
reload()
Reloads all properties.static void
setThreadProperties(PropertiesUtil properties)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.logging.log4j.util.PropertyEnvironment
getBooleanProperty, getBooleanProperty, getBooleanProperty, getBooleanProperty, getBooleanProperty, getBooleanProperty, getBooleanProperty, getCharsetProperty, getCharsetProperty, getCharsetProperty, getCharsetProperty, getDurationProperty, getDurationProperty, getDurationProperty, getIntegerProperty, getIntegerProperty, getIntegerProperty, getLongProperty, getLongProperty, getLongProperty, getStringProperty, getStringProperty, getStringProperty, getStringProperty, hasProperty, isOsWindows
-
-
-
-
Constructor Detail
-
PropertiesUtil
public PropertiesUtil(Properties props)
Constructs a PropertiesUtil using a given Properties object as its source of defined properties.- Parameters:
props
- the Properties to use by default
-
PropertiesUtil
public PropertiesUtil(Properties props, boolean includeInvalid)
Constructs a PropertiesUtil using a given Properties object as its source of defined properties.- Parameters:
props
- the Properties to use by defaultincludeInvalid
- includes invalid properties.
-
PropertiesUtil
public PropertiesUtil(String propertiesFileName)
Constructs a PropertiesUtil for a given properties file name on the classpath. The properties specified in this file are used by default. If a property is not defined in this file, then the equivalent system property is used.- Parameters:
propertiesFileName
- the location of properties file to load
-
PropertiesUtil
public PropertiesUtil(String contextName, String propertiesFileName)
Constructs a PropertiesUtil for a given properties file name on the classpath. The properties specified in this file are used by default. If a property is not defined in this file, then the equivalent system property is used.- Parameters:
propertiesFileName
- the location of properties file to load
-
PropertiesUtil
public PropertiesUtil(PropertySource source)
Constructs a PropertiesUtil for a given property source as source of additional properties.- Parameters:
source
- a property source
-
-
Method Detail
-
hasThreadProperties
public static boolean hasThreadProperties()
-
setThreadProperties
public static void setThreadProperties(PropertiesUtil properties)
-
clearThreadProperties
public static void clearThreadProperties()
-
getProperties
public static PropertiesUtil getProperties()
Returns the PropertiesUtil used by Log4j.- Returns:
- the main Log4j PropertiesUtil instance.
-
getProperties
public static PropertyEnvironment getProperties(String namespace)
-
getCharsetsResourceBundle
public static ResourceBundle getCharsetsResourceBundle()
-
addPropertySource
public void addPropertySource(PropertySource propertySource)
Description copied from interface:PropertyEnvironment
Allows a PropertySource to be added after PropertiesUtil has been created.- Specified by:
addPropertySource
in interfacePropertyEnvironment
- Parameters:
propertySource
- the PropertySource to add.
-
hasProperty
public boolean hasProperty(String name)
Returnstrue
if the specified property is defined, regardless of its value (it may not have a value).- Specified by:
hasProperty
in interfacePropertyEnvironment
- Parameters:
name
- the name of the property to verify- Returns:
true
if the specified property is defined, regardless of its value
-
getDoubleProperty
public double getDoubleProperty(String name, double defaultValue)
Gets the named property as a double.- Parameters:
name
- the name of the property to look updefaultValue
- the default value to use if the property is undefined- Returns:
- the parsed double value of the property or
defaultValue
if it was undefined or could not be parsed.
-
getStringProperty
public String getStringProperty(String name)
Gets the named property as a String.- Specified by:
getStringProperty
in interfacePropertyEnvironment
- Parameters:
name
- the name of the property to look up- Returns:
- the String value of the property or
null
if undefined.
-
getSystemProperties
public static Properties getSystemProperties()
Return the system properties or an empty Properties object if an error occurs.- Returns:
- The system properties.
-
reload
public void reload()
Reloads all properties. This is primarily useful for unit tests.- Since:
- 2.10.0
-
getContextProperties
public static PropertiesUtil getContextProperties(String contextName)
Get the properties for a LoggerContext just by the name. This ALWAYS creates a new PropertiesUtil. Use locateProperties to obtain the current properties.- Parameters:
contextName
- The context name.- Returns:
- The PropertiesUtil that was created.
-
getContextProperties
public static PropertiesUtil getContextProperties(String contextName, PropertiesUtil propertiesUtil)
Get the properties for a LoggerContext just by the name. This method is used for testing.- Parameters:
contextName
- The context name.- Returns:
- The PropertiesUtil that was created.
-
getContextProperties
public static PropertiesUtil getContextProperties(ClassLoader classLoader, String contextName)
Get the properties associated with a LoggerContext that is associated with a ClassLoader. This ALWAYS creates a new PropertiesUtil. Use locateProperties to obtain the current properties.- Parameters:
classLoader
- The ClassLoader.contextName
- The context name.- Returns:
- The PropertiesUtil created.
-
extractSubset
public 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.- Parameters:
properties
- The Properties to evaluate.prefix
- The prefix to extract.- Returns:
- The subset of properties.
-
partitionOnCommonPrefixes
public static Map<String,Properties> partitionOnCommonPrefixes(Properties properties)
Partitions a properties map based on common key prefixes up to the first period.- Parameters:
properties
- properties to partition- Returns:
- the partitioned properties where each key is the common prefix (minus the period) and the values are new property maps without the prefix and period in the key
- Since:
- 2.6
-
partitionOnCommonPrefixes
public static Map<String,Properties> partitionOnCommonPrefixes(Properties properties, boolean includeBaseKey)
Partitions a properties map based on common key prefixes up to the first period.- Parameters:
properties
- properties to partitionincludeBaseKey
- when true if a key exists with no '.' the key will be included.- Returns:
- the partitioned properties where each key is the common prefix (minus the period) and the values are new property maps without the prefix and period in the key
- Since:
- 2.17.2
-
-