JNDI 1.1.1

javax.naming
Class InitialContext

java.lang.Object
  |
  +--javax.naming.InitialContext
Direct Known Subclasses:
InitialDirContext

public class InitialContext
extends java.lang.Object
implements Context

This class is the starting context for performing naming operations.

All naming operations are relative to a context. The initial context implements the Context interface and provides the starting point for resolution of names.

The initial context implementation is determined at runtime. The default policy uses the environment or system property "java.naming.factory.initial", which contains the class name of the initial context factory. (If both are present, the environment property takes precedence).

If a URL string (with syntax scheme_id:rest_of_name) is passed to methods in InitialContext, either as a String argument or as the first component of Name, the URL's scheme id is used to locate the context factory for handling that scheme. If none is found, the initial context specified by "java.naming.factory.initial" is used. See jndi.naming.spi.NamingManager.getURLContext() for details on how URL context factories are located.

This default policy of locating the initial context can be overridden by calling javax.naming.spi.NamingManager.setInitialContextFactoryBuilder().

NoInitialContextException is thrown when an initial context cannot be instantiated. This exception can be thrown during any interaction with the InitialContext, not only when the InitialContext is constructed. For example, the implementation of the initial context might lazily retrieve the context only when actual methods are invoked on it. The application should not have any dependency on when the existence of an initial context is determined.

When the environment or system property java.naming.factory.initial is non-null, the InitialContext constructor will attempt to create the initial context specified therein. At that time, the initial context factory involved might throw an exception if a problem is encountered. However, it is provider implementation-dependent when it verifies and indicates to the users of the initial context any environment property- or connection- related problems. It can do so lazily--delaying until an operation is performed on the context, or eagerly, at the time the context is constructed.

An InitialContext instance is not synchronized against concurrent access by multiple threads. Multiple threads each manipulating their own InitialContext instances need not synchronize. Threads that need to access a single InitialContext instance concurrently should synchronize amongst themselves and provide the necessary locking.

See Also:
Context, NamingManager.setInitialContextFactoryBuilder(javax.naming.spi.InitialContextFactoryBuilder)

Field Summary
protected  Context defaultInitCtx
          Field holding the result of calling NamingManager.getInitialContext().
protected  boolean gotDefault
          Field indicating whether the initial context has been obtained by calling NamingManager.getInitialContext().
protected  java.util.Hashtable myProps
          The environment associated with this InitialContext.
 
Constructor Summary
InitialContext()
          Constructs an initial context.
InitialContext(java.util.Hashtable environment)
          Constructs an initial context using information supplied in 'environment'.
 
Method Summary
 java.lang.Object addToEnvironment(java.lang.String propName, java.lang.Object propVal)
          Adds a new environment property to the environment of this context.
 void bind(Name name, java.lang.Object obj)
          Binds 'name' to the object 'obj'.
 void bind(java.lang.String name, java.lang.Object obj)
          Binds the string name 'name' to the object 'obj'.
 void close()
          Closes this initial context.
 Name composeName(Name name, Name prefix)
          Composes the name of this context with a name relative to this context.
 java.lang.String composeName(java.lang.String name, java.lang.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(java.lang.String name)
          Creates and binds a new context.
 void destroySubcontext(Name name)
          Destroys the named context and removes it from the namespace.
 void destroySubcontext(java.lang.String name)
          Destroys the named context and removes it from the namespace.
protected  Context getDefaultInitCtx()
          Retrieves the initial context by calling NamingManager.getInitialContext() and cache it in defaultInitCtx.
 java.util.Hashtable getEnvironment()
          Retrieves the environment in effect for the initial context.
 NameParser getNameParser(Name name)
          Retrieves the parser associated with the named context.
 NameParser getNameParser(java.lang.String name)
          Retrieves the parser associated with the named context.
protected  Context getURLOrDefaultInitCtx(Name name)
          Retrieves a context for resolving name.
protected  Context getURLOrDefaultInitCtx(java.lang.String name)
          Retrieves a context for resolving the string name name.
 NamingEnumeration list(Name name)
          Enumerates the names and the class names of their bound objects in the context named relative to this initial context.
 NamingEnumeration list(java.lang.String name)
          Enumerates the names and the class names of their bound objects in the context named relative to this initial context.
 NamingEnumeration listBindings(Name name)
          Enumerates the names and their bound objects in the context named relative to this initial context.
 NamingEnumeration listBindings(java.lang.String name)
          Enumerates the names and their bound objects in the context named relative to this initial context.
 java.lang.Object lookup(Name name)
          Retrieves the object bound to name resolved relative to the initial context.
 java.lang.Object lookup(java.lang.String name)
          Retrieves the object bound to name resolved relative to the initial context.
 java.lang.Object lookupLink(Name name)
          Retrieve the object named relative to this initial context, following links except for the terminal atomic component of name.
 java.lang.Object lookupLink(java.lang.String name)
          Retrieve the object named relative to this initial context, following links except for the terminal atomic component of name.
 void rebind(Name name, java.lang.Object obj)
          Binds 'name' to the object 'obj', overwrite any existing binding.
 void rebind(java.lang.String name, java.lang.Object obj)
          Binds the string name 'name' to the object 'obj', overwrite any existing binding.
 java.lang.Object removeFromEnvironment(java.lang.String propName)
          Removes an environment property from the environment of this context.
 void rename(Name oldName, Name newName)
          Binds 'newName' to the object bound to 'oldName', and unbinds 'oldName'.
 void rename(java.lang.String oldName, java.lang.String newName)
          Binds 'newName' to the object bound to 'oldName', and unbinds 'oldName'.
 void unbind(Name name)
          Unbinds 'name' resolved relative to this initial context.
 void unbind(java.lang.String name)
          Unbinds 'name' resolved relative to this initial context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

myProps

protected java.util.Hashtable myProps
The environment associated with this InitialContext. It is initialized to null and is updated by the constructor that accepts an environment.
See Also:
addToEnvironment(java.lang.String, java.lang.Object), removeFromEnvironment(java.lang.String), getEnvironment()

defaultInitCtx

protected Context defaultInitCtx
Field holding the result of calling NamingManager.getInitialContext(). It is set by getDefaultInitCtx() the first time getDefaultInitCtx() is called. Subsequent invocations of getDefaultInitCtx() return the value of defaultInitCtx.
See Also:
getDefaultInitCtx()

gotDefault

protected boolean gotDefault
Field indicating whether the initial context has been obtained by calling NamingManager.getInitialContext(). If true, its result is in defaultInitCtx.
Constructor Detail

InitialContext

public InitialContext()
               throws NamingException
Constructs an initial context. The environment of the new context are set to null.
Throws:
NamingException - If a problem was encountered while creating the initial context.

InitialContext

public InitialContext(java.util.Hashtable environment)
               throws NamingException
Constructs an initial context using information supplied in 'environment'. See the Context class description for more details about environment.
Parameters:
environment - The possibly null environment used for creating the initial context (such as user name, password, etc.)
Throws:
NamingException - If a problem was encountered while creating the initial context.
Method Detail

getDefaultInitCtx

protected Context getDefaultInitCtx()
                             throws NamingException
Retrieves the initial context by calling NamingManager.getInitialContext() and cache it in defaultInitCtx. Set gotDefault so that we know we've tried this before.
Returns:
The non-null cached initial context.
Throws:
NoInitialContextException - If cannot find an initial context.
NamingException - If a naming exception was encountered.

getURLOrDefaultInitCtx

protected Context getURLOrDefaultInitCtx(java.lang.String name)
                                  throws NamingException
Retrieves a context for resolving the string name name. If name name is a URL string, then attempt to find a URL context for it. If none is found, or if name is not a URL string, then return getDefaultInitCtx().

See getURLOrDefaultInitCtx(Name) for description of how a subclass should use this method.

Parameters:
name - The non-null name for which to get the context.
Returns:
A URL context for name or the cached initial context. The result cannot be null.
Throws:
NoInitialContextException - If cannot find an initial context.
NamingException - In a naming exception is encountered.
See Also:
NamingManager.getURLContext(java.lang.String, java.util.Hashtable)

getURLOrDefaultInitCtx

protected Context getURLOrDefaultInitCtx(Name name)
                                  throws NamingException
Retrieves a context for resolving name. If the first component of name name is a URL string, then attempt to find a URL context for it. If none is found, or if the first component of name is not a URL string, then return getDefaultInitCtx().

When creating a subclass of InitialContext, use this method as follows. Define a new method that uses this method to get an initial context of the desired subclass.

 protected XXXContext getURLOrDefaultInitXXXCtx(Name name) 
 throws NamingException {
  Context answer = getURLOrDefaultInitCtx(name);
  if (!(answer instanceof XXXContext)) {
    if (answer == null) {
	    throw new NoInitialContextException();
    } else {
	    throw new NotContextException("Not an XXXContext");
    }
 	}
	return (XXXContext)answer;
 }
 
When providing implementations for the new methods in the subclass, use this newly defined method to get the initial context.

 public Object XXXMethod1(Name name, ...) {
	throws NamingException {
    return getURLOrDefaultInitXXXCtx(name).XXXMethod1(name, ...);
 }
 
Parameters:
name - The non-null name for which to get the context.
Returns:
A URL context for name or the cached initial context. The result cannot be null.
Throws:
NoInitialContextException - If cannot find an initial context.
NamingException - In a naming exception is encountered.
See Also:
NamingManager.getURLContext(java.lang.String, java.util.Hashtable)

lookup

public java.lang.Object lookup(java.lang.String name)
                        throws NamingException
Retrieves the object bound to name resolved relative to the initial context.
Specified by:
lookup in interface Context
Parameters:
name - The non-null string name of the object to lookup.
Returns:
The non-null object to which name is bound.
Throws:
NamingException - If a naming exception was encountered during the lookup.
See Also:
Context.lookup(javax.naming.Name)

lookup

public java.lang.Object lookup(Name name)
                        throws NamingException
Retrieves the object bound to name resolved relative to the initial context.
Specified by:
lookup in interface Context
Parameters:
name - The non-null name of the object to lookup.
Returns:
The non-null object to which name is bound.
Throws:
NamingException - If a naming exception was encountered during the lookup.
See Also:
Context.lookup(javax.naming.Name)

bind

public void bind(java.lang.String name,
                 java.lang.Object obj)
          throws NamingException
Binds the string name 'name' to the object 'obj'.
Specified by:
bind in interface Context
Parameters:
name - The non-null name to bind. It cannot be empty.
obj - The non-null object to bind.
Throws:
NameAlreadyBoundException - If name is already bound.
InvalidAttributesException - If obj did not supply all mandatory attributes.
NamingException - If a naming exception was encountered.
See Also:
Context.bind(javax.naming.Name, java.lang.Object)

bind

public void bind(Name name,
                 java.lang.Object obj)
          throws NamingException
Binds 'name' to the object 'obj'.
Specified by:
bind in interface Context
Parameters:
name - The non-null name to bind. It cannot be empty.
obj - The non-null object to bind.
Throws:
NameAlreadyBoundException - If name is already bound.
InvalidAttributesException - If obj did not supply all mandatory attributes.
NamingException - If a naming exception was encountered.
See Also:
Context.bind(javax.naming.Name, java.lang.Object)

rebind

public void rebind(java.lang.String name,
                   java.lang.Object obj)
            throws NamingException
Binds the string name 'name' to the object 'obj', overwrite any existing binding.
Specified by:
rebind in interface Context
Parameters:
name - The non-null name to bind. It cannot be empty.
obj - The non-null object to bind. If obj is a DirContext,
Throws:
InvalidAttributesException - If obj did not supply all mandatory attributes.
NamingException - If a naming exception was encountered.
See Also:
Context.rebind(javax.naming.Name, java.lang.Object)

rebind

public void rebind(Name name,
                   java.lang.Object obj)
            throws NamingException
Binds 'name' to the object 'obj', overwrite any existing binding.
Specified by:
rebind in interface Context
Parameters:
name - The non-null name to bind. It cannot be empty.
obj - The non-null object to bind. If obj is a DirContext,
Throws:
InvalidAttributesException - If obj did not supply all mandatory attributes.
NamingException - If a naming exception was encountered.
See Also:
Context.rebind(javax.naming.Name, java.lang.Object)

unbind

public void unbind(java.lang.String name)
            throws NamingException
Unbinds 'name' resolved relative to this initial context.
Specified by:
unbind in interface Context
Parameters:
name - The non-null name to unbind. It cannot be empty.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.unbind(Name)

unbind

public void unbind(Name name)
            throws NamingException
Unbinds 'name' resolved relative to this initial context.
Specified by:
unbind in interface Context
Parameters:
name - The non-null name to unbind. It cannot be empty.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.unbind(Name)

rename

public void rename(java.lang.String oldName,
                   java.lang.String newName)
            throws NamingException
Binds 'newName' to the object bound to 'oldName', and unbinds 'oldName'.
Specified by:
rename in interface Context
Parameters:
oldName - The non-null name of the existing binding,
newName - The non-null name of the new binding,
Throws:
NameAlreadyBoundException - If newName is already bound.
NamingException - If a naming exception was encountered.
See Also:
Context.rename(Name, Name)

rename

public void rename(Name oldName,
                   Name newName)
            throws NamingException
Binds 'newName' to the object bound to 'oldName', and unbinds 'oldName'.
Specified by:
rename in interface Context
Parameters:
oldName - The non-null name of the existing binding,
newName - The non-null name of the new binding,
Throws:
NameAlreadyBoundException - If newName is already bound.
NamingException - If a naming exception was encountered.
See Also:
Context.rename(Name, Name)

list

public NamingEnumeration list(java.lang.String name)
                       throws NamingException
Enumerates the names and the class names of their bound objects in the context named relative to this initial context.
Specified by:
list in interface Context
Parameters:
name - The non-null name of the context to list.
Returns:
A non-null enumeration of the names and class names (NameClassPair) of the bindings in this context.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.list(Name)

list

public NamingEnumeration list(Name name)
                       throws NamingException
Enumerates the names and the class names of their bound objects in the context named relative to this initial context.
Specified by:
list in interface Context
Parameters:
name - The non-null name of the context to list.
Returns:
A non-null enumeration of the names and class names of the bindings (NameClassPair) in this context.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.list(Name)

listBindings

public NamingEnumeration listBindings(java.lang.String name)
                               throws NamingException
Enumerates the names and their bound objects in the context named relative to this initial context.
Specified by:
listBindings in interface Context
Parameters:
name - The non-null name of the context to list.
Returns:
A non-null enumeration of the names and their bound objects (Binding).
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.listBindings(Name)

listBindings

public NamingEnumeration listBindings(Name name)
                               throws NamingException
Enumerates the names and their bound objects in the context named relative to this initial context.
Specified by:
listBindings in interface Context
Parameters:
name - The non-null name of the context to list.
Returns:
A non-null enumeration of the names and their bound objects (Binding).
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.listBindings(Name)

destroySubcontext

public void destroySubcontext(java.lang.String name)
                       throws NamingException
Destroys the named context and removes it from the namespace.
Specified by:
destroySubcontext in interface Context
Parameters:
name - The non-null name of the context to be destroyed.
Throws:
NameNotFoundException - If intermediate names not bound.
NotContextException - If name is bound but does not name a context.
ContextNotEmptyException - If named context is not empty.
NamingException - If a naming exception was encountered.
See Also:
Context.destroySubcontext(Name)

destroySubcontext

public void destroySubcontext(Name name)
                       throws NamingException
Destroys the named context and removes it from the namespace.
Specified by:
destroySubcontext in interface Context
Parameters:
name - The non-null name of the context to be destroyed.
Throws:
NameNotFoundException - If intermediate names not bound.
NotContextException - If name is bound but does not name a context.
ContextNotEmptyException - If named context is not empty.
NamingException - If a naming exception was encountered.
See Also:
Context.destroySubcontext(Name)

createSubcontext

public Context createSubcontext(java.lang.String name)
                         throws NamingException
Creates and binds a new context.
Specified by:
createSubcontext in interface Context
Parameters:
name - The non-null name of the context to create. It cannot be empty.
Returns:
The non-null newly created context.
Throws:
NameAlreadyBoundException - If name is already bound.
InvalidAttributesException - If creation of the subcontext requires specification of mandatory attributes.
NamingException - if a naming exception was encountered.
See Also:
Context.createSubcontext(Name)

createSubcontext

public Context createSubcontext(Name name)
                         throws NamingException
Creates and binds a new context.
Specified by:
createSubcontext in interface Context
Parameters:
name - The non-null name of the context to create. It cannot be empty.
Returns:
The non-null newly created context.
Throws:
NameAlreadyBoundException - If name is already bound.
InvalidAttributesException - If creation of the subcontext requires specification of mandatory attributes.
NamingException - if a naming exception was encountered.
See Also:
Context.createSubcontext(Name)

lookupLink

public java.lang.Object lookupLink(java.lang.String name)
                            throws NamingException
Retrieve the object named relative to this initial context, following links except for the terminal atomic component of name.
Specified by:
lookupLink in interface Context
Parameters:
name - The non-null name to look up.
Returns:
The non-null object bound to the name by not following the terminal link (if any). If the object bound to name is not a link, return the object itself.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.lookupLink(Name)

lookupLink

public java.lang.Object lookupLink(Name name)
                            throws NamingException
Retrieve the object named relative to this initial context, following links except for the terminal atomic component of name.
Specified by:
lookupLink in interface Context
Parameters:
name - The non-null name to look up.
Returns:
The non-null object bound to the name by not following the terminal link (if any). If the object bound to name is not a link, return the object itself.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.lookupLink(Name)

getNameParser

public NameParser getNameParser(java.lang.String name)
                         throws NamingException
Retrieves the parser associated with the named context.
Specified by:
getNameParser in interface Context
Parameters:
name - The non-null name of the context from which to get the parser.
Returns:
A non-null name parser that will parse names into their atomic components.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.getNameParser(Name)

getNameParser

public NameParser getNameParser(Name name)
                         throws NamingException
Retrieves the parser associated with the named context.
Specified by:
getNameParser in interface Context
Parameters:
name - The non-null name of the context from which to get the parser.
Returns:
A non-null name parser that will parse names into their atomic components.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.getNameParser(Name)

composeName

public java.lang.String composeName(java.lang.String name,
                                    java.lang.String prefix)
                             throws NamingException
Composes the name of this context with a name relative to this context.
Specified by:
composeName in interface Context
Parameters:
name - A non-null name relative to this context.
prefix - The non-null name of this context relative to one of its ancestors.
Returns:
The non-null composition of prefix and name.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.composeName(Name, Name)

composeName

public Name composeName(Name name,
                        Name prefix)
                 throws NamingException
Composes the name of this context with a name relative to this context.
Specified by:
composeName in interface Context
Parameters:
name - A non-null name relative to this context.
prefix - The non-null name of this context relative to one of its ancestors.
Returns:
The non-null composition of prefix and name.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.composeName(Name, Name)

addToEnvironment

public java.lang.Object addToEnvironment(java.lang.String propName,
                                         java.lang.Object propVal)
                                  throws NamingException
Adds a new environment property to the environment of this context.
Specified by:
addToEnvironment in interface Context
Parameters:
propName - The non-null name of the environment property to add. If already exists in environment, overwrite and return old value.
propVal - The non-null value.
Returns:
The value that propName used to have in the environment; null if not there before.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.addToEnvironment(java.lang.String, java.lang.Object)

removeFromEnvironment

public java.lang.Object removeFromEnvironment(java.lang.String propName)
                                       throws NamingException
Removes an environment property from the environment of this context.
Specified by:
removeFromEnvironment in interface Context
Parameters:
propName - The non-null name of the environment property to remove.
Returns:
The value associated with propName. null if propName was not in the environment.
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.removeFromEnvironment(java.lang.String)

getEnvironment

public java.util.Hashtable getEnvironment()
                                   throws NamingException
Retrieves the environment in effect for the initial context.
Specified by:
getEnvironment in interface Context
Returns:
The non-null (possibly empty) environment for this context. Caller should not make changes to this object: their effect on the context is undefined. To change the environment of the context, use addToEnvironment() and removeFromEnvironment().
Throws:
NamingException - If a naming exception was encountered.
See Also:
Context.getEnvironment()

close

public void close()
           throws NamingException
Closes this initial context.

This method is idempotent. Invoking close() on an already closed context does not do anything. However, invoking any other method on a closed context results in undefined behavior.

Specified by:
close in interface Context
Throws:
NamingException - If a naming exception was encountered.

JNDI 1.1.1

For more information on JNDI, please see http://java.sun.com/products/jndi