Difference between revisions of "Properties"

Line 5: Line 5:
 
The two important methods of this class are:
 
The two important methods of this class are:
 
<pre>
 
<pre>
public void setProperty(Class component, String key, String value) throws IOException
+
public void setProperty(Class componentClass, String key, String value) throws IOException;
 
</pre>
 
</pre>
 
and
 
and
 
<pre>
 
<pre>
public String getProperty(Class component, String key, String defaultValue) throws IOException
+
public String getProperty(Class componentClass, String key, String defaultValue) throws IOException;
 
</pre>
 
</pre>
 +
 +
The first parameter to both methods can be the <code>Class</code> object of any class in your component. This is used to determine where the configuration will be stored. The <code>defaultValue</code> parameter of <code>getProperty</code> will be the returned result if there is no known value for the specified key.
 +
 +
Here is an example use of the properties manager:
 +
 +
<pre>
 +
PropertiesManager.getInstance().setProperty(getClass(), "test", "value");
 +
</pre>
 +
 +
and later...
 +
 +
<pre>
 +
String value = PropertiesManager.getInstance().getProperty(getClass(), "test", "Default Value");
 +
</pre>
 +
 +
Setting the property will automatically cause the value to be saved to disk. The configuration file is an XML file stored in a <code>conf</code> subdirectory of the component's working directory.

Revision as of 17:02, 12 October 2006

Components often want to store and retrieve configuration settings between invocations. An API is provided for this purpose.

Configuration is handled by the setting and getting of properties using the org.geworkbench.engine.properties.PropertyManager class. A singleton instance is available to process requests.

The two important methods of this class are:

public void setProperty(Class componentClass, String key, String value) throws IOException;

and

public String getProperty(Class componentClass, String key, String defaultValue) throws IOException;

The first parameter to both methods can be the Class object of any class in your component. This is used to determine where the configuration will be stored. The defaultValue parameter of getProperty will be the returned result if there is no known value for the specified key.

Here is an example use of the properties manager:

PropertiesManager.getInstance().setProperty(getClass(), "test", "value");

and later...

String value = PropertiesManager.getInstance().getProperty(getClass(), "test", "Default Value");

Setting the property will automatically cause the value to be saved to disk. The configuration file is an XML file stored in a conf subdirectory of the component's working directory.