Package com.jalios.util
Class JProperties
- java.lang.Object
-
- java.util.Dictionary<K,V>
-
- java.util.Hashtable<K,V>
-
- com.jalios.util.JProperties
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.util.Map<java.lang.String,java.lang.String>
- Direct Known Subclasses:
LangProperties
public class JProperties extends java.util.Hashtable<K,V>This class, based onPropertiesmanage properties file in UTF-8 encoding. Its saveupdateAndSave(File)methods keep layout of previous properties file.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.StringENCRYPTED_EXTENSIONprotected java.util.Set<JPropertiesListener>listenerSetprotected static intPREFIX_PROPERTIES_CACHE_MAX_ENTRIESprotected java.util.Map<java.lang.String,JProperties>prefixPropertiesCacheprotected booleanpropertyNameAsDefaultValueprotected java.util.Set<java.lang.String>SENSITIVE_KEYWORD
-
Constructor Summary
Constructors Modifier Constructor Description JProperties()protectedJProperties(JProperties jProperties)Constructor with a parent JProperties (used only for resolve linked properties when subProperties feature is used.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddProperties(JProperties prop)Add every property of the givenJPropertiesto thisJProperties.voidaddProperties(java.util.Properties prop)Add every property of the givenPropertiesto thisJProperties.voidaddPropertiesListener(JPropertiesListener l)Adds a listener for any change of this JProperties.voidaddProtectedKeys(java.util.Collection<java.lang.String> col)protected static java.lang.String[]buildArrayFromString(java.lang.String value, java.lang.String[] defaultValue)Convert a string to a String[] using the following formats: "@{delim}value1{delim}value2...", where{delim}is one character delimiter of your choice. "value1{delim}value2{delim}...", where{delim}is a character from\t\n\r\f.protected JPropertiescheckCacheGetProperties(java.lang.String prefix)voidclear()voidclearProperties()Clear all properties.booleancontainsProperty(java.lang.String name)Tests if this JProperties contains the given property key/name.voidfirePropertiesChange()Triggers a properties change to notify all listener of this JProperties.booleangetBooleanProperty(java.lang.String name, boolean defaultValue)Gets the value of a property as a boolean given its key/name.doublegetDoubleProperty(java.lang.String name, double defaultValue)Gets the value of a property as a double value given its key/name.floatgetFloatProperty(java.lang.String name, float defaultValue)Gets the value of a property as a float value given its key/name.int[]getIntArrayProperty(java.lang.String name, int[] defaultValue)Gets the value of a property as a int array.intgetIntegerProperty(java.lang.String name, int defaultValue)Gets the value of a property as an int value given its key/name.java.util.Set<JPropertiesListener>getListenerSet()Retrieve set of allJPropertiesListeneruse by this JProperties instance.java.util.Set<java.io.File>getLoadedFiles()Retrieve a set of all that were loaded in this JProperties instance (ordered by the load order)longgetLongProperty(java.lang.String name, long defaultValue)Gets the value of a property as a long value given its key/name.JPropertiesgetProperties(java.lang.String prefix)Returns a new property set of properties with key matching a given prefix.java.lang.StringgetProperty(java.lang.String name)Gets value of a property.java.lang.StringgetProperty(java.lang.String name, java.lang.String defaultValue)Gets the value of a property given its key/name.java.util.Set<java.lang.String>getProtectedKeySet()java.lang.String[]getStringArrayProperty(java.lang.String name, java.lang.String[] defaultValue)Gets the value of a property as a String array.java.util.Map<java.lang.String,java.lang.Object>getSubProperties(java.lang.String prefix)Returns a map of properties built from those properties and having a given prefix.
Example:
prop:
foo.bar.case1.x: value of x for case1
foo.bar.case1.y: value of y for case1
foo.bar.case2.x: value of x for case2
foo.bar.case2.y: value of y for case2
foo.bar.case2.z.t: value of z for case2voidinvalidateCacheGetProperties()Invalidate the cache used forgetProperties(String)method.booleanisSensitiveProperty(java.lang.String key)Check if the specified property key is considered to be sensitive and for which values is being concealed or crypted on saved.voidload(java.io.File file)Reads a property file (key and element pairs) from the given File.voidload(java.io.File file, boolean unprotectSensitiveData)Reads a property file (key and element pairs) from the given File.voidload(java.io.File file, java.lang.String prefix)Reads a property file (key and element pairs) from the given File.voidload(java.io.File file, java.lang.String prefix, boolean unprotectSensitiveData)Reads a property file (key and element pairs) from the given File.static JPropertiesparse(java.lang.String str)static JPropertiesparse(java.lang.String str, boolean decodeDoubleBackslashedChar)Vput(K key, V value)protected voidputCacheGetProperties(java.lang.String prefix, JProperties prop)Vremove(java.lang.Object key)voidremovePropertiesListener(JPropertiesListener l)Removes a listener for any change of this JProperties.voidsetListenerSet(java.util.Set<JPropertiesListener> listeners)Set the Set ofJPropertiesListenerthat this JProperties instance must used to notifyjava.lang.ObjectsetProperty(java.lang.String name, java.lang.String value)Sets value of a property.java.lang.StringtoString()voidupdateAndSave(java.io.File file)Updates and save properties.-
Methods inherited from class java.util.Hashtable
clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, merge, putAll, putIfAbsent, rehash, remove, replace, replace, replaceAll, size, values
-
-
-
-
Field Detail
-
PREFIX_PROPERTIES_CACHE_MAX_ENTRIES
protected static final int PREFIX_PROPERTIES_CACHE_MAX_ENTRIES
- See Also:
- Constant Field Values
-
prefixPropertiesCache
protected java.util.Map<java.lang.String,JProperties> prefixPropertiesCache
-
propertyNameAsDefaultValue
protected boolean propertyNameAsDefaultValue
-
listenerSet
protected java.util.Set<JPropertiesListener> listenerSet
-
SENSITIVE_KEYWORD
protected java.util.Set<java.lang.String> SENSITIVE_KEYWORD
-
ENCRYPTED_EXTENSION
protected java.lang.String ENCRYPTED_EXTENSION
-
-
Constructor Detail
-
JProperties
public JProperties()
-
JProperties
protected JProperties(JProperties jProperties)
Constructor with a parent JProperties (used only for resolve linked properties when subProperties feature is used.- Parameters:
jProperties- parentJPropertiesobject, used for linked property resolution
-
-
Method Detail
-
getListenerSet
public java.util.Set<JPropertiesListener> getListenerSet()
Retrieve set of allJPropertiesListeneruse by this JProperties instance.- Returns:
- the set of all listeners for this JProperties.
- Since:
- public since jcms-10.0.3 - JCMS-6890
- See Also:
addPropertiesListener(JPropertiesListener),removePropertiesListener(JPropertiesListener)
-
setListenerSet
public void setListenerSet(java.util.Set<JPropertiesListener> listeners)
Set the Set ofJPropertiesListenerthat this JProperties instance must used to notify- Parameters:
listeners- a Set of JProperties listener to assigne (listener are cleared if null is specified)- Since:
- jcms-10.0.3 - JCMS-6890
- See Also:
addPropertiesListener(JPropertiesListener),removePropertiesListener(JPropertiesListener)
-
addPropertiesListener
public void addPropertiesListener(JPropertiesListener l)
Adds a listener for any change of this JProperties.- Parameters:
l- the listener to add- See Also:
removePropertiesListener(JPropertiesListener)
-
removePropertiesListener
public void removePropertiesListener(JPropertiesListener l)
Removes a listener for any change of this JProperties.- Parameters:
l- the listener to remove- See Also:
addPropertiesListener(JPropertiesListener)
-
firePropertiesChange
public void firePropertiesChange()
Triggers a properties change to notify all listener of this JProperties.
-
containsProperty
public boolean containsProperty(java.lang.String name)
Tests if this JProperties contains the given property key/name.- Parameters:
name- the key/name of the property to check- Returns:
- true if the specified object is a key in this hashtable; false otherwise.
-
getProperty
public java.lang.String getProperty(java.lang.String name)
Gets value of a property.- Parameters:
name- the key/name of the property to retrieve- Returns:
- the property value
-
getProperty
public java.lang.String getProperty(java.lang.String name, java.lang.String defaultValue)Gets the value of a property given its key/name.- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null.- Returns:
- the value of a property given its key/name.
-
setProperty
public java.lang.Object setProperty(java.lang.String name, java.lang.String value)Sets value of a property.- Parameters:
name- the key/name of the property to retrievevalue- the new value to assign.- Returns:
- the previous value of the specified key/name
-
getBooleanProperty
public boolean getBooleanProperty(java.lang.String name, boolean defaultValue)Gets the value of a property as a boolean given its key/name.- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null or is neither "false" nor "true" (case sensitive).- Returns:
- the boolean value of the property
-
getIntegerProperty
public int getIntegerProperty(java.lang.String name, int defaultValue)Gets the value of a property as an int value given its key/name.- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null or if Integer.parseInt() throws exception during String to int conversion.- Returns:
- the int value of the property
-
getLongProperty
public long getLongProperty(java.lang.String name, long defaultValue)Gets the value of a property as a long value given its key/name.- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null or if Long.parseLong() throws exception during String to long conversion.- Returns:
- the long value of the property
-
getFloatProperty
public float getFloatProperty(java.lang.String name, float defaultValue)Gets the value of a property as a float value given its key/name.- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null or if Float.parseFloat() throws exception during String to float conversion.- Returns:
- the float value of the property
-
getDoubleProperty
public double getDoubleProperty(java.lang.String name, double defaultValue)Gets the value of a property as a double value given its key/name.- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null or if Double.parseDouble() throws exception during String to double conversion.- Returns:
- the double value of the property
-
getStringArrayProperty
public java.lang.String[] getStringArrayProperty(java.lang.String name, java.lang.String[] defaultValue)Gets the value of a property as a String array.The property value must be in one of the following forms:
- "@{delim}value1{delim}value2...", where
{delim}is one character delimiter of your choice. - "value1{delim}value2{delim}...", where
{delim}is a character from\t\n\r\f.
String[] array = prop.getStringArrayProperty("missingexample", new String[] { "default", "value"}); assertEquals(2, array.length); assertEquals("default", array[0]); assertEquals("value", array[1]); prop.setProperty("example1", "@|abc|def"); String[] array1 = prop.getStringArrayProperty("example1", new String[] { }); assertEquals(2, array1.length); assertEquals("abc", array1[0]); assertEquals("def", array1[1]); prop.setProperty("example2", "foo bar\tzig"); String[] array2 = prop.getStringArrayProperty("example2", new String[] { }); assertEquals(3, array2.length); assertEquals("foo", array2[0]); assertEquals("bar", array2[1]); assertEquals("zig", array2[2]);- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null or no values could be found in the conversion.- Returns:
- a String array
- "@{delim}value1{delim}value2...", where
-
buildArrayFromString
protected static java.lang.String[] buildArrayFromString(java.lang.String value, java.lang.String[] defaultValue)Convert a string to a String[] using the following formats:- "@{delim}value1{delim}value2...", where
{delim}is one character delimiter of your choice. - "value1{delim}value2{delim}...", where
{delim}is a character from\t\n\r\f.
- Parameters:
value- the valuedefaultValue- the default value- Returns:
- an array of String
- "@{delim}value1{delim}value2...", where
-
getIntArrayProperty
public int[] getIntArrayProperty(java.lang.String name, int[] defaultValue)Gets the value of a property as a int array. Uses same format ofgetStringArrayProperty(String, String[]).- Parameters:
name- the key/name of the property to retrievedefaultValue- the default value to use if the existing value is null or no values could be found in the conversion.- Returns:
- an int array
- See Also:
getStringArrayProperty(String, String[])
-
addProperties
public void addProperties(java.util.Properties prop)
Add every property of the givenPropertiesto thisJProperties.- Parameters:
prop- the properties to be added
-
addProperties
public void addProperties(JProperties prop)
Add every property of the givenJPropertiesto thisJProperties.- Parameters:
prop- the jproperties to be added
-
checkCacheGetProperties
protected JProperties checkCacheGetProperties(java.lang.String prefix)
-
putCacheGetProperties
protected void putCacheGetProperties(java.lang.String prefix, JProperties prop)
-
invalidateCacheGetProperties
public void invalidateCacheGetProperties()
Invalidate the cache used forgetProperties(String)method.- Since:
- jcms-8.0.2
-
getProperties
public JProperties getProperties(java.lang.String prefix)
Returns a new property set of properties with key matching a given prefix.- Parameters:
prefix- the prefix the properties must match to be added to the returned JProperties instance, if null, all properties are added.- Returns:
- a new property set of properties with key matching a given prefix.
-
getSubProperties
public java.util.Map<java.lang.String,java.lang.Object> getSubProperties(java.lang.String prefix)
Returns a map of properties built from those properties and having a given prefix.
Example:
prop:
foo.bar.case1.x: value of x for case1
foo.bar.case1.y: value of y for case1
foo.bar.case2.x: value of x for case2
foo.bar.case2.y: value of y for case2
foo.bar.case2.z.t: value of z for case2
prop.getSubProperties("foo.bar.")returns a Map with the keyscase1andcase2.
A JProperties is bound to
case1and it contains the properties:
x: value of x for case1
y: value of y for case1
A JProperties is bound to
case2and it contains the properties:
x: value of x for case2
y: value of y for case2
z.t: value of z for case2- Parameters:
prefix- the prefix- Returns:
- a map with string as the key and either String or JProperties as the value.
- Since:
- jcms-5.7.2
-
clearProperties
public void clearProperties()
Clear all properties.
-
load
public void load(java.io.File file) throws java.io.IOExceptionReads a property file (key and element pairs) from the given File. The file encoding is assumed to be UTF-8.- Parameters:
file- the file from which to read properties.- Throws:
java.io.IOException- if an error occurred when reading from the input stream.java.lang.IllegalArgumentException- if the input stream contains a malformed Unicode escape sequence.- See Also:
Properties.load(java.io.InputStream)
-
load
public void load(java.io.File file, boolean unprotectSensitiveData) throws java.io.IOExceptionReads a property file (key and element pairs) from the given File. The file encoding is assumed to be UTF-8.- Parameters:
file- the file from which to read properties.unprotectSensitiveData- true if sensitive data should be unprotected after read- Throws:
java.io.IOException- if an error occurred when reading from the input stream.java.lang.IllegalArgumentException- if the input stream contains a malformed Unicode escape sequence.- See Also:
Properties.load(java.io.InputStream)
-
load
public void load(java.io.File file, java.lang.String prefix) throws java.io.IOExceptionReads a property file (key and element pairs) from the given File. The file encoding is assumed to be UTF-8.- Parameters:
file- the file from which to read properties.prefix- a prefix to use add to all properties' name. if null, no prefix is used.- Throws:
java.io.IOException- if an error occurred when reading from the input stream.java.lang.IllegalArgumentException- if the input stream contains a malformed Unicode escape sequence.- See Also:
Properties.load(java.io.InputStream)
-
load
public void load(java.io.File file, java.lang.String prefix, boolean unprotectSensitiveData) throws java.io.IOExceptionReads a property file (key and element pairs) from the given File. The file encoding is assumed to be UTF-8.- Parameters:
file- the file from which to read properties.prefix- a prefix to use add to all properties' name. if null, no prefix is used.unprotectSensitiveData- true if sensitive data should be unprotected after read- Throws:
java.io.IOException- if an error occurred when reading from the input stream.java.lang.IllegalArgumentException- if the input stream contains a malformed Unicode escape sequence.- See Also:
Properties.load(java.io.InputStream)
-
getLoadedFiles
public java.util.Set<java.io.File> getLoadedFiles()
Retrieve a set of all that were loaded in this JProperties instance (ordered by the load order)- Returns:
- a Set, never returns null
- Since:
- JCMS-6735
-
updateAndSave
public void updateAndSave(java.io.File file) throws java.io.IOExceptionUpdates and save properties. The original layout is preserved for existing properties and comments New properties are appended.- Parameters:
file- the properties File to read and update.- Throws:
java.io.IOException- IOException thrown in method
-
isSensitiveProperty
public boolean isSensitiveProperty(java.lang.String key)
Check if the specified property key is considered to be sensitive and for which values is being concealed or crypted on saved.- Parameters:
key- the name of the property being verified- Returns:
- true if property is sensitive, false otherwise
- Since:
- jcms-10.0.3 / JCMS-7225 (initally introduce as private in jcms-10.0.2 / JCMS-6928)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.util.Hashtable<java.lang.String,java.lang.String>
-
parse
public static JProperties parse(java.lang.String str)
-
parse
public static JProperties parse(java.lang.String str, boolean decodeDoubleBackslashedChar)
-
put
public V put(K key, V value)- Specified by:
putin interfacejava.util.Map<K,V>- Overrides:
putin classjava.util.Hashtable<K,V>
-
remove
public V remove(java.lang.Object key)
- Specified by:
removein interfacejava.util.Map<K,V>- Overrides:
removein classjava.util.Hashtable<K,V>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<K,V>- Overrides:
clearin classjava.util.Hashtable<K,V>
-
getProtectedKeySet
public java.util.Set<java.lang.String> getProtectedKeySet()
-
addProtectedKeys
public void addProtectedKeys(java.util.Collection<java.lang.String> col)
-
-