public final class PropertiesUtil extends Object
Properties
utilities.Modifier and Type | Field and Description |
---|---|
static String |
SUFFIX_NULL_VALUE
Suffix appended to the real property-key to store the boolean flag whether
the real property represents the
null value. |
Modifier and Type | Method and Description |
---|---|
static Map<String,String> |
filterProperties(Map<?,?> rawProperties)
Filter the given raw properties.
|
static Map<String,String> |
filterProperties(Map<?,?> rawProperties,
Map<?,?> variables)
Filter the given raw properties.
|
static String |
getMetaPropertyKeyNullValue(String key)
Get the
null -indicating meta-property's key for the given real property's key. |
static Properties |
getProperties(Properties properties,
String keyPrefix)
Get all properties whose keys start with the given prefix.
|
static Collection<Matcher> |
getPropertyKeyMatches(Properties properties,
Pattern pattern)
Get all matches where property keys match the given pattern.
|
static String |
getReferencedPropertyKeyForMetaPropertyKey(String key)
Get the referenced property-key for the given meta-property's key.
|
static int |
getSystemPropertyValueAsInt(String key,
int defaultValue) |
static long |
getSystemPropertyValueAsLong(String key,
long defaultValue) |
static boolean |
isMetaPropertyKey(String key)
Determine, if the given property-key is a meta-property for another property.
|
static boolean |
isMetaPropertyKeyNullValue(String key)
Determine, if the given property-key is a
null -indicating meta-property for another property. |
static boolean |
isNullValue(Map<?,?> properties,
String key)
Determine, if the property identified by the given
key has a null -value. |
static Properties |
load(File file) |
static Properties |
load(String filename) |
static void |
putAll(Properties source,
Properties target) |
static void |
store(File file,
Properties properties,
String comment) |
static void |
store(String filename,
Properties properties,
String comment) |
public static final String SUFFIX_NULL_VALUE
null
value.
It is not possible to store a null
value in a Properties
instance (and neither it is
in a properties file). But sometimes it is necessary to explicitly formulate a null
value,
for example when overriding a property in a way as if it had not been specified in the overridden properties.
For example, let there be these properties declared in a persistence unit:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.TransactionType = JTA javax.persistence.jtaDataSource = jdbc/someDataSource javax.persistence.transactionType = JTA
If the transaction type is to be overridden by "RESOURCE_LOCAL", this is straight-forward:
javax.jdo.option.TransactionType = RESOURCE_LOCAL javax.persistence.transactionType = RESOURCE_LOCAL
But to override the datasource properties to be null, is not possible by simply writing
"javax.jdo.option.ConnectionFactoryName = = " as this would
be interpreted as empty string. Therefore it is possible to declare the null
value using an additional
key:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.ConnectionFactoryName.null = true javax.persistence.jtaDataSource.null = trueIt is not necessary to quote the referenced key as shown in the 2nd example ("javax.persistence.jtaDataSource" is not present). However, if it is present ("javax.jdo.option.ConnectionFactoryName" above), the null-indicating meta-property "javax.jdo.option.ConnectionFactoryName.null" overrides the value "jdbc/someDataSource".
public static Collection<Matcher> getPropertyKeyMatches(Properties properties, Pattern pattern)
properties
- The properties to matchpattern
- The pattern to match againstMatcher
s that matched.public static Properties getProperties(Properties properties, String keyPrefix)
The returned property elements have the form (key,value)
where key
is
the part of the original key after the given keyPrefix
and value
is
the original value.
properties
- The properties to filter.keyPrefix
- The kex prefix to usepublic static void putAll(Properties source, Properties target)
public static Properties load(String filename) throws IOException
IOException
public static Properties load(File file) throws IOException
IOException
public static void store(String filename, Properties properties, String comment) throws IOException
IOException
public static void store(File file, Properties properties, String comment) throws IOException
IOException
public static Map<String,String> filterProperties(Map<?,?> rawProperties)
This is a convenience method delegating to
filterProperties(Map, Map)
with variables == null
.
rawProperties
- the properties to be filtered; must not be null
.filterProperties(Map, Map)
public static Map<String,String> filterProperties(Map<?,?> rawProperties, Map<?,?> variables)
Replace null-meta-data to null
values: Every property for which the
method isNullValue(Map, String)
returns true
is written with a null
value into the result-map. Every property for which the method isMetaPropertyKeyNullValue(String)
returns true
, is ignored (i.e. does not occur in the result-map).
It is not possible to store a null
value in a Properties
instance (and neither it is
in a properties file). But sometimes it is necessary to explicitly formulate a null
value,
for example when overriding a property in a way as if it had not been specified in the overridden properties.
For example, let there be these properties declared in a persistence unit:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.TransactionType = JTA javax.persistence.jtaDataSource = jdbc/someDataSource javax.persistence.transactionType = JTA
If the transaction type is to be overridden by "RESOURCE_LOCAL", this is straight-forward:
javax.jdo.option.TransactionType = RESOURCE_LOCAL javax.persistence.transactionType = RESOURCE_LOCAL
But to override the datasource properties to be null, is not possible by simply writing
"javax.jdo.option.ConnectionFactoryName = = " as this would
be interpreted as empty string. Therefore it is possible to declare the null
value using an additional
key:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.ConnectionFactoryName.null = true javax.persistence.jtaDataSource.null = trueIt is not necessary to quote the referenced key as shown in the 2nd example ("javax.persistence.jtaDataSource" is not present). However, if it is present ("javax.jdo.option.ConnectionFactoryName" above), the null-indicating meta-property "javax.jdo.option.ConnectionFactoryName.null" overrides the value "jdbc/someDataSource".
Replace template variables: If the optional variables
argument is present, every
value is filtered using IOUtil.replaceTemplateVariables(String, Map)
.
For example, let there be these properties:
some.url1 = file:/tmp/myTempDir some.url2 = http://host.domain.tld/tomcat
If this method is called with System.getProperties()
as variables
and the user "marco" is currently
working on a linux machine, the resulting Map
will contain the following resolved properties:
some.url1 = file:/tmp/myTempDir some.url2 = http://host.domain.tld/marco
rawProperties
- the properties to be filtered; must not be null
.variables
- optional template variables; if present, every value is filtered using
IOUtil.replaceTemplateVariables(String, Map)
.public static boolean isMetaPropertyKeyNullValue(String key)
null
-indicating meta-property for another property.
It is not possible to store a null
value in a Properties
instance (and neither it is
in a properties file). But sometimes it is necessary to explicitly formulate a null
value,
for example when overriding a property in a way as if it had not been specified in the overridden properties.
For example, let there be these properties declared in a persistence unit:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.TransactionType = JTA javax.persistence.jtaDataSource = jdbc/someDataSource javax.persistence.transactionType = JTA
If the transaction type is to be overridden by "RESOURCE_LOCAL", this is straight-forward:
javax.jdo.option.TransactionType = RESOURCE_LOCAL javax.persistence.transactionType = RESOURCE_LOCAL
But to override the datasource properties to be null, is not possible by simply writing
"javax.jdo.option.ConnectionFactoryName = = " as this would
be interpreted as empty string. Therefore it is possible to declare the null
value using an additional
key:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.ConnectionFactoryName.null = true javax.persistence.jtaDataSource.null = trueIt is not necessary to quote the referenced key as shown in the 2nd example ("javax.persistence.jtaDataSource" is not present). However, if it is present ("javax.jdo.option.ConnectionFactoryName" above), the null-indicating meta-property "javax.jdo.option.ConnectionFactoryName.null" overrides the value "jdbc/someDataSource".
key
- the property-key to check.true
, if the given key references a property that is a null
-indicating
meta-property for another property.isMetaPropertyKey(String)
public static boolean isMetaPropertyKey(String key)
Currently, this is equivalent to isMetaPropertyKeyNullValue(String)
, but other
meta-properties might be introduced later.
key
- the property-key to check.true
, if the given key references a property that is a meta-property
for another property.public static String getReferencedPropertyKeyForMetaPropertyKey(String key)
key
- a meta-property's key - for example the null
-indicating property-key
"some.prop.null".SUFFIX_NULL_VALUE
,
getMetaPropertyKeyNullValue(String)
public static String getMetaPropertyKeyNullValue(String key)
null
-indicating meta-property's key for the given real property's key.key
- a property-key - for example "some.prop".null
-indicating meta-property's key - for example "some.prop.null".SUFFIX_NULL_VALUE
,
getReferencedPropertyKeyForMetaPropertyKey(String)
public static boolean isNullValue(Map<?,?> properties, String key)
key
has a null
-value.
It is not possible to store a null
value in a Properties
instance (and neither it is
in a properties file). But sometimes it is necessary to explicitly formulate a null
value,
for example when overriding a property in a way as if it had not been specified in the overridden properties.
For example, let there be these properties declared in a persistence unit:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.TransactionType = JTA javax.persistence.jtaDataSource = jdbc/someDataSource javax.persistence.transactionType = JTA
If the transaction type is to be overridden by "RESOURCE_LOCAL", this is straight-forward:
javax.jdo.option.TransactionType = RESOURCE_LOCAL javax.persistence.transactionType = RESOURCE_LOCAL
But to override the datasource properties to be null, is not possible by simply writing
"javax.jdo.option.ConnectionFactoryName = = " as this would
be interpreted as empty string. Therefore it is possible to declare the null
value using an additional
key:
javax.jdo.option.ConnectionFactoryName = jdbc/someDataSource javax.jdo.option.ConnectionFactoryName.null = true javax.persistence.jtaDataSource.null = trueIt is not necessary to quote the referenced key as shown in the 2nd example ("javax.persistence.jtaDataSource" is not present). However, if it is present ("javax.jdo.option.ConnectionFactoryName" above), the null-indicating meta-property "javax.jdo.option.ConnectionFactoryName.null" overrides the value "jdbc/someDataSource".
properties
- the properties. Must not be null
.key
- the property-key for which to determine, whether its value is null
.true
, if the property referenced by the given key
is null
;
false
otherwise.public static int getSystemPropertyValueAsInt(String key, int defaultValue)
public static long getSystemPropertyValueAsLong(String key, long defaultValue)
Copyright © 2013-2014. All Rights Reserved.