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 home interface 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 the
Lookup
example:
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. ...
Get Java Enterprise in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.