Book Home Java Enterprise in a Nutshell Search this book

6.8. Binding Objects

A Context stores its subordinates as a set of Binding objects. A binding is an association between an object and its name. Thus, as we've already seen, a Binding object contains an object, its name, and its class. We can add a new Binding to a Context with the bind() method. For example, here's how to add a binding for a new file object to an existing Context:

java.io.File newfile = java.io.File("c:\temp\newfile");
tempContext.bind("newfile", newfile);

Now, if we call list() on this Context, we'll see a new child named newfile. If you recall, in the previous section, I said that you have to drop out of JNDI to create a new file when using the Sun filesystem provider. The previous example shows what I meant. To create a file, we use the java.io.File constructor, which is not part of JNDI. Then, to bind the file into the naming system, we use the bind() method of Context.

If you try to bind a name to an object, and the name has already been used, the method throws a NameAlreadyBoundException. If you want to bind a new object to an existing name, use the rebind() method instead. Context also has an unbind() method you can use to remove a binding.



Library Navigation Links

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