![]() ![]() ![]() ![]() |
Reading Objects from the Directory |
The Listlesson showed you how to list a context using the Context.list()
and Context.listBindings()
methods.
List
When you use Context.list(), you get back an enumeration of NameClassPair. You can invoke getClassName()
on each NameClassPair to obtain the class name of the object in that binding.
For example, if we list the context that we used to bind the various objects in the Storing Objects
lesson, we get the following output:
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.# java List ou=Groups: javax.naming.directory.DirContext ou=People: javax.naming.directory.DirContext cn=Button: java.awt.Button cn=favorite: Fruit cn=favDrink: javax.naming.directory.DirContextList Bindings
When you use Context.listBindings(), you get back an enumeration of Binding. You can invoke getObject()
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()
.
For example, if we list the bindings in the context that we used to bind the various objects in the Storing Objects
lesson, we get the following output:
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.# java 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=favorite: Fruit: orange cn=favDrink: Drink: water
![]() ![]() ![]() ![]() |
Reading Objects from the Directory |