![]() ![]() ![]() ![]() |
Reading Objects from the Directory |
In the serialization example, you saw that an object that was stored (serialized) into the directory could be read back using Context.lookup()
:
// Check that it 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 were able to simply use Context.lookup() to retrieve the stored object.
Object Factories
In 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, typically it needs only to be deserialized and not transformed any further. This was the case with the java.awt.Button example above, so again, no object factory was specified.
For the attributes example, since what is stored to represent the Drink object is just a collection of attributes, you need to specify an object factory, DrinkFactory, to use when converting 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 being 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. In that example, the factory was specified using a resource file (see the Beyond the Basics
trail for details).
Object factories are described in more detail in the Object Factories
lesson.
Type of Object
The type of object returned by lookup() is determined by the object factory and/or 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. Here 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 using the directly-bound
and reference
examples.
To run this example, you must specify a security manager and a security policy for the RMI to work.// Read from directory Hello h = (Hello)ctx.lookup(name); // Execute 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 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 org.omg.CORBA.Object cobj = (org.omg.CORBA.Object)ctx.lookup("cn=CorbaHello"); // Narrow to right type HelloApp.hello h2 = HelloApp.helloHelper.narrow(cobj); // Invoke method on object System.out.println(h2.sayHello());
Note: You must copy 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 |