Sun Microsystems V2.0 manual Persistent properties

Models: V2.0

1 54
Download 54 pages 11.11 Kb
Page 26
Image 26

Each physical device is controlled by a single instance of the respective class, accessed through the interfaces listed above. The single instance of the Sun SPOT class creates and manages access to the device driver singletons. The singletons are created only as required.

For example, to get access to the on-board green LED, do:

Iled theLed = Spot.getInstance().getGreenLed();

To turn the LED on and off:

theLed.setOn();

theLed.setOff();

Persistent properties

The sections Manifest and resources and Library manifest properties explain how to define application-specific and library-specific properties. Sometimes it is useful to define persistent properties for an individual SPOT device, and that is the topic of this section. Such properties are always available on the given SPOT regardless of which application is being run.

An 8k byte area of flash is reserved for persistent System properties that can be read and written from SPOT applications and by using ant commands on the host. All properties have a String key and a String value.

We distinguish between user-definedproperties, set either using ant set-system-propertyor from within a SPOT application, from other System properties, such as those defined by Squawk.

Accessing properties from SPOT applications

To obtain the value of a System property do:

System.getProperty(<propName>);

This call returns null if the property has not been defined. All system properties, including user- defined properties, can be accessed this way.

To set the value of a user-defined property do:

Spot.getInstance().setPersistentProperty(<propName>, <propValue>);

A property can be erased by setting its value to null. Setting or erasing a persistent property takes about 250ms. If you wish to set several properties at once, you can optimise performance like this:

Properties propsToWrite = new Properties(); propsToWrite.setProperty("key1", "value1");

propsToWrite.setProperty("key99", "value99"); Spot.getInstance().setPersistentProperties(propsToWrite);

You can also get the value of a user-defined system property by using:

Spot.getInstance().getPersistentProperty(<propName>);

This call is much slower to execute than System.getProperty, because it re-reads the flash memory. Therefore you should normally use System.getProperty to access user-defined properties. However, user-defined properties are loaded from the flash when an isolate starts which means that you will need to use the slower method of access in two situations:

26

Page 26
Image 26
Sun Microsystems V2.0 manual Persistent properties