public interface Context
Most of the methods have overloaded versions with one taking a
Name
parameter and one taking a String
.
These overloaded versions are equivalent in that if
the Name
and String
parameters are just
different representations of the same name, then the overloaded
versions of the same methods behave the same.
In the method descriptions below, only one version is fully documented.
The second version instead has a link to the first: the same
documentation applies to both.
For systems that support federation, String name arguments to Context methods are composite names. Name arguments that are instances of CompositeName are treated as composite names, while Name arguments that are not instances of CompositeName are treated as compound names (which might be instances of CompoundName or other implementations of compound names). This allows the results of NameParser.parse() to be used as arguments to the Context methods. Prior to JNDI 1.2, all name arguments were treated as composite names.
Furthermore, for systems that support federation, all names returned in a NamingEnumeration from list() and listBindings() are composite names represented as strings. See CompositeName for the string syntax of names.
For systems that do not support federation, the name arguments (in either Name or String forms) and the names returned in NamingEnumeration may be names in their own namespace rather than names in a composite namespace, at the discretion of the service provider.
lookup
method, when passed an empty name, will return a new Context instance
representing the same naming context.
For purposes of concurrency control, a Context operation that returns a NamingEnumeration is not considered to have completed while the enumeration is still in use, or while any referrals generated by that operation are still being followed.
JNDI applications need a way to communicate various preferences and properties that define the environment in which naming and directory services are accessed. For example, a context might require specification of security credentials in order to access the service. Another context might require that server configuration information be supplied. These are referred to as the environment of a context. The Context interface provides methods for retrieving and updating this environment.
The environment is inherited from the parent context as context methods proceed from one context to the next. Changes to the environment of one context do not directly affect those of other contexts.
It is implementation-dependent when environment properties are used and/or verified for validity. For example, some of the security-related properties are used by service providers to "log in" to the directory. This login process might occur at the time the context is created, or the first time a method is invoked on the context. When, and whether this occurs at all, is implementation-dependent. When environment properties are added or removed from the context, verifying the validity of the changes is again implementation-dependent. For example, verification of some properties might occur at the time the change is made, or at the time the next operation is performed on the context, or not at all.
Any object with a reference to a context may examine that context's environment. Sensitive information such as clear-text passwords should not be stored there unless the implementation is known to protect it.
To simplify the task of setting up the environment
required by a JNDI application,
application components and service providers may be distributed
along with resource files.
A JNDI resource file is a file in the properties file format (see
java.util.Properties
),
containing a list of key/value pairs.
The key is the name of the property (e.g. "java.naming.factory.object")
and the value is a string in the format defined
for that property. Here is an example of a JNDI resource file:
The JNDI class library reads the resource files and makes the property values freely available. Thus JNDI resource files should be considered to be "world readable", and sensitive information such as clear-text passwords should not be stored there.java.naming.factory.object=com.sun.jndi.ldap.AttrsToCorba:com.wiz.from.Person java.naming.factory.state=com.sun.jndi.ldap.CorbaToAttrs:com.wiz.from.Person java.naming.factory.control=com.sun.jndi.ldap.ResponseControlFactory
There are two kinds of JNDI resource files: provider and application.
[prefix/]jndiprovider.propertieswhere prefix is the package name of the provider's context implementation(s), with each period (".") converted to a slash ("/"). For example, suppose a service provider defines a context implementation with class name com.sun.jndi.ldap.LdapCtx. The provider resource for this provider is named com/sun/jndi/ldap/jndiprovider.properties. If the class is not in a package, the resource's name is simply jndiprovider.properties.
Certain methods in the JNDI class library make use of the standard JNDI properties that specify lists of JNDI factories:
ClassLoader.getResources()
)
all application resource files named jndi.properties
in the classpath.
In addition, if the file java.home/lib/jndi.properties
exists and is readable,
JNDI treats it as an additional application resource file.
(java.home indicates the
directory named by the java.home system property.)
All of the properties contained in these files are placed
into the environment of the initial context. This environment
is then inherited by other contexts.
For each property found in more than one application resource file, JNDI uses the first value found or, in a few cases where it makes sense to do so, it concatenates all of the values (details are given below). For example, if the "java.naming.factory.object" property is found in three jndi.properties resource files, the list of object factories is a concatenation of the property values from all three files. Using this scheme, each deployable component is responsible for listing the factories that it exports. JNDI automatically collects and uses all of these export lists when searching for factory classes.
When the JNDI class library needs to determine the value of a property, it does so by merging the values from the following two sources, in order:
When a service provider needs to determine the value of a property, it will generally take that value directly from the environment. A service provider may define provider-specific properties to be placed in its own provider resource file. In that case it should merge values as described in the previous paragraph.
In this way, each service provider developer can specify a list of factories to use with that service provider. These can be modified by the application resources specified by the deployer of the application or applet, which in turn can be modified by the user.
Modifier and Type | Field and Description |
---|---|
static String |
APPLET
Constant that holds the name of the environment property for
specifying an applet for the initial context constructor to use
when searching for other properties.
|
static String |
AUTHORITATIVE
Constant that holds the name of the environment property for
specifying the authoritativeness of the service requested.
|
static String |
BATCHSIZE
Constant that holds the name of the environment property for
specifying the batch size to use when returning data via the
service's protocol.
|
static String |
DNS_URL
Constant that holds the name of the environment property
for specifying the DNS host and domain names to use for the
JNDI URL context (for example, "dns://somehost/wiz.com").
|
static String |
INITIAL_CONTEXT_FACTORY
Constant that holds the name of the environment property
for specifying the initial context factory to use.
|
static String |
LANGUAGE
Constant that holds the name of the environment property for
specifying the preferred language to use with the service.
|
static String |
OBJECT_FACTORIES
Constant that holds the name of the environment property
for specifying the list of object factories to use.
|
static String |
PROVIDER_URL
Constant that holds the name of the environment property
for specifying configuration information for the service provider
to use.
|
static String |
REFERRAL
Constant that holds the name of the environment property for
specifying how referrals encountered by the service provider
are to be processed.
|
static String |
SECURITY_AUTHENTICATION
Constant that holds the name of the environment property for
specifying the security level to use.
|
static String |
SECURITY_CREDENTIALS
Constant that holds the name of the environment property for
specifying the credentials of the principal for authenticating
the caller to the service.
|
static String |
SECURITY_PRINCIPAL
Constant that holds the name of the environment property for
specifying the identity of the principal for authenticating
the caller to the service.
|
static String |
SECURITY_PROTOCOL
Constant that holds the name of the environment property for
specifying the security protocol to use.
|
static String |
STATE_FACTORIES
Constant that holds the name of the environment property
for specifying the list of state factories to use.
|
static String |
URL_PKG_PREFIXES
Constant that holds the name of the environment property
for specifying the list of package prefixes to use when
loading in URL context factories.
|
Modifier and Type | Method and Description |
---|---|
Object |
addToEnvironment(String propName,
Object propVal)
Adds a new environment property to the environment of this
context.
|
void |
bind(Name name,
Object obj)
Binds a name to an object.
|
void |
bind(String name,
Object obj)
Binds a name to an object.
|
void |
close()
Closes this context.
|
Name |
composeName(Name name,
Name prefix)
Composes the name of this context with a name relative to
this context.
|
String |
composeName(String name,
String prefix)
Composes the name of this context with a name relative to
this context.
|
Context |
createSubcontext(Name name)
Creates and binds a new context.
|
Context |
createSubcontext(String name)
Creates and binds a new context.
|
void |
destroySubcontext(Name name)
Destroys the named context and removes it from the namespace.
|
void |
destroySubcontext(String name)
Destroys the named context and removes it from the namespace.
|
Hashtable<?,?> |
getEnvironment()
Retrieves the environment in effect for this context.
|
String |
getNameInNamespace()
Retrieves the full name of this context within its own namespace.
|
NameParser |
getNameParser(Name name)
Retrieves the parser associated with the named context.
|
NameParser |
getNameParser(String name)
Retrieves the parser associated with the named context.
|
NamingEnumeration<NameClassPair> |
list(Name name)
Enumerates the names bound in the named context, along with the
class names of objects bound to them.
|
NamingEnumeration<NameClassPair> |
list(String name)
Enumerates the names bound in the named context, along with the
class names of objects bound to them.
|
NamingEnumeration<Binding> |
listBindings(Name name)
Enumerates the names bound in the named context, along with the
objects bound to them.
|
NamingEnumeration<Binding> |
listBindings(String name)
Enumerates the names bound in the named context, along with the
objects bound to them.
|
Object |
lookup(Name name)
Retrieves the named object.
|
Object |
lookup(String name)
Retrieves the named object.
|
Object |
lookupLink(Name name)
Retrieves the named object, following links except
for the terminal atomic component of the name.
|
Object |
lookupLink(String name)
Retrieves the named object, following links except
for the terminal atomic component of the name.
|
void |
rebind(Name name,
Object obj)
Binds a name to an object, overwriting any existing binding.
|
void |
rebind(String name,
Object obj)
Binds a name to an object, overwriting any existing binding.
|
Object |
removeFromEnvironment(String propName)
Removes an environment property from the environment of this
context.
|
void |
rename(Name oldName,
Name newName)
Binds a new name to the object bound to an old name, and unbinds
the old name.
|
void |
rename(String oldName,
String newName)
Binds a new name to the object bound to an old name, and unbinds
the old name.
|
void |
unbind(Name name)
Unbinds the named object.
|
void |
unbind(String name)
Unbinds the named object.
|
static final String INITIAL_CONTEXT_FACTORY
The value of this constant is "java.naming.factory.initial".
static final String OBJECT_FACTORIES
The value of this constant is "java.naming.factory.object".
static final String STATE_FACTORIES
The value of this constant is "java.naming.factory.state".
static final String URL_PKG_PREFIXES
The value of this constant is "java.naming.factory.url.pkgs".
NamingManager.getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable<?, ?>)
,
NamingManager.getURLContext(java.lang.String, java.util.Hashtable<?, ?>)
,
ObjectFactory
,
addToEnvironment(String, Object)
,
removeFromEnvironment(String)
,
APPLET
,
Constant Field Valuesstatic final String PROVIDER_URL
The value of this constant is "java.naming.provider.url".
static final String DNS_URL
The value of this constant is "java.naming.dns.url".
static final String AUTHORITATIVE
The value of this constant is "java.naming.authoritative".
static final String BATCHSIZE
The value of this constant is "java.naming.batchsize".
static final String REFERRAL
The value of this constant is "java.naming.referral".
static final String SECURITY_PROTOCOL
The value of this constant is "java.naming.security.protocol".
static final String SECURITY_AUTHENTICATION
The value of this constant is "java.naming.security.authentication".
static final String SECURITY_PRINCIPAL
The value of this constant is "java.naming.security.principal".
static final String SECURITY_CREDENTIALS
The value of this constant is "java.naming.security.credentials".
static final String LANGUAGE
The value of this constant is "java.naming.language".
static final String APPLET
The value of this constant is "java.naming.applet".
addToEnvironment(String, Object)
,
removeFromEnvironment(String)
,
InitialContext
,
Constant Field ValuesObject lookup(Name name) throws NamingException
name
- the name of the object to look upNamingException
- if a naming exception is encounteredlookup(String)
,
lookupLink(Name)
Object lookup(String name) throws NamingException
lookup(Name)
for details.name
- the name of the object to look upNamingException
- if a naming exception is encounteredvoid bind(Name name, Object obj) throws NamingException
name
- the name to bind; may not be emptyobj
- the object to bind; possibly nullNameAlreadyBoundException
- if name is already boundInvalidAttributesException
- if object did not supply all mandatory attributesNamingException
- if a naming exception is encounteredbind(String, Object)
,
rebind(Name, Object)
,
DirContext.bind(Name, Object,
javax.naming.directory.Attributes)
void bind(String name, Object obj) throws NamingException
bind(Name, Object)
for details.name
- the name to bind; may not be emptyobj
- the object to bind; possibly nullNameAlreadyBoundException
- if name is already boundInvalidAttributesException
- if object did not supply all mandatory attributesNamingException
- if a naming exception is encounteredvoid rebind(Name name, Object obj) throws NamingException
If the object is a DirContext, any existing attributes associated with the name are replaced with those of the object. Otherwise, any existing attributes associated with the name remain unchanged.
name
- the name to bind; may not be emptyobj
- the object to bind; possibly nullInvalidAttributesException
- if object did not supply all mandatory attributesNamingException
- if a naming exception is encounteredrebind(String, Object)
,
bind(Name, Object)
,
DirContext.rebind(Name, Object,
javax.naming.directory.Attributes)
,
DirContext
void rebind(String name, Object obj) throws NamingException
rebind(Name, Object)
for details.name
- the name to bind; may not be emptyobj
- the object to bind; possibly nullInvalidAttributesException
- if object did not supply all mandatory attributesNamingException
- if a naming exception is encounteredvoid unbind(Name name) throws NamingException
name
from the target context--that named by all but the terminal
atomic part of name
.
This method is idempotent. It succeeds even if the terminal atomic name is not bound in the target context, but throws NameNotFoundException if any of the intermediate contexts do not exist.
Any attributes associated with the name are removed. Intermediate contexts are not changed.
name
- the name to unbind; may not be emptyNameNotFoundException
- if an intermediate context does not existNamingException
- if a naming exception is encounteredunbind(String)
void unbind(String name) throws NamingException
unbind(Name)
for details.name
- the name to unbind; may not be emptyNameNotFoundException
- if an intermediate context does not existNamingException
- if a naming exception is encounteredvoid rename(Name oldName, Name newName) throws NamingException
oldName
- the name of the existing binding; may not be emptynewName
- the name of the new binding; may not be emptyNameAlreadyBoundException
- if newName is already boundNamingException
- if a naming exception is encounteredrename(String, String)
,
bind(Name, Object)
,
rebind(Name, Object)
void rename(String oldName, String newName) throws NamingException
rename(Name, Name)
for details.oldName
- the name of the existing binding; may not be emptynewName
- the name of the new binding; may not be emptyNameAlreadyBoundException
- if newName is already boundNamingException
- if a naming exception is encounteredNamingEnumeration<NameClassPair> list(Name name) throws NamingException
If a binding is added to or removed from this context, its effect on an enumeration previously returned is undefined.
name
- the name of the context to listNamingException
- if a naming exception is encounteredlist(String)
,
listBindings(Name)
,
NameClassPair
NamingEnumeration<NameClassPair> list(String name) throws NamingException
list(Name)
for details.name
- the name of the context to listNamingException
- if a naming exception is encounteredNamingEnumeration<Binding> listBindings(Name name) throws NamingException
If a binding is added to or removed from this context, its effect on an enumeration previously returned is undefined.
name
- the name of the context to listNamingException
- if a naming exception is encounteredlistBindings(String)
,
list(Name)
,
Binding
NamingEnumeration<Binding> listBindings(String name) throws NamingException
listBindings(Name)
for details.name
- the name of the context to listNamingException
- if a naming exception is encounteredvoid destroySubcontext(Name name) throws NamingException
This method is idempotent. It succeeds even if the terminal atomic name is not bound in the target context, but throws NameNotFoundException if any of the intermediate contexts do not exist.
In a federated naming system, a context from one naming system may be bound to a name in another. One can subsequently look up and perform operations on the foreign context using a composite name. However, an attempt destroy the context using this composite name will fail with NotContextException, because the foreign context is not a "subcontext" of the context in which it is bound. Instead, use unbind() to remove the binding of the foreign context. Destroying the foreign context requires that the destroySubcontext() be performed on a context from the foreign context's "native" naming system.
name
- the name of the context to be destroyed; may not be emptyNameNotFoundException
- if an intermediate context does not existNotContextException
- if the name is bound but does not name a
context, or does not name a context of the appropriate typeContextNotEmptyException
- if the named context is not emptyNamingException
- if a naming exception is encountereddestroySubcontext(String)
void destroySubcontext(String name) throws NamingException
destroySubcontext(Name)
for details.name
- the name of the context to be destroyed; may not be emptyNameNotFoundException
- if an intermediate context does not existNotContextException
- if the name is bound but does not name a
context, or does not name a context of the appropriate typeContextNotEmptyException
- if the named context is not emptyNamingException
- if a naming exception is encounteredContext createSubcontext(Name name) throws NamingException
name
- the name of the context to create; may not be emptyNameAlreadyBoundException
- if name is already boundInvalidAttributesException
- if creation of the subcontext requires specification of
mandatory attributesNamingException
- if a naming exception is encounteredcreateSubcontext(String)
,
DirContext.createSubcontext(javax.naming.Name, javax.naming.directory.Attributes)
Context createSubcontext(String name) throws NamingException
createSubcontext(Name)
for details.name
- the name of the context to create; may not be emptyNameAlreadyBoundException
- if name is already boundInvalidAttributesException
- if creation of the subcontext requires specification of
mandatory attributesNamingException
- if a naming exception is encounteredObject lookupLink(Name name) throws NamingException
name
- the name of the object to look upNamingException
- if a naming exception is encounteredlookupLink(String)
Object lookupLink(String name) throws NamingException
lookupLink(Name)
for details.name
- the name of the object to look upNamingException
- if a naming exception is encounteredNameParser getNameParser(Name name) throws NamingException
name
- the name of the context from which to get the parserNamingException
- if a naming exception is encounteredgetNameParser(String)
,
CompoundName
NameParser getNameParser(String name) throws NamingException
getNameParser(Name)
for details.name
- the name of the context from which to get the parserNamingException
- if a naming exception is encounteredName composeName(Name name, Name prefix) throws NamingException
name
) relative to this context, and
the name (prefix
) of this context relative to one
of its ancestors, this method returns the composition of the
two names using the syntax appropriate for the naming
system(s) involved. That is, if name
names an
object relative to this context, the result is the name of the
same object, but relative to the ancestor context. None of the
names may be null.
For example, if this context is named "wiz.com" relative to the initial context, then
composeName("east", "wiz.com")might return
"east.wiz.com"
.
If instead this context is named "org/research", then
composeName("user/jane", "org/research")might return
"org/research/user/jane"
while
composeName("user/jane", "research")returns
"research/user/jane"
.name
- a name relative to this contextprefix
- the name of this context relative to one of its ancestorsprefix
and name
NamingException
- if a naming exception is encounteredcomposeName(String, String)
String composeName(String name, String prefix) throws NamingException
composeName(Name, Name)
for details.name
- a name relative to this contextprefix
- the name of this context relative to one of its ancestorsprefix
and name
NamingException
- if a naming exception is encounteredObject addToEnvironment(String propName, Object propVal) throws NamingException
propName
- the name of the environment property to add; may not be nullpropVal
- the value of the property to add; may not be nullNamingException
- if a naming exception is encounteredgetEnvironment()
,
removeFromEnvironment(String)
Object removeFromEnvironment(String propName) throws NamingException
propName
- the name of the environment property to remove; may not be nullNamingException
- if a naming exception is encounteredgetEnvironment()
,
addToEnvironment(String, Object)
Hashtable<?,?> getEnvironment() throws NamingException
The caller should not make any changes to the object returned: their effect on the context is undefined. The environment of this context may be changed using addToEnvironment() and removeFromEnvironment().
NamingException
- if a naming exception is encounteredaddToEnvironment(String, Object)
,
removeFromEnvironment(String)
void close() throws NamingException
This method is idempotent: invoking it on a context that has already been closed has no effect. Invoking any other method on a closed context is not allowed, and results in undefined behaviour.
NamingException
- if a naming exception is encounteredString getNameInNamespace() throws NamingException
Many naming services have a notion of a "full name" for objects in their respective namespaces. For example, an LDAP entry has a distinguished name, and a DNS record has a fully qualified name. This method allows the client application to retrieve this name. The string returned by this method is not a JNDI composite name and should not be passed directly to context methods. In naming systems for which the notion of full name does not make sense, OperationNotSupportedException is thrown.
OperationNotSupportedException
- if the naming system does
not have the notion of a full nameNamingException
- if a naming exception is encountered Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2020, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.