Class WTProperties
- All Implemented Interfaces:
Serializable,Cloneable,Map<Object,Object>
This is a convenience class providing static access to the following property sources:
- System Properties
- Properties supplied by the Java runtime system.
- Configuration File
- Properties loaded from a resource file named wt.properties. This is loaded just like a class file, and therefore it represents the properties associated with the software installation from which classes are being loaded. The file is loaded as a class resource of the WTProperties class. If the WTProperties class is loaded from the local class path, then this file will be loaded from the local class path. If the WTProperties class is loaded remotely, then this file may be loaded from either the local class path or remotely.
- Application Arguments
- Command line arguments to a Java application. The argument strings are parsed and any of the form name=value are made available as properties.
- Server's Configuration File
- Properties loaded from the server's properites file. These properties are loaded via a URL connection to wt.properties relative to the server's codebase URL. For applets, the codebase is the applet's codebase unless an applet parameter named wt.server.codebase indicates otherwise. For applications, it is the wt.server.codebase property as defined by any of the previous property sources.
- Applet Parameters
- Parameters of the current applet.
Access is provided at three levels with each level inheriting the lower levels as default values.
In all cases, the properties objects returned support a form of macro substitution in the property values so that values can be constructed from other property values. Values are parsed for expressions of the form
getLocalProperties ()- Properties defined by the application arguments, configuration file, and system properties. These properties are global to the Java Virtual Machine and the Java class files that are loaded.This level should be used for properties accessed by static class initializers.
getServerProperties- Properties defined in the server's configuration file. This interface is intended to allow a client to view the properties being used by the server, however, this object will only reflect the server's configuration file, not any additional system properties and command line parameters that may have been used.
getAppletProperties- The highest level where all properties defined in the above sources are visible, including applet parameters. Note, however, that applet parameters will not appear in enumerations of the properties since the list of available parameters is not exposed by the Applet class.
$(name) and the value of the property name
is substituted into the resulting property value. The expression
$DATE(format) is a builtin SimpleDateFormat
macro where format is a pattern expression such as MMddyy. It can be used to construct
date/time based values such as log file names. The expression
$LIB(key) is a builtin macro which lists the contents
of the directory pointed at by key separated by File.pathSeparator.
The methods for accessing properties are static, making it very convenient
for classes to access properties without the benefit of any other context.
However, the server and applet properties are not static relative to
this class. If this class was loaded from the local class path, it
may be shared by multiple applets that may be communicating with multiple
servers. Therefore, a simple convention is used to correctly associate
the calling thread to the appropriate server and applet properties. This
class uses the WTContext class to map the calling thread to
appropriate server and applet properties objects. It is important that applications
and applets call the WTContext.init method at startup. Applets should
also call the destroy method when they are destoyed to remove this mapping and
allow the timely garbage collection of the Applet object.
Example application main:
public static void main (String args)
{
// Initialize WTContext with command line arguments
WTContext.init(args);
// Replace system properties with WTProperties
Properties props = WTProperties.getLocalProperties();
System.setProperties(props);
// Get our wt.foo.bar property
String bar = props.getProperty("wt.foo.bar");
...
}
Example applet init:
public void init ()
{
// Initialize WTContext for this applet
WTContext.init(this);
// Get the wt.foo.bar property from applet parameters, server
// configuration properties, local configuration properties,
// or system properties.
Properties props = WTProperties.getAppletProperties();
String bar = props.getProperty("wt.foo.bar");
...
}
public void destroy ()
{
// Destroy WTContext for this applet
WTContext.getContext().destroy(this);
}
Supported API: true
Extendable: false
- See Also:
-
Field Summary
Fields inherited from class java.util.Properties
defaults -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic WTPropertiesGet a properties object for the current thread that includes applet parameters (if any), server configuration file properties, and local properties.static WTPropertiesGet a properties object that includes application arguments, properties from the resource file wt.properties in the local codebase, and Java system properties.getProperty(String key) Searches for the property with the specified key in this property list.booleangetProperty(String key, boolean default_value) Convenience method to get a boolean property string and convert it to a boolean value.intgetProperty(String key, int default_value) Convenience method to get an integer property string and convert it to an int value.getProperty(String key, String defaultValue) Searches for the property with the specified key in this property list.static URLConvenience method to retrieve server codebase URL for the current thread.static URLgetServerCodebase(Applet applet, Properties props) Get server codebase URL as determined from applet codebase or property value.static WTPropertiesGet a properties object for the current thread that includes properties from the server's wt.properties configuration file in addition to local properties.static WTPropertiesgetServerProperties(URL codebase) Get a properties object for the given server codebase that includes server configuration file properties and local properties.getURL()Gets the URL from which the corresponding local or server properties where loaded.voidlist(String prefix, PrintStream out) Prints this property list out to the specified output stream.static voidSimple tester.voidParse name=value pairs from a string array into this properties object.static voidreset()Reset theWTPropertiesclass to its initial state.static voidsetLocalPropertiesOnly(boolean local_only) Sets to allow only local properties to be loaded.static voidsetVerbose(boolean verbose) Deprecated.substitute(String str) Substitutes expressions of the form$(name)in strings with property values from this properties object.Methods inherited from class java.util.Properties
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, list, list, load, load, loadFromXML, merge, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, size, store, store, storeToXML, storeToXML, storeToXML, toString, values
-
Constructor Details
-
WTProperties
Construct a newWTPropertiesobject. Normally, references to the standard properties objects are returned by static methods of this class. This constructor exists for applications that wish to load their own private properties files and take advantage of the extra utility functions or macro substitution.
Supported API: true
-
-
Method Details
-
getLocalProperties
Get a properties object that includes application arguments, properties from the resource file wt.properties in the local codebase, and Java system properties.
Supported API: true- Returns:
- the local
WTPropertiesobject - Throws:
IOException
-
getServerProperties
Get a properties object for the current thread that includes properties from the server's wt.properties configuration file in addition to local properties.
Supported API: true- Returns:
- a server
WTPropertiesobject - Throws:
IOException
-
getServerProperties
Get a properties object for the given server codebase that includes server configuration file properties and local properties. If thecodebaseargument is null, the properties for the default server are returned.
Supported API: true- Parameters:
codebase- the server codebase to load properties from- Returns:
- a server
WTPropertiesobject - Throws:
IOException
-
getServerCodebase
Convenience method to retrieve server codebase URL for the current thread. If called from an applet context, it will be the applet's codebase unless overridden by an applet parameter namedwt.server.codebase. If called from an application, it will return the URL for the localwt.server.codebaseproperty.
Supported API: true- Returns:
- the codebase URL
- Throws:
IOException
-
getServerCodebase
Get server codebase URL as determined from applet codebase or property value. If the specified applet is non-null, it will be the applet's codebase unless overridden by an applet parameter namedwt.server.codebase. If the codebase is not available from the applet, it will return the URL for thewt.server.codebaseproperty in the given property object. Ifwt.server.codebaseis not specified, butwt.server.codebase.locatoris specified, the given URL will be opened and used to read the server codebase URL as the first line of the response body.
Supported API: true- Parameters:
applet- theAppletobjectprops- thePropertiesobject- Returns:
- the codebase URL
- Throws:
IOException
-
getAppletProperties
Get a properties object for the current thread that includes applet parameters (if any), server configuration file properties, and local properties.
Supported API: true- Returns:
- a applet
WTPropertiesobject - Throws:
IOException
-
getURL
Gets the URL from which the corresponding local or server properties where loaded. If no properties file was found for thisWTPropertiesobject, null is returned.
Supported API: true- Returns:
- the URL used to load this
WTPropertiesobject
-
reset
Reset theWTPropertiesclass to its initial state. This discards any current property objects, causing reloading when they are next accessed. It is used to force reloading of property values from their original sources.If the local properties object was set as the system property object, it is reloaded immediately and set to be the system property object again.
If the local properties object has property change listeners or local values (as a consequence of explicit setProperty calls), immediately reload it, copy the listeners and local values forward, and fire a property change event with a null key and the old/new WTProperties objects as old/new values.
This method may be necessary to build very long lived applets or applications that need to recover from exceptions due to property changes that take place during their lifetimes. For example, a long lived client that needs to recover even if its server host or port number is changed while it is active.
Note that resetting the
WTPropertieswill not affect static class initialization performed in existing classes. Often, classes will use local properties to initialize static final values that cannot be reset later.
Supported API: true- Throws:
IOException
-
setLocalPropertiesOnly
public static void setLocalPropertiesOnly(boolean local_only) Sets to allow only local properties to be loaded. This forces the local properties to be used as the server properties. This can be called during initialization of applications that want to limit themselves to properties found in the local class path.Classes that load and execute in both client and server processes may request the server properties. Server applications can use this method to bypass attempts to load server properties which will ultimately be identical to the local properties already loaded.
Supported API: true- Parameters:
local_only- the setting.
-
parseArgs
Parse name=value pairs from a string array into this properties object. If a string does not contain a name=value string, it is skipped. This method can be used to add command line arguments to the properties object.
Supported API: true- Parameters:
args- string array
-
getProperty
Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returnsnullif the property is not found. Expressions of the form$(name)are substituted with the value of the named property if it is found, otherwise they are replaced by an empty string. If the entire value is a substitution and the named property does not exist, null is returned instead. Substitution is only performed for properties contained in this object. Properties found in the default property list are returned unchanged. The expression$DATE(format)is a builtinSimpleDateFormatmacro where format is a pattern expression such as MMddyy. It can be used to construct date/time based values such as log file names. The expression$LIB(key)is a builtin macro which lists the contents of the directory pointed at by key separated by File.pathSeparator. Once a substitution has been performed, the substituted value is cached and will continue to be returned for consistency.
Supported API: true- Overrides:
getPropertyin classProperties- Parameters:
key- the property key.- Returns:
- the value in this property list with the specified key value.
-
getProperty
Convenience method to get an integer property string and convert it to an int value.
Supported API: true- Parameters:
key- the property key.default_value- value returned if named property does not exist- Returns:
- the value as an int.
-
getProperty
Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns the default value argument if the property is not found. Overridden to support IBM/Harmony JDK implementation of java.util.Properties
Supported API: true- Overrides:
getPropertyin classProperties- Parameters:
key- the hashtable key.defaultValue- a default value.- Returns:
- the value in this property list with the specified key value.
-
getProperty
Convenience method to get a boolean property string and convert it to a boolean value.
Supported API: true- Parameters:
key- the property key.default_value- value returned if named property does not exist- Returns:
- the value as a boolean.
-
substitute
Substitutes expressions of the form$(name)in strings with property values from this properties object. If the entire value is a substitution and the named property is not found, null is returned instead of an empty string. The expression$DATE(format)is a builtinSimpleDateFormatmacro where format is a pattern expression such as MMddyy. It can be used to construct date/time based values such as log file names. The expression$LIB(key)is a builtin macro which lists the contents of the directory pointed at by key separated by File.pathSeparator.This is used internally to the
WTProperties.getPropertymethod. It is exposed as a public method to allow the same substitutions to be applied against arbitrary strings.
Supported API: true- Parameters:
str- the string to be parsed for substitutions- Returns:
- the substituted string
-
setVerbose
public static void setVerbose(boolean verbose) Deprecated.No longer needed. Use log4j instead. Set verbose mode for debugging. When true, this class will display where properties are being loaded from.
Supported API: true- Parameters:
verbose- the verbose setting
-
list
Prints this property list out to the specified output stream. This method is useful for debugging.
Supported API: true- Parameters:
prefix- Optional prefix filter for property namesout- a print stream
-
main
Simple tester. Prints local and server properties lists. If an argument is specified, it is used as a prefix filter on the keys.
Supported API: true- Throws:
IOException
-