O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating and Destroying Contexts

With JNDI, you can create a context in a naming system using the createSubcontext() method of an existing Context. All you have to specify in this call is the name of the new subcontext. Note that Context doesn’t provide a public constructor; creating a new context requires a parent Context (such as an InitialContext) whose createSubcontext() method we can call.

When you call createSubcontext(), the JNDI service provider you are using looks at the class of the Context whose method you are calling. Based on this class and the provider’s own internal logic, the provider creates a new object of a particular class. You don’t get to pick the class of this object; the provider has all the control over the class of the object it creates (you do, however, have control over the class of object that is created when using directory services, as we’ll see shortly). The documentation for a service provider should tell you what kinds of objects createSubcontext() can create. Note that whatever object the provider creates, it always implements Context; there is no way to use JNDI to create an object that doesn’t implement Context.

For example, if we are using the Sun filesystem provider and our current Context is a directory, calling createSubcontext() causes the provider to create a directory, not a file. This makes sense, as a directory can have subordinates and thus implements Context. There is actually no way to create a file using the JNDI API and the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required