Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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 4 of 4
The example below illustrates how to connect to a naming service, list all of the bindings, or list a specific binding. It
uses the filesystem service provider, which is one of the reference JNDI service-provider implementations provided by Sun.
The filesystem service provider makes the filesystem look like a naming service (which it is, in many ways -- filenames like
/foo/bar/baz are names and are bound to objects like files and directories). I selected it because everyone has access to a filesystem
(as opposed to, say, an LDAP server).
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Binding;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.util.Hashtable;
public
class Main {
public
static
void
main(String [] rgstring) {
try {
// Create the initial context. The environment
// information specifies the JNDI provider to use
// and the initial URL to use (in our case, a
// directory in URL form -- file:///...).
Hashtable hashtableEnvironment = new Hashtable();
hashtableEnvironment.put(
Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory"
);
hashtableEnvironment.put(
Context.PROVIDER_URL,
rgstring[0]
);
Context context = new InitialContext(hashtableEnvironment);
// If you provide no other command line arguments,
// list all of the names in the specified context and
// the objects they are bound to.
if (rgstring.length == 1) {
NamingEnumeration namingenumeration = context.listBindings("");
while (namingenumeration.hasMore()) {
Binding binding = (Binding)namingenumeration.next();
System.out.println(
binding.getName() + " " +
binding.getObject()
);
}
}
// Otherwise, list the names and bindings for the
// specified arguments.
else {
for (int i = 1; i < rgstring.length; i++) {
Object object = context.lookup(rgstring[i]);
System.out.println(
rgstring[i] + " " +
object
);
}
}
context.close();
}
catch (NamingException namingexception) {
namingexception.printStackTrace();
}
}
}
The program in the listing above first creates an initial context from the specified JNDI provider (in this case, Sun's filesystem provider) and a URL specifying a local directory. If no additional command-line arguments are specified, the program lists the objects and names of every entity in the specified directory. Otherwise, it lists the objects and names of only those items specified on the command line.
You should now have both an understanding of and an appreciation for naming services in general and JNDI in particular. In distributed environments, they are valuable tools for locating information and resources. JNDI makes it possible to work with a variety of naming services without having to master a multitude of APIs. Next month, we'll take a look at the other half of JNDI -- its directory functions.