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

Lookups

In the serialization example (in the Java Objects and the Directory trail), you saw that an object that was stored (serialized) into the directory could be read back using Context.lookup()(in the API reference documentation):

// Check that it 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 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(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, 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 (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. In that example, the factory was specified using a 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 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 (in the Java Objects and the Directory trail) and reference (in the Java Objects and the Directory trail) examples.

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

// Execute remote method
System.out.println(h.sayHello());
To run this example, you must specify a security manager and a security policy for the RMI to work.
#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 using the CORBA object example (in the Java Objects and the Directory trail).

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