Previous | Next | Trail Map | Java Objects and the Directory | Reading Objects from the Directory

Lists

The List (in the Basics trail) lesson showed you how to list a context using the Context.list()(in the API reference documentation) and Context.listBindings()(in the API reference documentation) methods.

List

When you use Context.list(), you get back an enumeration of NameClassPair(in the API reference documentation). You can invoke getClassName()(in the API reference documentation) on each NameClassPair to obtain the class name of the object in that binding.

For example, if you list the context that you used to bind the various objects in the Storing Objects in the Directory (in the Java Objects and the Directory trail) lesson, you get the following output:

# java List
ou=Groups: javax.naming.directory.DirContext
ou=People: javax.naming.directory.DirContext
cn=Button: java.awt.Button
cn=Flower: Flower
cn=favorite: Fruit
cn=favDrink: javax.naming.directory.DirContext
cn=Hello: HelloImpl
For each binding, the Java class name was determined based on information stored in the directory, without necessarily creating an instance of the object in the binding.

List Bindings

When you use Context.listBindings(), you get back an enumeration of Binding(in the API reference documentation). You can invoke getObject()(in the API reference documentation) on each Binding to obtain the object in that binding. The result of getObject() is the same as the result obtained by looking up the object using Context.lookup()(in the API reference documentation).

For example, if you list the bindings in the context that you used to bind the various objects in the Storing Objects (in the Java Objects and the Directory trail) lesson, you get the following output:

# java -Djava.security.manager -Djava.security.policy=.policy ListBindings
ou=Groups: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@1dacd730
ou=People: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@1dacd8ae
cn=Button: java.awt.Button: java.awt.Button[button0,0,0,0x0,invalid,label=Push me]
cn=Flower: Flower: pink rose
cn=favorite: Fruit: orange
cn=favDrink: Drink: water
cn=Hello: HelloImpl_Stub: HelloImpl_Stub[RemoteStub [ref: [endpoint:[129.144.124.135:33858](remote),objID:[1dc60a59:d5e1275092:-8000, 0]]]]
Notice that the "cn=favDrink" entry now has a more precise class name ("Drink"), because instantiating the object (by the corresponding object factory) provided more class information. Notice also that the remote object HelloImpl is returned as a stub to the real object.

Note 1: If you have not installed and make available to your program an RMI Registry service provider (such as the one from Sun), instead of seeing the remote stub, you would see an "rmi" URL instead.

Note 2: The output shown is generated using the Java 2 Platform. The -Djava.security.manager option specifies that the default security manager be used and the -Djava.security.policy=.policy option specifies that the .policy file be used for the security policy. These two options are needed when looking up the java.rmi.Remote object bound in the remote example (in the Java Objects and the Directory trail). In the policy file, you need to grant permission to connect to and accept connections from the Web server and the machine that the RMI registry is running on.

If you are using JDK 1.1, you need to modify the program to install a security manager. See the RMI documentation for details.


Previous | Next | Trail Map | Java Objects and the Directory | Reading Objects from the Directory