Book Home Java Enterprise in a Nutshell Search this book

6.2. A JNDI Example

Before we go any further, let's take a look at a simple JNDI example. To access an object in a naming system, we need to create an initial context for the naming system, to give us an entry point into the naming system. Once we have an initial context, we can look up an object by name.

Example 6-1 demonstrates the basic JNDI tasks of getting an initial context to a naming system and looking up an object in that naming system. With slight modification, this code can be used to look up objects with any JNDI provider. So, for example, you could use Lookup to look up Enterprise JavaBeans or remote objects in an RMI registry and handle them however you like. All you have to change is the properties that control the naming system being accessed.

Example 6-1. Looking Up an Object in a Naming System

import java.util.Properties;
import javax.naming.*;

public class Lookup {        
  public static void main(String[] args) {        
    String name = "";
    if (args.length > 0) 
      name = args[0];
        
    try {
      // Create a Properties object and set properties appropriately
      Properties props = new Properties();
      props.put(Context.INITIAL_CONTEXT_FACTORY,
          "com.sun.jndi.fscontext.RefFSContextFactory");
      props.put(Context.PROVIDER_URL, "file:///");
            
      // Create the initial context from the properties we just created
      Context initialContext = new InitialContext(props);
            
      // Look up the object
      Object obj = initialContext.lookup(name);
      if (name.equals(""))
        System.out.println("Looked up the initial context");
      else
        System.out.println(name + " is bound to: " + obj);
    }
    catch (NamingException nnfe) {
      System.out.println("Encountered a naming exception");
    }
  }
}

The first thing the Lookup application does is create a java.util.Properties object and use it to store some String values. The keys used for these values are constants defined in the javax.naming.Context class. Each constant corresponds to an underlying JNDI property name that is meant to communicate specific information about the JNDI service the application is using. Context.INITIAL_CONTEXT_FACTORY specifies the factory class that creates an initial context for the service we want to use. The class com.sun.jndi.fscontext.RefFSContextFactory is a factory class from the filesystem service provider from Sun. Context.PROVIDER_URL tells the factory class the protocol, server name, and path to use in creating an initial context. We specify the URL file:/// to indicate the root of the local filesystem. This works on any Unix or Windows filesystem.

Once we have created the Properties object, we pass it to the javax.naming.InitialContext constructor, which returns the initial context object that is our entry point into this particular naming system. Next, we call the lookup() method on initialContext, specifying the name we want to look up. This call returns an object from the naming system, which, in this case, is a file or directory.

You can run Lookup from the command line and specify an optional name to look up. For example:

% java Lookup boot.ini
boot.ini is bound to: \boot.ini

If the name is instead a directory, the output looks a bit different:

% java Lookup winnt
winnt is bound to: com.sun.jndi.fscontext.RefFSContext@803adec0

Note that if we wanted to make Lookup more general, we might change it so that it reads its property values from a properties file. Then changing the naming system is a simple matter of editing the properties file to specify an appropriate factory object and URL. Depending on the value you use for the factory class, the object you look up could be an Enterprise JavaBeans component, a reference to a remote object, or something else.

JNDI throws naming exceptions when naming operations cannot be completed. The root naming exception, javax.naming.NamingException, is a catch all for any JNDI exception. The javax.naming package defines numerous subclasses of NamingException. A common naming exception, NameNotFoundException, is thrown when a name cannot be found, either because it doesn't exist or it is spelled incorrectly. JNDI throws a NoPermissionException when a program doesn't have sufficient rights or permissions and an OperationNotSupportedException when an application uses a JNDI method on an object that doesn't support that specific naming operation.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.