Class XProperties
- java.lang.Object
-
- java.util.Dictionary<K,V>
-
- java.util.Hashtable<Object,Object>
-
- java.util.Properties
-
- edu.uiuc.ncsa.security.core.configuration.XProperties
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<Object,Object>
- Direct Known Subclasses:
NSProperties
,XXProperties
public class XProperties extends Properties
A subclass of java.util.Properties with many, many features. This is huge, boring and extremely useful.Usage
This may be used wherever aProperties
object may be used.Lifecycle
Instantiate as needed. Since this is a subclass of Properties, it is effectively a replacement for it. The specific problems it solves are as follows.- methods for saving/getting specific types (see below) This saves having a lot of little snippets to make things like dates and numbers over and over. Also, error handling is better and only runtime exceptions are ever thrown.
- methods for adding (vs. simply replacing) values from another property file
- methods for loading from other sources without replacing all the properties
- a few more convenient constructors
There is a main method to test this. It takes a single argument which is a file name which will be created (thereby allowing a test of writing a file and reading it.)
Property types
Here is a list of the types supported. The type is given in the first column and either the suffix for the setter of getter is given (soInt
as an entry means there are two methods namedsetInt
andgetInt
) or the explicit names of these are given. The last column gives a bit more description.Property Summary Type Set/Get Description java.util.Date
Date
A standard Java date. This is actually stored as a long. integer
Int
An integer
.long
Long
A long
String
String
A string. Note that the values are escaped in the resulting properties file. boolean
Boolean
A boolean
value. This automatically parses values ofyes, on, enable(d), ok, true, yup, yeah, 1
to be logical true andfalse,no, disable(d), nope, off, nay, 0
to be logical false. These are case insensitive. If the value is not recognized an exception is thrown.bytes array
Bytes
This will store or retrieve an arbitrary array of bytes url, uri
URL, URI
urls and uris. double
Double
a double value serializable java object
Serializable
Works if the object implements the java.io.Serializable
interface.serializable list
SerializableList
An array of java objects each of which implements the java.io.Serializable
interface.file
getFile(key), getFile(key, parent), setFileName
This will store a file name in the properties. There are two getters, so that if the users needs to resolve the file against a specific parent (vs. the java default of the invocation directory) it may be done. - Version:
- 1.0
- Author:
- Jeff Gaynor
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_LIST_SEPARATOR
The default separator for lists.static String[]
LOGICAL_FALSES
Strings this will treat as equivalent to logical false.static String[]
LOGICAL_TRUES
Strings that this will treat as equivalent to logical true.protected String
storeFileName
-
Fields inherited from class java.util.Properties
defaults
-
-
Constructor Summary
Constructors Constructor Description XProperties()
Creates a new instance of XPropertyXProperties(File file)
Create a new XProperties object by parsing a properties file.XProperties(String fileName)
Convenience method.XProperties(Properties p)
Same as the constructor in the superclass, this gives this object all the same properties as the argument.XProperties(Properties[] sources, boolean overwrite)
This will allow for an array of Properties objects.XProperties(Properties[] sources, boolean[] overwrite)
This will instantiate the properties taking the value ssupplied.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Map source, boolean overwrite)
void
add(Properties[] sources, boolean overwrite)
Adds each of the given array of properties.void
add(Properties[] sources, boolean[] overwrite)
Adds each property set with a flag for the set indicating overwrite permission.Object
clone()
Creates a deep copy of this object.boolean
equals(Object obj)
Checks if this XProperties object is the same as a given object.ArrayList<String>
getArrayList(String key)
ArrayList<String>
getArrayList(String key, String separator)
return list as anArrayList
.boolean
getBoolean(String key)
Retrieves the boolean value.byte[]
getBytes(String key)
Returns the value associated with this key as a byte array.XProperties
getClone()
Gets a clone of this object.Date
getDate(String key)
Get the value as a date.double
getDouble(String key)
Attempts to retrieve the value associated with the key as a double.File
getFile(String key)
Retrieve the file stored by this key, resolving it against the given invocation directory.File
getFile(String key, File parent)
Retrieve the file stored by this key, resolving it against the given file.int
getInt(String key)
Get the value associated with this key as an integer.String[]
getList(String key)
Gets the value associated with the key and splits it into an array of strings using the value fromgetListSeparator
String[]
getList(String key, String separator)
This will take a c list separated by a string and return a vector of strings.String
getListSeparator()
Get the list separator.long
getLong(String key)
Get the property as a long.Object
getSerializable(String key)
Gets a single serializable java object.Object[]
getSerializableList(String key)
Retrieves a list of serializable objects from the property.String
getStoreFileName()
Get the name of the store fileString
getString(String key)
Gets the value corresponding to the key as a string.URI
getURI(String key)
Get the value as a uriURL
getURL(String key)
Gets the value as a URL.boolean
isEmpty()
This will returntrue
if the current properties has no entries andfalse
otherwise.boolean
isList(String key)
boolean
isList(String key, String separator)
check if a string is in list format.protected boolean
keySubset(XProperties b)
Checks that the keys of b are a subset of the keys in thisXProperties
.void
load()
Loads the content of the file ingetStoreFileName
void
load(File f)
Loads from a file rather than from a stream.void
load(String storeFileName)
Load the content of the given file into the current properties object, over-writing any duplicate values.static void
main(String[] args)
This exists simply for testing this object.void
setBoolean(String key, boolean value)
Sets the given boolean value.void
setBytes(String key, byte[] ba)
Sets the value for the given key to the byte array.void
setDate(String key, Date d)
Set the value of with the given Date.void
setDouble(String key, double value)
Set the value associated with this key to the given double value.void
setFile(String key, File f)
Puts the file name into the properties, (not) the contents! This will not be portable between platforms, since this is the full path, properly escaped.void
setInt(String key, int value)
Set the property from the integer value.void
setList(String key, String list)
This sets a list directly, so the list itself is not touched.void
setList(String key, String[] list)
Set the value of a list using the default separator.void
setList(String key, String[] list, String separator)
Sets the value from an array of strings, delimiting them with the supplied separator.void
setListSeparator(String listSeparator)
Set the list separatorvoid
setLong(String key, long value)
Set the value of the property from the give long.Object
setProperty(String key, String value)
A bug fix for Properties...void
setSerializable(String key, Serializable object)
Retrieves a single serializable java object.Don't forget that you need to have any classes for this available to the current virtual machine to correctly deserialize the result.void
setSerializableList(String key, Object[] oList)
Stores a list of serializable java objects in the properties.void
setStoreFileName(String fileName)
Sets the name of the file that calling the defaultstore()
method will write to.void
setString(String key, String value)
Sets the value of the key with the given string.void
setURI(String key, String uriString)
A convenience.void
setURI(String key, URI uri)
Sets the value for the key from the URIvoid
setURL(String key, String urlString)
A convenience.void
setURL(String key, URL url)
Set the value from the given URLvoid
store()
This just invokesstore
with an empty header.void
store(String header)
Writes the properties to the store file.String
toString(int indent)
-
Methods inherited from class java.util.Properties
clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, forEach, get, getOrDefault, getProperty, getProperty, hashCode, keys, keySet, list, list, load, load, loadFromXML, merge, propertyNames, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, size, store, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, toString, values
-
-
-
-
Field Detail
-
DEFAULT_LIST_SEPARATOR
public static String DEFAULT_LIST_SEPARATOR
The default separator for lists. This is a comma.
-
storeFileName
protected String storeFileName
-
LOGICAL_TRUES
public static String[] LOGICAL_TRUES
Strings that this will treat as equivalent to logical true.
-
LOGICAL_FALSES
public static String[] LOGICAL_FALSES
Strings this will treat as equivalent to logical false.
-
-
Constructor Detail
-
XProperties
public XProperties()
Creates a new instance of XProperty
-
XProperties
public XProperties(String fileName)
Convenience method. This loads all the properties in the given file.- Parameters:
fileName
-
-
XProperties
public XProperties(File file)
Create a new XProperties object by parsing a properties file.- Parameters:
file
- the (standard) properties file.
-
XProperties
public XProperties(Properties p)
Same as the constructor in the superclass, this gives this object all the same properties as the argument.Grrrrr this is busted in the super class (!!). This works here...
-
XProperties
public XProperties(Properties[] sources, boolean overwrite)
This will allow for an array of Properties objects. Note that the order is important if overwriting is enabled, since this starts with an empty properties list so earlier sources have right of way over later ones.- Parameters:
sources
- an array ofProperties
overwrite
-
-
XProperties
public XProperties(Properties[] sources, boolean[] overwrite)
This will instantiate the properties taking the value ssupplied. Each given source Properties object as an associated flag indicating overwrite permission. Therefore, order is important for loading these.- Parameters:
sources
-overwrite
-
-
-
Method Detail
-
getListSeparator
public String getListSeparator()
Get the list separator. The default is inDEFAULT_LIST_SEPARATOR
.- Returns:
- the list separator
-
setListSeparator
public void setListSeparator(String listSeparator)
Set the list separator- Parameters:
listSeparator
- the list separator
-
getBoolean
public boolean getBoolean(String key)
Retrieves the boolean value. Any ofLOGICAL_TRUES
are equivalent to logical true. Any ofLOGICAL_FALSES
are equivalent to logical false. All comparisons are done case-insensitive- Parameters:
key
-- Returns:
- the boolean value of the property.
-
setBoolean
public void setBoolean(String key, boolean value)
Sets the given boolean value.- Parameters:
key
-value
-
-
getInt
public int getInt(String key)
Get the value associated with this key as an integer.- Parameters:
key
-- Returns:
- Throws:
Exception
- if the value cannot be parsed as an integer
-
setInt
public void setInt(String key, int value)
Set the property from the integer value.- Parameters:
key
-value
-
-
getLong
public long getLong(String key)
Get the property as a long.- Parameters:
key
-- Returns:
- the long value
- Throws:
Exception
- if the value cannot be interpreted as a long.
-
setLong
public void setLong(String key, long value)
Set the value of the property from the give long.- Parameters:
key
-value
-
-
getFile
public File getFile(String key)
Retrieve the file stored by this key, resolving it against the given invocation directory.- Parameters:
key
-- Returns:
- the
File
object.
-
getFile
public File getFile(String key, File parent)
Retrieve the file stored by this key, resolving it against the given file.- Parameters:
key
-parent
-- Returns:
-
setFile
public void setFile(String key, File f)
Puts the file name into the properties, (not) the contents! This will not be portable between platforms, since this is the full path, properly escaped.- Parameters:
key
-f
-
-
getString
public String getString(String key)
Gets the value corresponding to the key as a string. This always works.- Parameters:
key
-- Returns:
-
setString
public void setString(String key, String value)
Sets the value of the key with the given string.- Parameters:
key
-value
-
-
getSerializable
public Object getSerializable(String key)
Gets a single serializable java object.- Parameters:
key
-- Returns:
-
setSerializable
public void setSerializable(String key, Serializable object)
Retrieves a single serializable java object.Don't forget that you need to have any classes for this available to the current virtual machine to correctly deserialize the result.- Parameters:
object
- the serializable object
-
setSerializableList
public void setSerializableList(String key, Object[] oList)
Stores a list of serializable java objects in the properties.- Parameters:
key
-oList
-
-
getSerializableList
public Object[] getSerializableList(String key)
Retrieves a list of serializable objects from the property. Don't for get that you need to have the classes for these available to the current virtual machine to correctly deserialize these.- Parameters:
key
-- Returns:
-
getBytes
public byte[] getBytes(String key)
Returns the value associated with this key as a byte array.- Parameters:
key
-- Returns:
- the byte array
-
setBytes
public void setBytes(String key, byte[] ba)
Sets the value for the given key to the byte array.- Parameters:
key
-ba
-
-
setDouble
public void setDouble(String key, double value)
Set the value associated with this key to the given double value.- Parameters:
key
-value
-
-
getDouble
public double getDouble(String key)
Attempts to retrieve the value associated with the key as a double.- Parameters:
key
-- Returns:
-
setProperty
public Object setProperty(String key, String value)
A bug fix for Properties... Attempting to set a non-existent property with a null value throws aNullPointerException
. This is intercepted here.- Overrides:
setProperty
in classProperties
-
add
public void add(Map source, boolean overwrite)
-
add
public void add(Properties[] sources, boolean overwrite)
Adds each of the given array of properties. The flag deteremines if pre-exisiting key-value pairs should be replaced. This uses the same flag for every property.- Parameters:
sources
-overwrite
-
-
add
public void add(Properties[] sources, boolean[] overwrite)
Adds each property set with a flag for the set indicating overwrite permission.- Parameters:
sources
-overwrite
-- Throws:
Exception
- thrown if the arguments are not the same length.
-
setDate
public void setDate(String key, Date d)
Set the value of with the given Date. Note: internally this is actually stored as a long.- Parameters:
key
-d
-- Throws:
Exception
-
getURI
public URI getURI(String key)
Get the value as a uri- Parameters:
key
-- Returns:
- the URI
- Throws:
Exception
- if the value cannot be parsed as a URI.
-
setURI
public void setURI(String key, URI uri)
Sets the value for the key from the URI- Parameters:
key
-uri
-- Throws:
Exception
-
setURI
public void setURI(String key, String uriString)
A convenience. This takes a string and checks that it is a valid URI before setting the property.- Parameters:
key
-uriString
- the uri- Throws:
Exception
- if the supplied string cannot be parsed as a URI.
-
setURL
public void setURL(String key, URL url)
Set the value from the given URL- Parameters:
key
-url
- the new value- Throws:
Exception
-
setURL
public void setURL(String key, String urlString)
A convenience. This takes a string and checks that it is a valid URL before setting the property.- Parameters:
key
-urlString
-- Throws:
Exception
-
getDate
public Date getDate(String key)
Get the value as a date.- Parameters:
key
-- Returns:
- the date
- Throws:
Exception
- if the value is not parseable as a long.
-
getList
public String[] getList(String key, String separator)
This will take a c list separated by a string and return a vector of strings.- Parameters:
key
-separator
- a string that separates each element of the list.- Throws:
Exception
-
getArrayList
public ArrayList<String> getArrayList(String key, String separator)
return list as anArrayList
.- Parameters:
key
-separator
-- Returns:
-
getList
public String[] getList(String key)
Gets the value associated with the key and splits it into an array of strings using the value fromgetListSeparator
- Parameters:
key
-- Returns:
- the array of strings
- Throws:
Exception
-
setStoreFileName
public void setStoreFileName(String fileName)
Sets the name of the file that calling the defaultstore()
method will write to. It may be changed at any time.- Parameters:
fileName
- the name of the file
-
getStoreFileName
public String getStoreFileName()
Get the name of the store file- Returns:
- the name of the file.
-
load
public void load()
Loads the content of the file ingetStoreFileName
- Throws:
Exception
- if the file cannot be read
-
load
public void load(String storeFileName)
Load the content of the given file into the current properties object, over-writing any duplicate values.- Parameters:
storeFileName
-
-
load
public void load(File f)
Loads from a file rather than from a stream. The parent class really needs one of these... Note that this does not set the default property file name for this object! This allows you to load multiple property files into the current properties object. Be advised that no vetting of the result occurs -- a property's value will be the last one loaded.- Parameters:
f
- the file
-
setList
public void setList(String key, String list)
This sets a list directly, so the list itself is not touched. This is equivalent to callingsetString(key,list)
and is simply provided as a convenience.- Parameters:
key
-list
-
-
setList
public void setList(String key, String[] list)
Set the value of a list using the default separator.- Parameters:
key
-list
- the array of strings to store as a list
-
isList
public boolean isList(String key)
-
isList
public boolean isList(String key, String separator)
check if a string is in list format. This is not a clever test! It is unaware of any structure for the list.- Parameters:
key
-separator
-- Returns:
-
store
public void store(String header)
Writes the properties to the store file. The header is written at the start of the file. It may be empty.- Parameters:
header
- the header (i.e. intial comment) for the file- Throws:
IOException
- if no store file has been set.
-
store
public void store()
This just invokesstore
with an empty header.
-
setList
public void setList(String key, String[] list, String separator)
Sets the value from an array of strings, delimiting them with the supplied separator.- Parameters:
key
-list
- the array of stringsseparator
-
-
isEmpty
public boolean isEmpty()
This will returntrue
if the current properties has no entries andfalse
otherwise.
-
clone
public Object clone()
Creates a deep copy of this object.- Overrides:
clone
in classProperties
- Returns:
- the XProperties object
-
getClone
public XProperties getClone()
Gets a clone of this object. Note that each call causes a new clone to be produced.- Returns:
- the XProperties object as an XProperties object, so unlike the
clone
method no casting is required.
-
equals
public boolean equals(Object obj)
Checks if this XProperties object is the same as a given object. Note that if these are large this might be expensive.
-
keySubset
protected boolean keySubset(XProperties b)
Checks that the keys of b are a subset of the keys in thisXProperties
.- Parameters:
b
- theXProperties
to check against.
-
main
public static void main(String[] args)
This exists simply for testing this object. You supply the name of a file that will be written so that this can check that it does read and write files correctly. It will also put various properties in and retrieve them. The file is left should you want to look at it.- Parameters:
args
-
-
toString
public String toString(int indent)
-
-