com.metamatrix.common.util
Class PropertiesUtils

java.lang.Object
  extended by com.metamatrix.common.util.PropertiesUtils

public final class PropertiesUtils
extends java.lang.Object

Static utility methods for common tasks having to do with java.util.Properties.


Nested Class Summary
static class PropertiesUtils.InvalidPropertyException
           
 
Constructor Summary
PropertiesUtils()
           
 
Method Summary
static java.util.Properties clone(java.util.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 java.util.Properties clone(java.util.Properties props, boolean makeUnmodifiable)
          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 java.util.Properties clone(java.util.Properties props, java.util.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 java.util.Properties clone(java.util.Properties props, java.util.Properties defaults, boolean deepClone, boolean makeUnmodifiable)
          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 int compare(java.util.Properties p1, java.util.Properties p2)
          This method implements a 'compareTo' logic for two Properties objects, equivalent to calling p1.compareTo(p2) if the Properties class implemented Comparable (which it does not).
static void copyProperty(java.util.Properties srcProperties, java.lang.String srcPropName, java.util.Properties tgtProperties, java.lang.String tgtPropName)
           
static java.util.List filter(java.lang.String pattern, java.util.Properties props)
          Returns a list of property names matching the given pattern.
static boolean filterTest(java.lang.String pattern, java.lang.String sCandidate)
          Returns a boolean indicating whether the string matched the given pattern.
static boolean getBooleanProperty(java.util.Properties props, java.lang.String propName, boolean defaultValue)
           
static double getDoubleProperty(java.util.Properties props, java.lang.String propName, double defaultValue)
           
static float getFloatProperty(java.util.Properties props, java.lang.String propName, float defaultValue)
           
static int getIntProperty(java.util.Properties props, java.lang.String propName, int defaultValue)
           
static long getLongProperty(java.util.Properties props, java.lang.String propName, long defaultValue)
           
static java.util.Properties getProperties(java.lang.String filterPattern, java.util.Properties props)
          Obtains from this source the list of all properties that match the pattern specified by the filter.
static java.util.Properties load(java.lang.String fileName)
           
static java.util.Properties loadAsResource(java.lang.Class clazz, java.lang.String resourceName)
           
static java.lang.String loadHeader(java.lang.String fileName)
          Read the header part of a properties file into a String.
static java.lang.String prettyPrint(java.util.Properties props)
           
static void print(java.io.PrintStream stream, java.util.Properties props)
           
static void print(java.lang.String fileName, java.util.Properties props)
          Write the specified properties to the specified file, with the specified header.
static void print(java.lang.String fileName, java.util.Properties props, java.lang.String header)
          Write the specified properties to the specified file, with the specified header.
static void putAll(java.util.Properties addToThis, java.util.Properties withThese)
          This method is intended to replace the use of the putAll method of Properties inherited from java.util.Hashtable.
static java.util.Properties resolveNestedProperties(java.util.Properties original)
          The speciality 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, then it will look up for the property value in the System properties.
static java.lang.String saveConvert(java.lang.String theString, boolean escapeSpace)
           
static void setBeanProperties(java.lang.Object bean, java.util.Properties props, java.lang.String prefix)
           
static void setOverrideProperies(java.util.Properties base, java.util.Properties override)
           
static java.util.Properties sort(java.util.Properties props)
           
static java.util.Collection sortPropertiesForPrinting(java.util.Properties props)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertiesUtils

public PropertiesUtils()
Method Detail

filter

public static java.util.List filter(java.lang.String pattern,
                                    java.util.Properties props)
Returns a list of property names matching the given pattern. A '*' may be given to match the pattern exactly up to the '*', then anything after. Note: Should also implement matching with '?' which means match any single char.

Parameters:
pattern - The property name to match which may include a '*'.
props - The properties to search.
Returns:
The List of property names matching given pattern - may be empty but never null.

getProperties

public static java.util.Properties getProperties(java.lang.String filterPattern,
                                                 java.util.Properties props)
Obtains from this source the list of all properties that match the pattern specified by the filter.

The filter is a string that may contain the '*' character as a wildcard one or more times in the string. For example, the following filter:

       *metamatrix.*

finds all properties that contain somewhere in the property name the string "metamatrix.".

Parameters:
filterPattern - the string filter pattern that will be used to narrow the set of properties returned.
props - The properties to search.
Returns:
the enumeration of all of the property names of the primary source; the enumeration may be empty if there is an error connecting to the property sources.

clone

public static java.util.Properties clone(java.util.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. If the input is an instance of UnmodifiableProperties, this method returns an UnmodifiableProperties instance around a new (flattened) copy of the underlying Properties object.


clone

public static java.util.Properties clone(java.util.Properties props,
                                         boolean makeUnmodifiable)
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. If an unmodifiable properties object is sought this method returns an UnmodifiableProperties instance around a new (flattened) copy of the underlying Properties object.


clone

public static java.util.Properties clone(java.util.Properties props,
                                         java.util.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. If the input is an instance of UnmodifiableProperties, this method returns an UnmodifiableProperties instance around a new (flattened) copy of the underlying Properties object.


clone

public static java.util.Properties clone(java.util.Properties props,
                                         java.util.Properties defaults,
                                         boolean deepClone,
                                         boolean makeUnmodifiable)
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. If an unmodifiable properties object is sought this method returns an UnmodifiableProperties instance around a new (flattened) copy of the underlying Properties object.


compare

public static int compare(java.util.Properties p1,
                          java.util.Properties p2)
This method implements a 'compareTo' logic for two Properties objects, equivalent to calling p1.compareTo(p2) if the Properties class implemented Comparable (which it does not).

Parameters:
p1 - the first Properties instance to compare; may be null
p2 - the second Properties instance to compare; may be null
Returns:
a negative integer, zero, or a positive integer as p1 is less than, equal to, or greater than p2, respectively.

putAll

public static void putAll(java.util.Properties addToThis,
                          java.util.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.

Parameters:
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.

setOverrideProperies

public static void setOverrideProperies(java.util.Properties base,
                                        java.util.Properties override)

getIntProperty

public static int getIntProperty(java.util.Properties props,
                                 java.lang.String propName,
                                 int defaultValue)
                          throws PropertiesUtils.InvalidPropertyException
Throws:
PropertiesUtils.InvalidPropertyException

getLongProperty

public static long getLongProperty(java.util.Properties props,
                                   java.lang.String propName,
                                   long defaultValue)

getFloatProperty

public static float getFloatProperty(java.util.Properties props,
                                     java.lang.String propName,
                                     float defaultValue)

getDoubleProperty

public static double getDoubleProperty(java.util.Properties props,
                                       java.lang.String propName,
                                       double defaultValue)

getBooleanProperty

public static boolean getBooleanProperty(java.util.Properties props,
                                         java.lang.String propName,
                                         boolean defaultValue)

loadHeader

public static java.lang.String loadHeader(java.lang.String fileName)
                                   throws java.io.IOException
Read the header part of a properties file into a String.

Parameters:
fileName -
Returns:
Throws:
java.io.IOException
Since:
4.3

load

public static java.util.Properties load(java.lang.String fileName)
                                 throws java.io.IOException
Throws:
java.io.IOException

loadAsResource

public static java.util.Properties loadAsResource(java.lang.Class clazz,
                                                  java.lang.String resourceName)
                                           throws java.io.IOException
Throws:
java.io.IOException

sort

public static java.util.Properties sort(java.util.Properties props)

print

public static void print(java.lang.String fileName,
                         java.util.Properties props,
                         java.lang.String header)
                  throws java.io.IOException
Write the specified properties to the specified file, with the specified header. Results may not be sorted.

Parameters:
fileName -
props -
header -
Throws:
java.io.IOException
Since:
4.3

print

public static void print(java.lang.String fileName,
                         java.util.Properties props)
                  throws java.io.IOException
Write the specified properties to the specified file, with the specified header. Results are sorted by property name.

Throws:
java.io.IOException

print

public static void print(java.io.PrintStream stream,
                         java.util.Properties props)

prettyPrint

public static java.lang.String prettyPrint(java.util.Properties props)

sortPropertiesForPrinting

public static java.util.Collection sortPropertiesForPrinting(java.util.Properties props)

saveConvert

public static java.lang.String saveConvert(java.lang.String theString,
                                           boolean escapeSpace)

copyProperty

public static final void copyProperty(java.util.Properties srcProperties,
                                      java.lang.String srcPropName,
                                      java.util.Properties tgtProperties,
                                      java.lang.String tgtPropName)

resolveNestedProperties

public static java.util.Properties resolveNestedProperties(java.util.Properties original)
                                                    throws java.io.IOException
The speciality 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, then it will look up for the property value in the System properties. Multiple nesting is OK, however recursive nested is not good, and behaviour is undocumented in this case.

Parameters:
original - - Original properties to be resolved
Returns:
resolved properties object.
Throws:
java.io.IOException
Since:
4.4

filterTest

public static boolean filterTest(java.lang.String pattern,
                                 java.lang.String sCandidate)
Returns a boolean indicating whether the string matched the given pattern. A '*' may be given to match the pattern exactly up to the '*', then anything after. We will also support a leading star, and match on anything that ends with the string specified after the star. Note: Should also implement matching with '?' which means match any single char.

Parameters:
pattern - The property name to match which may include a '*'.
props - The properties to search.
Returns:
The boolean - passed or failed but never null.

setBeanProperties

public static void setBeanProperties(java.lang.Object bean,
                                     java.util.Properties props,
                                     java.lang.String prefix)


Copyright © 2009. All Rights Reserved.