These property classes provide a convenient method to easily provide an agent with configuration data. For example, the following would be usable in any agent running in an environment supporting ExpandedProperties:
// Add and initialize the following in your agent. protected myProperties = new ExpandedProperties(); // Called by JADE with any arguments given to the agent from the command line. public void setArguments(String[] args) { myProperties.parseArgs(args); // myPropeties may now be used throughout your agent code. // See the class BasicProperties for all its easy to use getters. }Properties are of the form: key=value or key:value. The special property import:foo.properties causes the property file foo.properties to be read. The ".properties" suffix is simply a common convention and is not required or enforced by these classes. One property file may import from others. Circular imports are checked for and if detected will throw a PropertiesException (extension of RuntimeException). Here is an example of a properties file:
# Specifies one or more user IDs to notify. # Separate each with a space. jabber.notify=smith # The Jabber server. jabber.server=theserver.hpl.hp.com SimpleCalendar.input=${AGENT_HOME}/config/simplecalendar.properties weather.state=CA weather.city=MOFFETT ARPT weather.zonecode=CAZ006Property files may contain as many pairs as necessary. Blank lines as well as lines beginning with '#' or '!' are ignored. A line ending with '\' is interpreted as continued to the next line. Within a collection of continued lines, any beginning with '#' or '!' are ignored making it easy to comment out particular ones.
To fetch the value associated with a key, use the format ${xxx}. An all uppercase key, such as ${AGENT_HOME} in the above example will cause the environment space (full not just those in the JVM's System properties) to be checked if the key doesn't exist in the properties collection.
A property may be set such that it can't be altered by ending the key value with a '!'. For example:
agentClass!=com.hp.agent.FooOne still references this property as ${agentClass}. This is particularly handy when you want to make a property collection available but prevent the changing of particular values. We have used this in conjunction with loading a property collection from the same jar file as a particul class. This is demonstrated below:
String defaultPropName = "com/hp/agent/smartagent/setup.properties"; properties = new ExpandedProperties(); InputStream propertyStream = this.getClass().getClassLoader().getResourceAsStream(defaultPropName); if (propertyStream != null) { try { properties.load(propertyStream); } catch (IOException ioe) { throw new PropertiesException("Error reading:" + defaultPropName); } }
Consult each classes documentation for further information but they relate as follows:
return new InputStreamReader(new FileInputStream(aFileName));whereas in the CLDC environment it would be:
return new InputStreamReader(Connector.openInputStream(aFileName));