| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
InitialContext iniCtx = new InitialContext();
Based on 265 examples
public class InitialContext extends 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.
When the initial context is constructed, its environment is initialized with properties defined in the environment parameter passed to the constructor, and in any application resource files. In addition, a small number of standard JNDI properties may be specified as system properties or as applet parameters (through the use of {@link Context#APPLET}). These special properties are listed in the field detail sections of the Context and LdapContext interface documentation.
JNDI determines each property's value by merging the values from the following two sources, in order:
The initial context implementation is determined at runtime. The default policy uses the environment property "{@link Context#INITIAL_CONTEXT_FACTORY java.naming.factory.initial}", which contains the class name of the initial context factory. An exception to this policy is made when resolving URL strings, as described below.
When a URL string (a String of the form scheme_id:rest_of_name) is passed as a name parameter to any method, a URL context factory for handling that scheme is located and used to resolve the URL. If no such factory is found, the initial context specified by "java.naming.factory.initial" is used. Similarly, when a CompositeName object whose first component is a URL string is passed as a name parameter to any method, a URL context factory is located and used to resolve the first name component. See {@link NamingManager#getURLContext NamingManager.getURLContext()} for a description of how URL context factories are located.
This default policy of locating the initial context and URL context factories may be overridden by calling 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 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 a different InitialContext instance need not synchronize. Threads that need to access a single InitialContext instance concurrently should synchronize amongst themselves and provide the necessary locking.
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 Hashtable |
myProps
The environment associated with this InitialContext. |
Constructor Summary | |
---|---|
Constructs an initial context. |
|
protected |
InitialContext(boolean lazy) Constructs an initial context with the option of not initializing it. |
InitialContext(Hashtable environment) Constructs an initial context using the supplied environment. |
Method Summary | |
---|---|
Object |
addToEnvironment(String propName, Object propVal) |
void |
|
void |
|
void |
close() |
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) |
Context |
createSubcontext(String name) |
void |
destroySubcontext(Name name) |
void |
destroySubcontext(String name) |
static Object |
A static method to retrieve the named object. |
static Object |
A static method to retrieve the named object. |
protected Context |
Retrieves the initial context by calling NamingManager.getInitialContext()
and cache it in defaultInitCtx.
|
Hashtable |
|
String |
|
NameParser |
getNameParser(Name name) |
NameParser |
getNameParser(String name) |
protected Context |
getURLOrDefaultInitCtx(Name name) Retrieves a context for resolving name .
|
protected Context |
getURLOrDefaultInitCtx(String name) Retrieves a context for resolving the string name name .
|
protected void |
Initializes the initial context using the supplied environment. |
NamingEnumeration |
|
NamingEnumeration |
|
NamingEnumeration |
listBindings(Name name) |
NamingEnumeration |
listBindings(String name) |
Object |
|
Object |
|
Object |
lookupLink(Name name) |
Object |
lookupLink(String name) |
void |
|
void |
|
Object |
removeFromEnvironment(String propName) |
void |
|
void |
|
void |
|
void |
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected Context defaultInitCtx
protected boolean gotDefault
defaultInitCtx
.
protected Hashtable myProps
Constructor Detail |
---|
public InitialContext() throws NamingException
NamingException
- if a naming exception is encounteredprotected InitialContext(boolean lazy) throws NamingException
lazy
- true means do not initialize the initial context; false
is equivalent to calling new InitialContext()NamingException
- if a naming exception is encounteredpublic InitialContext(Hashtable environment) throws NamingException
This constructor will not modify environment or save a reference to it, but may save a clone.
environment
- environment used to create the initial context.
Null indicates an empty environment.NamingException
- if a naming exception is encounteredMethod Detail |
---|
public Object addToEnvironment(String propName, Object propVal) throws NamingException
propName
propVal
NamingException
public void bind(Name name, Object obj) throws NamingException
name
obj
NamingException
public void bind(String name, Object obj) throws NamingException
name
obj
NamingException
public void close() throws NamingException
NamingException
public Name composeName(Name name, Name prefix) throws NamingException
name
prefix
NamingException
public String composeName(String name, String prefix) throws NamingException
name
prefix
NamingException
public Context createSubcontext(Name name) throws NamingException
name
NamingException
public Context createSubcontext(String name) throws NamingException
name
NamingException
public void destroySubcontext(Name name) throws NamingException
name
NamingException
public void destroySubcontext(String name) throws NamingException
name
NamingException
public static Object doLookup(Name name) throws NamingException
InitialContext ic = new InitialContext();
Object obj = ic.lookup();
If name is empty, returns a new instance of this context (which represents the same naming context as this context, but its environment may be modified independently and it may be accessed concurrently).
name
- the name of the object to look upNamingException
- if a naming exception is encounteredpublic static Object doLookup(String name) throws NamingException
name
- the name of the object to look upNamingException
- if a naming exception is encounteredprotected Context getDefaultInitCtx() throws NamingException
NamingManager.getInitialContext()
and cache it in defaultInitCtx.
Set gotDefault
so that we know we've tried this before.
NamingException
- If a naming exception was encountered.public Hashtable getEnvironment() throws NamingException
NamingException
public String getNameInNamespace() throws NamingException
NamingException
public NameParser getNameParser(Name name) throws NamingException
name
NamingException
public NameParser getNameParser(String name) throws NamingException
name
NamingException
protected Context getURLOrDefaultInitCtx(Name name) throws NamingException
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.
When providing implementations for the new methods in the subclass, use this newly defined method to get the initial context.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; }
public Object XXXMethod1(Name name, ...) { throws NamingException { return getURLOrDefaultInitXXXCtx(name).XXXMethod1(name, ...); }
name
- The non-null name for which to get the context.name
or the cached
initial context. The result cannot be null.NamingException
- In a naming exception is encountered.protected Context getURLOrDefaultInitCtx(String name) throws NamingException
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.
name
- The non-null name for which to get the context.name
or the cached
initial context. The result cannot be null.NamingException
- In a naming exception is encountered.protected void init(Hashtable environment) throws NamingException
This method will modify environment and save a reference to it. The caller may no longer modify it.
environment
- environment used to create the initial context.
Null indicates an empty environment.NamingException
- if a naming exception is encounteredpublic NamingEnumeration list(Name name) throws NamingException
name
NamingException
public NamingEnumeration list(String name) throws NamingException
name
NamingException
public NamingEnumeration listBindings(Name name) throws NamingException
name
NamingException
public NamingEnumeration listBindings(String name) throws NamingException
name
NamingException
public Object lookup(Name name) throws NamingException
name
NamingException
public Object lookup(String name) throws NamingException
name
NamingException
public Object lookupLink(Name name) throws NamingException
name
NamingException
public Object lookupLink(String name) throws NamingException
name
NamingException
public void rebind(Name name, Object obj) throws NamingException
name
obj
NamingException
public void rebind(String name, Object obj) throws NamingException
name
obj
NamingException
public Object removeFromEnvironment(String propName) throws NamingException
propName
NamingException
public void rename(Name oldName, Name newName) throws NamingException
oldName
newName
NamingException
public void rename(String oldName, String newName) throws NamingException
oldName
newName
NamingException
public void unbind(Name name) throws NamingException
name
NamingException
public void unbind(String name) throws NamingException
name
NamingException
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |