The JNDI Application Programming Interface


The JNDI API is provided by the javax.naming.Context interface for naming services and javax.naming.directory.DirContext for directory services. J2EE applications rely exclusively on naming services; therefore, this introduction to the JNDI API focuses on the javax.naming.Context class. However, an overview of using JNDI with directory services is also provided to give a complete introduction to the JNDI API. The Java application creates and uses a javax.naming.Context object to lookup a name in the naming service. The return value is the object that was bound to that name .

Looking Up an Object Using the JNDI Context

After the context is created, the lookup() method is used to locate an object by name, errors are handled with exceptions. The NameNotFoundException is thrown if the name is not found in the naming service. The NamingException is thrown if there was a communication failure with the naming service. Listing 14.7 retrieves an EJB named MySessionBean from the Context created for WebLogic Server.

Listing 14.7 Retrieving MySessionBean from the Context Created for WebLogic Server
 try {     // create the Context first...     // use ctx to lookup an EJB named MySessionBean     MySessionBean msb = (MySessionBean)ctx.lookup( "ejb.mySessionBean" );     // use MySessionBean here } catch( NameNotFoundException x ) {     System.err.println( "ejb.mySessionBean was not found" ); } catch( NamingException x ) {     System.err.println( x.toString() ); } 

Closing the JNDI Context

It is important to close the Context after the client is finished using it. By doing so, you release the resources that were created for the Context . A finally block is recommended to ensure the Context is closed whether or not an exception is thrown. The structure of using the finally block to close the context is shown in Listing 14.8.

Listing 14.8 Using the finally Block to Close the Context
 Context ctx = null; try {     // create the Context     ctx = new InitialContext();     // use the Context } catch( NamingException x ) {     System.err.println( x.toString() ); } catch( Exception x ) {     System.err.println( "unexpected exception: " + x ); } finally {     try {         if( ctx != null )           ctx.close();     } catch( Exception x ) {         System.err.println( "failed to close Context: " + x );     } } 

Binding an Object Using the JNDI Context

In this section, you learn how to bind an object with a JNDI naming service. The binding is created using the bind() or rebind() methods of the Context object.

Note

An Enterprise Java Bean does not contain code to explicitly bind the EJB with the naming service. The EJB Container in WebLogic Server is responsible for updating the JNDI tree when the bean is deployed. The JNDI name for the binding is defined in the deployment descriptor for the EJB.


Follow these steps to bind an object with a JNDI naming service:

  1. Choose a unique name for the object.

  2. Create the object.

  3. Create a Context for the selected service provider.

  4. To support clustering for WebLogic Server, decide if the object is pinned or replicated. The environment property for controlling replication in WebLogic Server is WLContext.REPLICATE_BINDINGS . The value for this property is set to either "true" or "false" as a java.lang.String.

  5. Handle exceptions.

The parameters to the Context method bind() are a name as a String and an object. You must use the rebind() method to force the new object to be bound to a name that already exists. You use the unbind() method to remove the binding from the service provider. The parameter to unbind() is the bound name. All JNDI Context methods throw NamingException to indicate that a failure occurred while trying to communicate with the naming service. Listing 14.9 shows an example of binding a pinned object with the WebLogic naming service.

Listing 14.9 Binding a Pinned Object with WebLogic Server
 private void pinObject( String name, Object obj ) {   try {     Hashtable env = new Hashtable();     // pin the object     env.put( WLContext.REPLICATE_BINDINGS, "false" );     Context ctx = new InitialContext( env );     // bind the object     ctx.bind( name, obj );   } catch( NamingException x )     System.err.println( "Failed to bind " + name + ": " + x );   } } 


BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net