public final class PropertiesUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
PropertiesUtils.InvalidPropertyException |
Constructor and Description |
---|
PropertiesUtils() |
Modifier and Type | Method and Description |
---|---|
static Properties |
clone(Properties props)
Performs a correct deep clone of the properties object by capturing
all properties in the default(s) and placing them directly into the
new Properties object.
|
static Properties |
clone(Properties props,
Properties defaults,
boolean deepClone)
Performs a correct deep clone of the properties object by capturing
all properties in the default(s) and placing them directly into the
new Properties object.
|
static boolean |
getBooleanProperty(Properties props,
String propName,
boolean defaultValue) |
static double |
getDoubleProperty(Properties props,
String propName,
double defaultValue) |
static float |
getFloatProperty(Properties props,
String propName,
float defaultValue) |
static String |
getHierarchicalProperty(String key,
String defaultValue)
Search for the property first in the environment, then in the system properties
|
static <T> T |
getHierarchicalProperty(String key,
T defaultValue,
Class<T> clazz)
Search for the property first in the environment, then in the system properties
|
static int |
getIntProperty(Properties props,
String propName,
int defaultValue) |
static long |
getLongProperty(Properties props,
String propName,
long defaultValue) |
static Properties |
load(String fileName) |
static Properties |
loadAsResource(Class clazz,
String resourceName) |
static Properties |
loadFromURL(URL url) |
static String |
loadHeader(String fileName)
Read the header part of a properties file into a String.
|
static void |
print(PrintStream stream,
Properties props) |
static void |
print(String fileName,
Properties props)
Write the specified properties to the specified file,
with the specified header.
|
static void |
print(String fileName,
Properties props,
String header)
Write the specified properties to the specified file,
with the specified header.
|
static void |
putAll(Properties addToThis,
Properties withThese)
This method is intended to replace the use of the
putAll
method of Properties inherited from java.util.Hashtable . |
static Properties |
resolveNestedProperties(Properties original)
The specialty of nested properties is, in a given property file
there can be values with pattern like "${...}"
key1=value1
key2=${key1}/value2
where the value of the key2 should resolve to value1/value2
also if the property in the pattern ${..} is not found in the loaded
properties, an exception is thrown. |
static String |
saveConvert(String theString,
boolean escapeSpace) |
static void |
setBeanProperties(Object bean,
Properties props,
String prefix) |
static void |
setBeanProperties(Object bean,
Properties props,
String prefix,
boolean caseSensitive) |
static void |
setBeanProperty(Object bean,
String name,
Object value) |
static Collection |
sortPropertiesForPrinting(Properties props) |
static String |
toHex(byte[] bytes) |
static char |
toHex(int nibble)
Convert a nibble to a hex character
|
static void |
toHex(StringBuilder sb,
InputStream is) |
public static Properties clone(Properties props)
UnmodifiableProperties
, this method returns an
UnmodifiableProperties
instance around a new (flattened)
copy of the underlying Properties object.public static Properties clone(Properties props, Properties defaults, boolean deepClone)
UnmodifiableProperties
, this method returns an
UnmodifiableProperties
instance around a new (flattened)
copy of the underlying Properties object.public static void putAll(Properties addToThis, Properties withThese)
This method is intended to replace the use of the putAll
method of Properties
inherited from java.util.Hashtable
.
The problem with that method is that, since it is inherited from
Hashtable
, default properties are lost.
For example, the following code
Properties a;
Properties b;
//initialize ...
a.putAll(b);
will fail if b
had been constructed with a default
Properties
object. Those defaults would be lost and
not added to a
.
The above code could be correctly performed with this method, like this:
Properties a;
Properties b;
//initialize ...
PropertiesUtils.putAll(a,b);
In the above example, a
is modified - properties are added to
it (note that if a
has defaults they will remain unaffected.)
The properties from b
, including defaults, will be
added to a
using its setProperty
method -
these new properties will overwrite any pre-existing ones of the same
name.
addToThis
- This Properties object is modified; the properties
of the other parameter are added to this. The added property values
will replace any current property values of the same names.withThese
- The properties (including defaults) of this
object are added to the "addToThis" parameter.public static int getIntProperty(Properties props, String propName, int defaultValue) throws PropertiesUtils.InvalidPropertyException
public static long getLongProperty(Properties props, String propName, long defaultValue)
public static float getFloatProperty(Properties props, String propName, float defaultValue)
public static double getDoubleProperty(Properties props, String propName, double defaultValue)
public static boolean getBooleanProperty(Properties props, String propName, boolean defaultValue)
public static String loadHeader(String fileName) throws IOException
fileName
- IOException
public static Properties load(String fileName) throws IOException
IOException
public static Properties loadFromURL(URL url) throws MalformedURLException, IOException
MalformedURLException
IOException
public static Properties loadAsResource(Class clazz, String resourceName) throws IOException
IOException
public static void print(String fileName, Properties props, String header) throws IOException
fileName
- props
- header
- IOException
public static void print(String fileName, Properties props) throws IOException
IOException
public static void print(PrintStream stream, Properties props)
public static Collection sortPropertiesForPrinting(Properties props)
public static char toHex(int nibble)
nibble
- the nibble to convert.public static String toHex(byte[] bytes)
public static void toHex(StringBuilder sb, InputStream is) throws IOException
IOException
public static Properties resolveNestedProperties(Properties original)
key1=value1
key2=${key1}/value2
where the value of the key2
should resolve to value1/value2
also if the property in the pattern ${..}
is not found in the loaded
properties, an exception is thrown. Multiple nesting is OK, however recursive nested is not supported.original
- - Original properties to be resolvedpublic static void setBeanProperties(Object bean, Properties props, String prefix)
public static void setBeanProperties(Object bean, Properties props, String prefix, boolean caseSensitive)
public static String getHierarchicalProperty(String key, String defaultValue)
key
- defaultValue
- clazz
- Copyright © 2018 JBoss by Red Hat. All rights reserved.