JNDI Name Lookup

The most common use of JNDI is to look up objects that have been bound to a name. To do this, you require two items of information:

  • The JNDI name

  • The class of the bound object

With this information in hand, object lookup is the simple matter of using the Context.lookup() method to find the object and then to cast that object to the required class.

Listing 3.2 shows a simple program to look up the name sams/book that was bound by the program in Listing 3.1.

Listing 3.2. Full Text of JNDILookup.java
 1: import javax.naming.*;
 2: public class JNDILookup
 3: {
 4:     private final static String JNDI = "sams/book";
 5:     public static void main(String[] args) {
 6:         try {
 7:             Context ic = new InitialContext();
 8:             String name = (String)ic.lookup(JNDI);
 9:             System.out.println(JNDI+"="+name);
10:         }
11:         catch (NamingException ex) {
12:              System.err.println(ex);
13:              System.exit(1);
14:         }
15:         catch (ClassCastException ex) {
16:              System.err.println(ex);
17:              System.exit(1);
18:         }
19:     }
20: }
					

You can run the JNDIBind program in Listing 3.1 and then run this JNDILookup program to print out the value of the string bound against sams/book.

Note

When casting an object that the lookup() method returns, that object's class must be in the client program's class path. If this is not the case, the program throws an exception.


Changing Contexts

The example name sams/book used in Listings 3.1 and 3.2 is an example of a Composite Name. If you need to look up many names in the same context of a composite name (names of the form sams/...), it is better to change to sub-context and look up the simple name within that context.

With this information in hand, the sub-context is a name entry just like any other name, and you look it up in just the same way. The retrieved object is another Context object. Listing 3.3 shows code that retrieves a name from a sub-context.

Listing 3.3. Full Text of JNDILookupSAMS.java
 1: import javax.naming.*;
 2: public class JNDILookupSAMS
 3: {
 4:     public static void main(String[] args) {
 5:         try {
 6:             Context ic = new InitialContext();
 7:             Context ctx = (Context)ic.lookup("sams");
 8:             String name = (String)ctx.lookup("book");
 9:             System.out.println(name);
10:         }
11:         catch (NamingException ex) {
12:              System.err.println(ex);
13:              System.exit(1);
14:         }
15:         catch (ClassCastException ex) {
16:              System.err.println(ex);
17:              System.exit(1);
18:         }
19:     }
20: }
						

Narrowing RMI-IIOP Objects

There is only one additional twist to the lookup tale, and that is when dealing with RMI over IIOP objects.

The implementation of J2EE requires the use of RMI-IIOP to implement the remote interfaces to EJB components. Consequently, when a lookup is for an EJB name (more on this on Day 4, “Introduction to EJBs”), you cannot cast the returned object to the required class; instead, you must narrow it.

RMI-IIOP uses a portable remote object to encapsulate information about the real remote object. A portable remote object contains information about the real bound object in a portable format that can be interogated by the recipient to find the real remote object. The process of obtaining the real object from the portable remote object is called narrowing.

You use the PortableRemoteObject.narrow() method in the javax.rmi package to narrow a protable remote object to obtain the actual remote object. The narrow() method takes two parameters:

  • The object to narrow

  • A java.lang.Class object defining the real remote object's class

Listing 3.4 previews the discussion on Day 4 about EJB objects, but also serves to illustrate the use of the narrow() method.

Listing 3.4. Narrowing an EJB Home Object
1: InitialContext ic = new InitialContext();
2: Object lookup = ic.lookup("java:comp/env/ejb/Agency");
3: AgencyHome home = (AgencyHome) PortableRemoteObject.narrow(lookup, AgencyHome.class);
						

If your primary purpose for understanding JNDI is to enable the lookup and use of EJBs and other J2EE technologies (such as JDBC data sources and Message queues), you can skip the rest of this day's material and return to it at a later date.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.79.20