|Reading Objects from the Directory|
The serialization example showed that an object stored (serialized) in a directory can be read back by using Context.lookup().
// Check that the object is bound Button b2 = (Button)ctx.lookup("cn=Button"); System.out.println(b2);
Similarly, in the reference example , attributes example , remote object examples , the CORBA object example , and the custom object example , you could simply use lookup() to retrieve the stored object.
Object FactoriesIn the attributes example, the environment used to create the initial context had an additional property, Context.OBJECT_FACTORIES. This property specifies the class names of one or more object factories to use when turning information stored in the directory into Java objects expected by the application.
When the object is represented as a reference in the directory, the reference contains the class name and, optionally, the location of the object factory. Consequently, the reference example did not need to set the Context.OBJECT_FACTORIES property. Similarly, when an object is serialized, it typically needs only to be deserialized and not transformed any further. This was the case with the previous java.awt.Button example, so again, no object factory was specified.
In the attributes example, what is stored to represent the Drink object is simply a collection of attributes, so you need to specify an object factory, DrinkFactory, to use to convert those attributes to a Drink object.
Although no factories were specified explicitly in the remote and CORBA object examples, object factories were preconfigured into the LDAP service provider that was used. (See the Beyond the Basics trail for details about environment properties and how they are used to configure service providers.) The custom object example also used an object factory. There, the factory was specified by using an application resource file (see the Beyond the Basics trail for details).
Object factories are described in more detail in the Object Factories lesson.
Type of ObjectThe type of object returned by lookup() is determined by the object factory and/or the service provider. In the remote object examples, the object looked up is a java.rmi.Remote object. In the RMI/IIOP object and CORBA object examples, the object looked up is a CORBA object. Following are some examples of how an object is used after it has been looked up from the directory.
The following code looks up a remote object bound by using the directly bound and reference examples.To successfully run this example, the RMI requires that you specify a security manager and a security policy.// Read from the directory Hello h = (Hello)ctx.lookup(name); // Execute the remote method System.out.println(h.sayHello());After performing the lookup(), you can cast the result to a Hello class and invoke a method on it.# java -Djava.security.manager -Djava.security.policy=.policy \ LookupRemote cn=RemoteHello
Note: The stub and server files must have been placed in the location specified by the server programs (i.e., that specified by the "java.rmi.server.codebase" property), as directed by the binding examples .
The following code looks up a CORBA object bound by using the CORBA object example .After performing the lookup(), you must use the appropriate narrow() method to narrow the object to the right type and then invoke the appropriate method on the object.// Look up the object org.omg.CORBA.Object cobj = (org.omg.CORBA.Object)ctx.lookup("cn=CorbaHello"); // Narrow the object to the right type HelloApp.hello h2 = HelloApp.helloHelper.narrow(cobj); // Invoke the method on the object System.out.println(h2.sayHello());
Note: You must copy to the classpath, or otherwise make available in the classpath, the class files generated by idltojava (i.e., the HelloApp directory and its contents) in the binding example .
|Reading Objects from the Directory|