Binding JNDI Objects

After the initial JNDI context has been obtained, a program can look up existing objects and bind new objects to the context.

When working with EJBs, the main JNDI activity is to look up existing bound objects; the J2EE server does most of the binding of the objects automatically.

Because this section discusses the binding of objects, you can skip it if your primary purpose for using JNDI is to obtain EJB and other references within a J2EE application.

Binding Objects

Binding an object means adding a name to the JNDI service and associating that name with a Java object. The name and object are bound to a context. Listing 3.1 shows how a text message can be bound to the name sams/book.

Listing 3.1. Full Text of JNDIBind.java
 1: import javax.naming.*;
 2: public class JNDIBind
 3: {
 4:     private final static String JNDI = "sams/book";
 5:
 6:     public static void main(String[] args) {
 7:         try {
 8:             Context ic = new InitialContext();
 9:             ic.bind(JNDI,"Teach Yourself J2EE in 21 Days");
10:             System.out.println("Bound "+JNDI);
11:         }
12:         catch (NamingException ex) {
13:              System.err.println(ex);
14:              System.exit(1);
15:         }
16:     }
17: }
						

The object to be bound must implement the Serializable interface so that the name server can store a copy of the object.

The Context.bind() method will fail with a NameAlreadyBoundException (which extends NamingException) if an object is already bound to that name. Another subclass of NamingException is thrown if there is some other form of error, such as an invalid name. Remember that different Service Providers may have different naming conventions.

Binding Problems

A Service Provider may not support binding of all types of objects. If the service cannot bind a particular object, it will throw an exception.

Using the default naming service for J2EE RI that uses a transient CORBA naming service, the class of the object must be in the CLASSPATH used by the J2EE RI JNDI server.

For now, this means using standard J2SE and J2EE classes or configuring the J2EE RI services to include your class files. The recommended approach is to edit the user configuration file (userconfig.sh or userconfig.bat) in the bin directory of the J2EE RI home directory, and add the required class directories or JAR files to the J2EE_CLASSPATH variable defined in the configuration file.

An alternative is to use a Web Service to dynamically upload the required class files. Dynamic uploading of class files is dicussed in the “Loading Classes from a Code Base” section, later in this chapter.

Some Naming Services (such as LDAP) may use security features to ensure that only authorized programs can bind new objects. The bind() method can also fail if it violates any security features of the underlying naming service. The “Security” section of today's lesson covers this in more detail.

Name Persistence

A bound object normally remains in the namespace until it is unbound. If the bound name remains across server restarts, it is said to be persistent. Commercial servers, such as NDS, Active Directory, and LDAP, are persistent name servers and store the bound names and ancilliary information on disk (typically in a database).

The default naming service for Sun Microsystems' J2EE RI is a transient service; it reloads bound objects from configuration files in the SDK home directory whenever it is restarted. This naming service will not retain objects bound with the Context.bind() method across server restarts.

Rebinding Objects

You can use the rebind() method to solve the problem of bind() failing if a name is already bound. For example,

ic.rebind("sams/book","Teach Yourself J2EE in 21 Days");

The code unbinds any existing object bound to that name and binds the new object in its place.

Using rebind() is a good design technique when a programmer is sure the name will not be in use by another component. The alternative is to explicitly unbind the old name first if it is in use as discussed in the next section on “Unbinding Objects.”

Unbinding Objects

You can remove an object from a namespace by using the Context.unbind() method. A program uses this method when it is closing down and needs to remove its advertised service because a bound name is not automatically unbound when the program shuts down.

Another common use for unbind() is to test if a name is already in use and unbind the old object before binding a new object. The advantage of using unbind() in preference to rebind() is that you can verify that the object to be unbound is at least of the same type as the new object to be bound.

String JNDI = "sams/book";
try {
    Object o = ic.lookup(JNDI);
    if (o instanceof String)
        ic.unbind (JNDI);
}
catch (NameNotFoundException ex) {}
ic.bind(JNDI,"Teach Yourself J2EE in 21 Days");

This example rebinds a string bound to the name sams/book, but will fail with a NameAlreadyBoundException if the name is bound to another class of object. This is a better design approach than that of using the rebind() method.

Renaming Objects

You can rename objects using Context.rename() by specifying the old name and then the new name as parameters. The new name must specify a name in the same context as the old name. An object must be bound to the old name, and the new name must not have a bound object; otherwise, a NamingException is thrown.

ic.rename("sams/book","sams/teachyourself");

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

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