|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
<key>=<value>
Now, remove your administrator's hat and don your user's hat. It's time to retrieve some of the objects we've published. As a user, the only facts we have to know to retrieve an object are its name and the URL that describes the location of the library. We don't have to know how the library is implemented. This is especially useful if we, as users, must write programs to use the library. As you'll see a bit later, the code required to use a library is small indeed.
I've supplied a small client program that retrieves objects from the library. Objects are retrieved as follows:
java Client <properties URL> [name]...
The Client command requires the URL of the properties file. It also accepts any number of additional arguments specifying the names of objects to retrieve from the library.
Now, let's get to the core of this article -- the code. Figure 2, below, depicts the six classes that form the core of Doc-u-Matic. I'll visit each, briefly describe its role, and then present the important bits of the code (see Resources to download each class's complete source code).

Figure 2. Doc-u-Matic's six core classes
The Library interface defines the methods that all libraries must provide. For the sake of simplicity, it requires only two methods:
one to publish objects and one to retrieve objects. Full-featured libraries would provide search functionality as well.
/** * Publishes an object in the library. * * @param object the object * @param stringName the name of the object * @param map a map containing the object's attributes * */ public void publish(Object object, String stringName, Map map); /** * Retrieves a copy of a published object from the library * and restores it if necessary. * * @param stringName the name of the object * * @returns the object, or null * */ public Object retrieve(String stringName);
The JNDILibrary class provides a JNDI-based implementation of the Library interface. The publish() and retrieve() methods demonstrate how to use JNDI to bind objects to and look up objects from a directory service.
/**
* Publishes an object in the library.
*
* @param object the object
* @param stringName the name of the object
* @param map a map containing the object's attributes
*
*/
public
void
publish(Object object, String stringName, Map map) {
try {
// Use the properties to establish an initial directory context.
DirContext dircontext = new InitialDirContext(m_properties);
// Create an iterator that contains all the entries in the map.
Iterator iterator = map.entrySet().iterator();
// For each entry, create an attribute.
BasicAttributes basicattributes = new BasicAttributes();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();
basicattributes.put(entry.getKey().toString(),
entry.getValue().toString());
}
// Bind the object to the specified name.
dircontext.rebind(stringName, object, basicattributes);
// Close the context.
dircontext.close();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Retrieves a copy of a published object from the library
* and restores it if necessary.
*
* @param stringName the name of the object
*
* @returns the object, or null
*
*/
public
Object
retrieve(String stringName) {
Object object = null;
try {
// Use the properties to establish an initial directory context.
DirContext dircontext = new InitialDirContext(m_properties);
// Look up the object using the specified name.
object = dircontext.lookup(stringName);
// Close the context.
dircontext.close();
}
catch (Exception exception) {
exception.printStackTrace();
}
return object;
}
The getReference() method demonstrates how to create a Reference instance that represents the current instance of a class. Instead of being stored as serialized data, classes that implement
the Referenceable interface are stored indirectly, via Reference instances. Notice how, in the example below, the properties are transformed and stored as a series of bytes. Typically, enough
of an object's state must be stored to create a working copy of the object when the object is looked up in a directory service.