|
|
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 6 of 6
/**
* Publishes an object.
*
*/
public
static
void
main(String [] rgstring) {
if (rgstring.length < 1) {
System.out.println("Usage: java Publish *lt;URL> [name class]...");
System.exit(-1);
}
try {
Properties properties = new Properties();
// Load the properties from the specified URL.
properties.load(new URL(rgstring[0]).openStream());
// Use the properties to establish an initial directory context.
DirContext dircontext = new InitialDirContext(properties);
// Get the name of the library.
String stringName = properties.getProperty("library.name");
// Look up a library using the specified name.
Library library = (Library)dircontext.lookup(stringName);
// Close the context.
dircontext.close();
// For each command-line argument, instantiate the specified
// object, and publish it to the library.
for (int i = 1; i < rgstring.length; i++) {
Object object = Beans.instantiate(null, rgstring[i + 1]);
library.publish(object, rgstring[i], new HashMap());
i++;
}
}
catch (Exception exception) {
exception.printStackTrace();
System.exit(-1);
}
}
The Client class is almost identical to the Publish class, except that it retrieves objects rather than publishes them.
/**
* A simple client.
*
* Demonstrates how to connect to and retrieve from a
* Library instance via JNDI.
*
*/
public
static
void
main(String [] rgstring) {
if (rgstring.length < 1) {
System.out.println("Usage: java Client <URL> [name]...");
System.exit(-1);
}
try {
Properties properties = new Properties();
// Load the properties from the specified URL.
properties.load(new URL(rgstring[0]).openStream());
// Use the properties to establish an initial directory context.
DirContext dircontext = new InitialDirContext(properties);
// Get the name of the library.
String stringName = properties.getProperty("library.name");
// Look up a library using the specified name.
Library library = (Library)dircontext.lookup(stringName);
// Close the context.
dircontext.close();
// For each command-line argument, retrieve the specified object
// from the library.
for (int i = 1; i < rgstring.length; i++) {
Object object = library.retrieve(rgstring[i]);
// If the object is restorable, restore it.
if (object instanceof Restorable) {
Restorable restorable = (Restorable)object;
restorable.restore();
}
}
}
catch (Exception exception) {
exception.printStackTrace();
System.exit(-1);
}
}
And that's all there is to it!
If you've stayed with me through the four columns in the JNDI series, you're probably slightly out of breath -- I covered a lot of ground in four short columns. However, you should also have a much clearer understanding of naming and directory services, JNDI, its capabilities, and the flexibility it brings to JNDI-enabled enterprise applications. JNDI already plays an important role in several key Java APIs -- you can expect its role to expand.