下面列出了怎么用javax.naming.NotContextException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Retrieves the parser associated with the named context. In a
* federation of namespaces, different naming systems will parse names
* differently. This method allows an application to get a parser for
* parsing names into their atomic components using the naming convention
* of a particular naming system. Within any single naming system,
* NameParser objects returned by this method must be equal (using the
* equals() test).
*
* @param name the name of the context from which to get the parser
* @return a name parser that can parse compound names into their atomic
* components
* @exception NamingException if a naming exception is encountered
*/
@Override
public NameParser getNameParser(Name name)
throws NamingException {
while ((!name.isEmpty()) && (name.get(0).length() == 0))
name = name.getSuffix(1);
if (name.isEmpty())
return nameParser;
if (name.size() > 1) {
Object obj = bindings.get(name.get(0));
if (obj instanceof Context) {
return ((Context) obj).getNameParser(name.getSuffix(1));
} else {
throw new NotContextException
(sm.getString("namingContext.contextExpected"));
}
}
return nameParser;
}
/**
* Retrieves the parser associated with the named context. In a
* federation of namespaces, different naming systems will parse names
* differently. This method allows an application to get a parser for
* parsing names into their atomic components using the naming convention
* of a particular naming system. Within any single naming system,
* NameParser objects returned by this method must be equal (using the
* equals() test).
*
* @param name the name of the context from which to get the parser
* @return a name parser that can parse compound names into their atomic
* components
* @exception NamingException if a naming exception is encountered
*/
@Override
public NameParser getNameParser(Name name)
throws NamingException {
while ((!name.isEmpty()) && (name.get(0).length() == 0))
name = name.getSuffix(1);
if (name.isEmpty())
return nameParser;
if (name.size() > 1) {
Object obj = bindings.get(name.get(0));
if (obj instanceof Context) {
return ((Context) obj).getNameParser(name.getSuffix(1));
} else {
throw new NotContextException
(sm.getString("namingContext.contextExpected"));
}
}
return nameParser;
}
/**
* Destroys subcontext with name name. The subcontext must be empty otherwise
* ContextNotEmptyException is thrown. Once a context is destroyed, the
* instance should not be used.
*
* @param name subcontext to destroy
* @throws NoPermissionException if this context has been destroyed.
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system.
* @throws ContextNotEmptyException if Context name is not empty.
* @throws NameNotFoundException if subcontext with name name can not be
* found.
* @throws NotContextException if name is not bound to instance of
* ContextImpl.
*
*/
public void destroySubcontext(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String subContextName = parsedName.get(0);
Object boundObject = ctxMaps.get(subContextName);
if (boundObject == null) { throw new NameNotFoundException(LocalizedStrings.ContextImpl_NAME_0_NOT_FOUND_IN_THE_CONTEXT.toLocalizedString(subContextName)); }
if (!(boundObject instanceof ContextImpl)) { throw new NotContextException(); }
ContextImpl contextToDestroy = (ContextImpl) boundObject;
if (parsedName.size() == 1) {
// Check if the Context to be destroyed is empty. Can not destroy
// non-empty Context.
if (contextToDestroy.ctxMaps.size() == 0) {
ctxMaps.remove(subContextName);
contextToDestroy.destroyInternal();
}
else {
throw new ContextNotEmptyException(LocalizedStrings.ContextImpl_CAN_NOT_DESTROY_NONEMPTY_CONTEXT.toLocalizedString());
}
}
else {
// Let the subcontext destroy the context
((ContextImpl) boundObject).destroySubcontext(parsedName.getSuffix(1));
}
}
/**
* Removes name and its associated object from the context.
*
* @param name name to remove
* @throws NoPermissionException if this context has been destroyed.
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system
* @throws NameNotFoundException if intermediate context can not be found
* @throws NotContextException if name has more than one atomic name and
* intermediate context is not found.
* @throws NamingException if any other naming exception occurs
*
*/
public void unbind(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String nameToRemove = parsedName.get(0);
// scenerio unbind a
// remove a and its associated object
if (parsedName.size() == 1) {
ctxMaps.remove(nameToRemove);
}
else {
// scenerio unbind a/b or a/b/c
// remove b and its associated object
Object boundObject = ctxMaps.get(nameToRemove);
if (boundObject instanceof Context) {
// remove b and its associated object
((Context) boundObject).unbind(parsedName.getSuffix(1));
}
else {
// if the name is not found then throw exception
if (!ctxMaps.containsKey(nameToRemove)) { throw new NameNotFoundException(LocalizedStrings.ContextImpl_CAN_NOT_FIND_0.toLocalizedString(name)); }
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
}
}
}
/**
* Retrieves the parser associated with the named context. In a
* federation of namespaces, different naming systems will parse names
* differently. This method allows an application to get a parser for
* parsing names into their atomic components using the naming convention
* of a particular naming system. Within any single naming system,
* NameParser objects returned by this method must be equal (using the
* equals() test).
*
* @param name the name of the context from which to get the parser
* @return a name parser that can parse compound names into their atomic
* components
* @exception NamingException if a naming exception is encountered
*/
@Override
public NameParser getNameParser(Name name)
throws NamingException {
while ((!name.isEmpty()) && (name.get(0).length() == 0))
name = name.getSuffix(1);
if (name.isEmpty())
return nameParser;
if (name.size() > 1) {
Object obj = bindings.get(name.get(0));
if (obj instanceof Context) {
return ((Context) obj).getNameParser(name.getSuffix(1));
} else {
throw new NotContextException
(sm.getString("namingContext.contextExpected"));
}
}
return nameParser;
}
/**
* Destroys subcontext with name name. The subcontext must be empty otherwise
* ContextNotEmptyException is thrown. Once a context is destroyed, the
* instance should not be used.
*
* @param name subcontext to destroy
* @throws NoPermissionException if this context has been destroyed.
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system.
* @throws ContextNotEmptyException if Context name is not empty.
* @throws NameNotFoundException if subcontext with name name can not be
* found.
* @throws NotContextException if name is not bound to instance of
* ContextImpl.
*
*/
public void destroySubcontext(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String subContextName = parsedName.get(0);
Object boundObject = ctxMaps.get(subContextName);
if (boundObject == null) { throw new NameNotFoundException(LocalizedStrings.ContextImpl_NAME_0_NOT_FOUND_IN_THE_CONTEXT.toLocalizedString(subContextName)); }
if (!(boundObject instanceof ContextImpl)) { throw new NotContextException(); }
ContextImpl contextToDestroy = (ContextImpl) boundObject;
if (parsedName.size() == 1) {
// Check if the Context to be destroyed is empty. Can not destroy
// non-empty Context.
if (contextToDestroy.ctxMaps.size() == 0) {
ctxMaps.remove(subContextName);
contextToDestroy.destroyInternal();
}
else {
throw new ContextNotEmptyException(LocalizedStrings.ContextImpl_CAN_NOT_DESTROY_NONEMPTY_CONTEXT.toLocalizedString());
}
}
else {
// Let the subcontext destroy the context
((ContextImpl) boundObject).destroySubcontext(parsedName.getSuffix(1));
}
}
/**
* Removes name and its associated object from the context.
*
* @param name name to remove
* @throws NoPermissionException if this context has been destroyed.
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system
* @throws NameNotFoundException if intermediate context can not be found
* @throws NotContextException if name has more than one atomic name and
* intermediate context is not found.
* @throws NamingException if any other naming exception occurs
*
*/
public void unbind(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String nameToRemove = parsedName.get(0);
// scenerio unbind a
// remove a and its associated object
if (parsedName.size() == 1) {
ctxMaps.remove(nameToRemove);
}
else {
// scenerio unbind a/b or a/b/c
// remove b and its associated object
Object boundObject = ctxMaps.get(nameToRemove);
if (boundObject instanceof Context) {
// remove b and its associated object
((Context) boundObject).unbind(parsedName.getSuffix(1));
}
else {
// if the name is not found then throw exception
if (!ctxMaps.containsKey(nameToRemove)) { throw new NameNotFoundException(LocalizedStrings.ContextImpl_CAN_NOT_FIND_0.toLocalizedString(name)); }
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
}
}
}
@Override
public void bind(Name name, Object obj) throws NamingException {
if (name.isEmpty()) {
throw new InvalidNameException("Cannot bind empty name");
}
Name nm = getMyComponents(name);
String atom = nm.get(0);
Object inter = iBindings.get(atom);
if (nm.size() == 1) {
if (inter != null)
throw new NameAlreadyBoundException("Use rebind to override");
obj = NamingManager.getStateToBind(obj, new CompositeName().add(atom), this, iEnv);
iBindings.put(atom, obj);
} else {
if (!(inter instanceof Context))
throw new NotContextException(atom + " does not name a context");
((Context) inter).bind(nm.getSuffix(1), obj);
}
}
@Override
public void rebind(Name name, Object obj) throws NamingException {
if (name.isEmpty())
throw new InvalidNameException("Cannot bind empty name");
Name nm = getMyComponents(name);
String atom = nm.get(0);
if (nm.size() == 1) {
obj = NamingManager.getStateToBind(obj, new CompositeName().add(atom), this, iEnv);
iBindings.put(atom, obj);
} else {
Object inter = iBindings.get(atom);
if (!(inter instanceof Context))
throw new NotContextException(atom + " does not name a context");
((Context) inter).rebind(nm.getSuffix(1), obj);
}
}
@Override
public void unbind(Name name) throws NamingException {
if (name.isEmpty())
throw new InvalidNameException("Cannot unbind empty name");
Name nm = getMyComponents(name);
String atom = nm.get(0);
if (nm.size() == 1) {
iBindings.remove(atom);
} else {
Object inter = iBindings.get(atom);
if (!(inter instanceof Context))
throw new NotContextException(atom + " does not name a context");
((Context) inter).unbind(nm.getSuffix(1));
}
}
@Override
public Context createSubcontext(Name name) throws NamingException {
if (name.isEmpty())
throw new InvalidNameException("Cannot bind empty name");
Name nm = getMyComponents(name);
String atom = nm.get(0);
Object inter = iBindings.get(atom);
if (nm.size() == 1) {
if (inter != null)
throw new NameAlreadyBoundException("Use rebind to override");
Context child = createCtx(this, atom, iEnv);
iBindings.put(atom, child);
return child;
} else {
if (!(inter instanceof Context))
throw new NotContextException(atom + " does not name a context");
return ((Context) inter).createSubcontext(nm.getSuffix(1));
}
}
public NamingEnumeration list(String name) throws NamingException {
if (name.length() > 0) {
throw new NotContextException("Subcontexts not supported");
}
return new NamingEnumeration() {
public void close() {}
public boolean hasMore() {
return false;
}
public Object next() {
throw new NoSuchElementException();
}
public boolean hasMoreElements() {
return hasMore();
}
public Object nextElement() {
return next();
}
};
}
public NamingEnumeration listBindings(String name) throws NamingException {
if (name.length() > 0) {
throw new NotContextException("Subcontexts not supported");
}
return new NamingEnumeration() {
public void close() {}
public boolean hasMore() {
return false;
}
public Object next() {
throw new NoSuchElementException();
}
public boolean hasMoreElements() {
return hasMore();
}
public Object nextElement() {
return next();
}
};
}
/**
* Destroys the named context and removes it from the namespace. Any
* attributes associated with the name are also removed. Intermediate
* contexts are not destroyed.
* <p>
* 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.
*
* @param name the name of the context to be destroyed; may not be empty
* @exception NameNotFoundException if an intermediate context does not
* exist
* @exception NotContextException if the name is bound but does not name
* a context, or does not name a context of the appropriate type
*/
@Override
public void destroySubcontext(Name name) throws NamingException {
if (!checkWritable()) {
return;
}
while ((!name.isEmpty()) && (name.get(0).length() == 0))
name = name.getSuffix(1);
if (name.isEmpty())
throw new NamingException
(sm.getString("namingContext.invalidName"));
NamingEntry entry = bindings.get(name.get(0));
if (entry == null) {
throw new NameNotFoundException
(sm.getString("namingContext.nameNotBound", name, name.get(0)));
}
if (name.size() > 1) {
if (entry.type == NamingEntry.CONTEXT) {
((Context) entry.value).destroySubcontext(name.getSuffix(1));
} else {
throw new NamingException
(sm.getString("namingContext.contextExpected"));
}
} else {
if (entry.type == NamingEntry.CONTEXT) {
((Context) entry.value).close();
bindings.remove(name.get(0));
} else {
throw new NotContextException
(sm.getString("namingContext.contextExpected"));
}
}
}
/**
* Destroy the sub context.
*
* @param name the name.
* @throws NamingException when a naming error occurs.
*/
@Override
public void destroySubcontext(String name) throws NamingException {
if (name.contains("/")) {
String[] names = name.split("/");
DefaultInitialContext contextMap = this;
try {
for (int i = 0; i < names.length - 1; i++) {
contextMap = (DefaultInitialContext) contextMap.lookup(names[i]);
}
contextMap.destroySubcontext(names[names.length - 1]);
} catch (NameNotFoundException nnfe) {
throw nnfe;
}
} else if (bindings.containsKey(name)) {
if (bindings.get(name) instanceof DefaultInitialContext) {
DefaultInitialContext subContext = (DefaultInitialContext) bindings.get(name);
if (subContext.bindings.isEmpty()) {
bindings.remove(name);
} else {
throw new ContextNotEmptyException("Context not empty: " + name);
}
} else {
throw new NotContextException("Not a context: " + name);
}
} else {
throw new NameNotFoundException("Unable to find name: " + name);
}
}
/**
* Test destroySubcontext method.
*
* @throws Exception when an error occurs.
*/
@Test
public void testDestroySubcontext3() throws Exception {
DefaultInitialContext context = new DefaultInitialContext();
context.bind("name", 12);
assertThrows(NotContextException.class, () -> context.destroySubcontext("name"));
}
/**
* Destroys the named context and removes it from the namespace. Any
* attributes associated with the name are also removed. Intermediate
* contexts are not destroyed.
* <p>
* 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.
*
* @param name the name of the context to be destroyed; may not be empty
* @exception NameNotFoundException if an intermediate context does not
* exist
* @exception NotContextException if the name is bound but does not name
* a context, or does not name a context of the appropriate type
*/
@Override
public void destroySubcontext(Name name) throws NamingException {
if (!checkWritable()) {
return;
}
while ((!name.isEmpty()) && (name.get(0).length() == 0))
name = name.getSuffix(1);
if (name.isEmpty())
throw new NamingException
(sm.getString("namingContext.invalidName"));
NamingEntry entry = bindings.get(name.get(0));
if (entry == null) {
throw new NameNotFoundException
(sm.getString("namingContext.nameNotBound", name, name.get(0)));
}
if (name.size() > 1) {
if (entry.type == NamingEntry.CONTEXT) {
((Context) entry.value).destroySubcontext(name.getSuffix(1));
} else {
throw new NamingException
(sm.getString("namingContext.contextExpected"));
}
} else {
if (entry.type == NamingEntry.CONTEXT) {
((Context) entry.value).close();
bindings.remove(name.get(0));
} else {
throw new NotContextException
(sm.getString("namingContext.contextExpected"));
}
}
}
/**
* Creates subcontext with name, relative to this Context.
*
* @param name subcontext name.
* @return new subcontext named name relative to this context.
* @throws NoPermissionException if this context has been destroyed.
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system.
* @throws NameAlreadyBoundException if name is already bound in this Context
* @throws NotContextException if any intermediate name from name is not bound
* to instance of javax.naming.Context.
*
*/
public Context createSubcontext(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String subContextName = parsedName.get(0);
Object boundObject = ctxMaps.get(parsedName.get(0));
if (parsedName.size() == 1) {
// Check if name is already in use
if (boundObject == null) {
Context subContext = new ContextImpl(this, subContextName);
ctxMaps.put(subContextName, subContext);
return subContext;
}
else {
throw new NameAlreadyBoundException(LocalizedStrings.ContextImpl_NAME_0_IS_ALREADY_BOUND.toLocalizedString(subContextName));
}
}
else {
if (boundObject instanceof Context) {
// Let the subcontext create new subcontext
// lets consider a scenerio a/b/c
// case a/b exists : c will be created
// case a exists : b/c will not be created
// an exception will be thrown in that case.
return ((Context) boundObject)
.createSubcontext(parsedName.getSuffix(1));
}
else {
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
}
}
}
/**
* Lists all bindings for Context with name name. If name is empty then this
* Context is assumed.
*
* @param name name of Context, relative to this Context
* @return NamingEnumeration of all name-object pairs. Each element from the
* enumeration is instance of Binding.
* @throws NoPermissionException if this context has been destroyed
* @throws InvalidNameException if name is CompositeName that spans more than
* one naming system
* @throws NameNotFoundException if name can not be found
* @throws NotContextException component of name is not bound to instance of
* ContextImpl, when name is not an atomic name
* @throws NamingException if any other naming error occurs
*
*/
public NamingEnumeration listBindings(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0) {
Vector bindings = new Vector();
Iterator iterat = ctxMaps.keySet().iterator();
while (iterat.hasNext()) {
String bindingName = (String) iterat.next();
bindings.addElement(new Binding(bindingName, ctxMaps.get(bindingName)));
}
return new NamingEnumerationImpl(bindings);
}
else {
Object subContext = ctxMaps.get(parsedName.get(0));
if (subContext instanceof Context) {
Name nextLayer = nameParser.parse("");
// getSuffix(1) only apply to name with size() > 1
if (parsedName.size() > 1) {
nextLayer = parsedName.getSuffix(1);
}
return ((Context) subContext).list(nextLayer);
}
if (subContext == null && !ctxMaps.containsKey(parsedName.get(0))) {
throw new NameNotFoundException(LocalizedStrings.ContextImpl_NAME_0_NOT_FOUND.toLocalizedString(name));
}
else {
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(subContext));
}
}
}
/**
* Rebinds object obj to name name . If there is existing binding it will be
* overwritten.
*
* @param name name of the object to rebind.
* @param obj object to add. Can be null.
* @throws NoPermissionException if this context has been destroyed
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system
* @throws NotContextException if name has more than one atomic name and
* intermediate context is not found
* @throws NamingException if any other naming error occurs
*
*/
public void rebind(Name name, Object obj) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String nameToBind = parsedName.get(0);
if (parsedName.size() == 1) {
ctxMaps.put(nameToBind, obj);
}
else {
Object boundObject = ctxMaps.get(nameToBind);
if (boundObject instanceof Context) {
/*
* Let the subcontext bind the object.
*/
((Context) boundObject).bind(parsedName.getSuffix(1), obj);
}
else {
if (boundObject == null) {
// Create new subcontext and let it do the binding
Context sub = createSubcontext(nameToBind);
sub.bind(parsedName.getSuffix(1), obj);
}
else {
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
}
}
}
}
/**
* Destroys the named context and removes it from the namespace. Any
* attributes associated with the name are also removed. Intermediate
* contexts are not destroyed.
* <p>
* 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.
*
* @param name the name of the context to be destroyed; may not be empty
* @exception NameNotFoundException if an intermediate context does not
* exist
* @exception NotContextException if the name is bound but does not name
* a context, or does not name a context of the appropriate type
*/
@Override
public void destroySubcontext(Name name) throws NamingException {
if (!checkWritable()) {
return;
}
while ((!name.isEmpty()) && (name.get(0).length() == 0))
name = name.getSuffix(1);
if (name.isEmpty())
throw new NamingException
(sm.getString("namingContext.invalidName"));
NamingEntry entry = bindings.get(name.get(0));
if (entry == null) {
throw new NameNotFoundException
(sm.getString("namingContext.nameNotBound", name, name.get(0)));
}
if (name.size() > 1) {
if (entry.type == NamingEntry.CONTEXT) {
((Context) entry.value).destroySubcontext(name.getSuffix(1));
} else {
throw new NamingException
(sm.getString("namingContext.contextExpected"));
}
} else {
if (entry.type == NamingEntry.CONTEXT) {
((Context) entry.value).close();
bindings.remove(name.get(0));
} else {
throw new NotContextException
(sm.getString("namingContext.contextExpected"));
}
}
}
/**
* Creates subcontext with name, relative to this Context.
*
* @param name subcontext name.
* @return new subcontext named name relative to this context.
* @throws NoPermissionException if this context has been destroyed.
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system.
* @throws NameAlreadyBoundException if name is already bound in this Context
* @throws NotContextException if any intermediate name from name is not bound
* to instance of javax.naming.Context.
*
*/
public Context createSubcontext(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String subContextName = parsedName.get(0);
Object boundObject = ctxMaps.get(parsedName.get(0));
if (parsedName.size() == 1) {
// Check if name is already in use
if (boundObject == null) {
Context subContext = new ContextImpl(this, subContextName);
ctxMaps.put(subContextName, subContext);
return subContext;
}
else {
throw new NameAlreadyBoundException(LocalizedStrings.ContextImpl_NAME_0_IS_ALREADY_BOUND.toLocalizedString(subContextName));
}
}
else {
if (boundObject instanceof Context) {
// Let the subcontext create new subcontext
// lets consider a scenerio a/b/c
// case a/b exists : c will be created
// case a exists : b/c will not be created
// an exception will be thrown in that case.
return ((Context) boundObject)
.createSubcontext(parsedName.getSuffix(1));
}
else {
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
}
}
}
/**
* Lists all bindings for Context with name name. If name is empty then this
* Context is assumed.
*
* @param name name of Context, relative to this Context
* @return NamingEnumeration of all name-object pairs. Each element from the
* enumeration is instance of Binding.
* @throws NoPermissionException if this context has been destroyed
* @throws InvalidNameException if name is CompositeName that spans more than
* one naming system
* @throws NameNotFoundException if name can not be found
* @throws NotContextException component of name is not bound to instance of
* ContextImpl, when name is not an atomic name
* @throws NamingException if any other naming error occurs
*
*/
public NamingEnumeration listBindings(Name name) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0) {
Vector bindings = new Vector();
Iterator iterat = ctxMaps.keySet().iterator();
while (iterat.hasNext()) {
String bindingName = (String) iterat.next();
bindings.addElement(new Binding(bindingName, ctxMaps.get(bindingName)));
}
return new NamingEnumerationImpl(bindings);
}
else {
Object subContext = ctxMaps.get(parsedName.get(0));
if (subContext instanceof Context) {
Name nextLayer = nameParser.parse("");
// getSuffix(1) only apply to name with size() > 1
if (parsedName.size() > 1) {
nextLayer = parsedName.getSuffix(1);
}
return ((Context) subContext).list(nextLayer);
}
if (subContext == null && !ctxMaps.containsKey(parsedName.get(0))) {
throw new NameNotFoundException(LocalizedStrings.ContextImpl_NAME_0_NOT_FOUND.toLocalizedString(name));
}
else {
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(subContext));
}
}
}
/**
* Rebinds object obj to name name . If there is existing binding it will be
* overwritten.
*
* @param name name of the object to rebind.
* @param obj object to add. Can be null.
* @throws NoPermissionException if this context has been destroyed
* @throws InvalidNameException if name is empty or is CompositeName that
* spans more than one naming system
* @throws NotContextException if name has more than one atomic name and
* intermediate context is not found
* @throws NamingException if any other naming error occurs
*
*/
public void rebind(Name name, Object obj) throws NamingException {
checkIsDestroyed();
Name parsedName = getParsedName(name);
if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString()); }
String nameToBind = parsedName.get(0);
if (parsedName.size() == 1) {
ctxMaps.put(nameToBind, obj);
}
else {
Object boundObject = ctxMaps.get(nameToBind);
if (boundObject instanceof Context) {
/*
* Let the subcontext bind the object.
*/
((Context) boundObject).bind(parsedName.getSuffix(1), obj);
}
else {
if (boundObject == null) {
// Create new subcontext and let it do the binding
Context sub = createSubcontext(nameToBind);
sub.bind(parsedName.getSuffix(1), obj);
}
else {
throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
}
}
}
}
@Override
public Object lookup(Name name) throws NamingException {
if (name.isEmpty())
return cloneCtx();
Name nm = getMyComponents(name);
String atom = nm.get(0);
Object inter = iBindings.get(atom);
if (nm.size() == 1) {
if (inter == null)
throw new NameNotFoundException(name + " not found");
try {
return NamingManager.getObjectInstance(inter, new CompositeName().add(atom), this, iEnv);
} catch (Exception e) {
NamingException ne = new NamingException("getObjectInstance failed");
ne.setRootCause(e);
throw ne;
}
} else {
if (!(inter instanceof Context))
throw new NotContextException(atom + " does not name a context");
return ((Context) inter).lookup(nm.getSuffix(1));
}
}
@Override
public void rename(Name oldname, Name newname) throws NamingException {
if (oldname.isEmpty() || newname.isEmpty())
throw new InvalidNameException("Cannot rename empty name");
Name oldnm = getMyComponents(oldname);
Name newnm = getMyComponents(newname);
if (oldnm.size() != newnm.size())
throw new OperationNotSupportedException("Do not support rename across different contexts");
String oldatom = oldnm.get(0);
String newatom = newnm.get(0);
if (oldnm.size() == 1) {
if (iBindings.get(newatom) != null)
throw new NameAlreadyBoundException(newname.toString() + " is already bound");
Object oldBinding = iBindings.remove(oldatom);
if (oldBinding == null)
throw new NameNotFoundException(oldname.toString() + " not bound");
iBindings.put(newatom, oldBinding);
} else {
if (!oldatom.equals(newatom))
throw new OperationNotSupportedException("Do not support rename across different contexts");
Object inter = iBindings.get(oldatom);
if (!(inter instanceof Context))
throw new NotContextException(oldatom + " does not name a context");
((Context) inter).rename(oldnm.getSuffix(1), newnm.getSuffix(1));
}
}
@Override
public NamingEnumeration list(Name name) throws NamingException {
if (name.isEmpty())
return new ListOfNames(iBindings.keys());
Object target = lookup(name);
if (target instanceof Context)
return ((Context) target).list("");
throw new NotContextException(name + " cannot be listed");
}
@Override
public NamingEnumeration listBindings(Name name) throws NamingException {
if (name.isEmpty())
return new ListOfBindings(iBindings.keys());
Object target = lookup(name);
if (target instanceof Context)
return ((Context) target).listBindings("");
throw new NotContextException(name + " cannot be listed");
}
@Override
public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
Object o = lookup(name);
if (o == this) {
return new ListEnumeration();
} else if (o instanceof Context) {
return ((Context) o).list("");
} else {
throw new NotContextException();
}
}
@Override
public NamingEnumeration<Binding> listBindings(String name) throws NamingException {
Object o = lookup(name);
if (o == this) {
return new ListBindingEnumeration();
} else if (o instanceof Context) {
return ((Context) o).listBindings("");
} else {
throw new NotContextException();
}
}