![]() ![]() ![]() ![]() |
Naming Operations |
Instead of getting a single object at a time, as is the case with Context.lookup(), you can also list an entire context using a single operation. There are two methods for listing a context: one that returns the bindings and one that returns only the name-to-object class pairs.
List
The Context.list()method returns an enumeration of NameClassPair
. Each NameClassPair consists of the name of the object, and the class name of the object. The following code fragment lists the contents of the "awt" directory (i.e., the files and directories found in "awt" directory):
Running this example yields the following output:NamingEnumeration list = ctx.list("awt"); while (list.hasMore()) { NameClassPair nc = (NameClassPair)list.next(); System.out.println(nc); }# java List accessibility: javax.naming.Context color: javax.naming.Context datatransfer: javax.naming.Context dnd: javax.naming.Context event: javax.naming.Context font: javax.naming.Context geom: javax.naming.Context im: javax.naming.Context image: javax.naming.Context peer: javax.naming.Context print: javax.naming.Context swing: javax.naming.ContextList Bindings
The Context.listBindings()method returns an enumeration of Binding
. Binding is a subclass of NameClassPair. In addition to the object's name and class name, a binding also contains the object. The following code enumerates the "awt" context, printing out each binding's name and object:
NamingEnumeration bindings = ctx.listBindings("awt"); while (bindings.hasMore()) { Binding bd = (Binding)bindings.next(); System.out.println(bd.getName() + ": " + bd.getObject()); }Running this example yields the following output:
# java ListBindings accessibility: com.sun.jndi.fscontext.RefFSContext@1dacd52e color: com.sun.jndi.fscontext.RefFSContext@1dacd551 datatransfer: com.sun.jndi.fscontext.RefFSContext@1dacd584 dnd: com.sun.jndi.fscontext.RefFSContext@1dacd5b6 event: com.sun.jndi.fscontext.RefFSContext@1dacd5e8 font: com.sun.jndi.fscontext.RefFSContext@1dacd61b geom: com.sun.jndi.fscontext.RefFSContext@1dacd64d im: com.sun.jndi.fscontext.RefFSContext@1dacd62a image: com.sun.jndi.fscontext.RefFSContext@1dacd65c peer: com.sun.jndi.fscontext.RefFSContext@1dacd68f print: com.sun.jndi.fscontext.RefFSContext@1dacd6c1 swing: com.sun.jndi.fscontext.RefFSContext@1dacd6f3Terminating a NamingEnumeration
A NamingEnumerationcan be terminated in one of three ways: naturally, explicitly, or unexpectedly:
- When NamingEnumeration.hasMore()
returns false, the enumeration is complete and effectively terminated.
- You can terminate an enumeration explicitly before it has completed by invoking NamingEnumeration.close()
. This provides a hint to the underlying implementation to free up any resources associated with the enumeration.
- If either hasMore() or next()
throws a NamingException
, the enumeration is effectively terminated.
Regardless of how an enumeration has been terminated, once terminated, the enumeration cannot be used any further. Invoking a method on a terminated enumeration yields an undefined result.
Why Two Different List Methods?
The list() method is intended for browser-style applications that just want to display the names of objects in a context. For example, a browser might list the names in a context, and wait for the user to select one or a few of the names displayed to perform further operations. Such applications typically do not need access to all the objects in a context.The listBindings() method is intended for applications that need to perform operations on the objects in a context en masse. For example, a backup application might need to perform "file stats" operations on all the objects in a file directory. A printer administration program might want to restart all the printers in a building. To perform such operations, these applications need to obtain all the objects bound in a context and, thus, it is more expedient to have the objects be returned as part of the enumeration.
The application, depending on the type of information it needs, can use either list() or the potentially more expensive listBindings() method.
![]() ![]() ![]() ![]() |
Naming Operations |