![]() ![]() ![]() ![]() |
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 you list the context that you used to bind the various objects in the Storing Objects in the Directory
lesson, you 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=Flower: Flower cn=favorite: Fruit cn=favDrink: javax.naming.directory.DirContext cn=Hello: HelloImplList 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 you list the bindings in the context that you used to bind the various objects in the Storing Objects
lesson, you 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. Notice also that the remote object HelloImpl is returned as a stub to the real object.# 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]]]]
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 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.
![]() ![]() ![]() ![]() |
Reading Objects from the Directory |