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 ...
Get Java Enterprise in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.