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

Lookups

The serialization example (in the Java Objects and the Directory trail) showed that an object stored (serialized) in a directory can be read back by using Context.lookup()(in the API reference documentation).

// Check that the object is bound
Button b2 = (Button)ctx.lookup("cn=Button");
System.out.println(b2);

Similarly, in the reference example (in the Java Objects and the Directory trail), attributes example (in the Java Objects and the Directory trail), remote object examples (in the Java Objects and the Directory trail), the CORBA object example (in the Java Objects and the Directory trail), and the custom object example (in the Java Objects and the Directory trail), you could simply use 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(in the API reference documentation). 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 (in the Beyond the Basics trail) 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 (in the Beyond the Basics trail) trail for details).

Object factories are described in more detail in the Object Factories (in the Java Objects and the Directory trail) lesson.

Type of Object

The 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 (in the Java Objects and the Directory trail) and reference (in the Java Objects and the Directory trail) examples.

// Read from the directory 
Hello h = (Hello)ctx.lookup(name);

// Execute the remote method
System.out.println(h.sayHello());
To successfully run this example, the RMI requires that you specify a security manager and a security policy.
# java -Djava.security.manager -Djava.security.policy=.policy \
    LookupRemote cn=RemoteHello
After performing the lookup(), you can cast the result to a Hello class and invoke a method on it.

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 (in the Java Objects and the Directory trail).

The following code looks up a CORBA object bound by using the CORBA object example (in the Java Objects and the Directory trail).

// 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());
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.

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 (in the Java Objects and the Directory trail).


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