home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book Home Java Enterprise in a Nutshell Search this book

6.4. Looking Up Objects in a Context

Retrieving an object by name from a naming system or directory is called looking up the object. This is the job of the lookup() method of Context. Performing a lookup is analogous to getting the number of a friend from a telephone book by looking up his name. You can use JNDI to look up and retrieve an EJB component from an application server or a remote object from a remote RMI registry.

When you call lookup(), you specify the name of the child of the Context you want to find. lookup() returns a java.lang.Object that represents the child. Here's how we did it in Lookup:

Object obj = initialContext.lookup(name); 

Calling lookup() retrieves an object from the underlying naming system. The JNDI service provider determines the Java representation of these objects, and we have no way of affecting the provider's decision. Depending on the naming system and the design of the provider, the object you retrieve may or may not implement Context. For example, if you use the Sun filesystem provider, and your current context is a directory, looking up a child that is a file returns an instance of java.io.File. Looking up a directory, however, returns an instance of FSContext or RefFSContext, both of which implement Context. As another example, say you use Novell's NDS provider, and the current context is an NDS tree. If you look up an organization, you get back an OrganizationDirContext that implements both Context and Novell's NdsObject interface. The bottom line is that the class you get back from lookup() depends on how the service provider is implemented.

JNDI leaves it up to the service provider to choose whether objects should implement Context. There are no strict rules about when an object should implement it, but there are some general guidelines. An object with children is a container, and the guideline for containers is that they should implement Context. This is because we generally perform naming operations upon these objects. For example, in the filesystem, directories can contain other objects, so the object that represents a directory should implement Context (which is how Sun's filesystem provider behaves). If directories don't support Context methods, we cannot use JNDI to look up any children of the directory.

Objects without children are leaves, and leaves may or may not implement Context, depending on how they are used. For example, because files have no children to look up, the methods we perform on them lie outside the naming system. The author of the Sun filesystem provider made a design choice that once we have looked up a file, we're done with JNDI. So, we can read the input stream on a file or write an output stream to it, but we cannot use JNDI to perform naming operations on the file.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.