下面列出了怎么用javax.naming.ContextNotEmptyException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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));
}
}
/**
* 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));
}
}
/**
* 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 testDestroySubcontext2() throws Exception {
DefaultInitialContext context = new DefaultInitialContext();
context.createSubcontext(new CompositeName("context"));
context.bind("context/name", 12);
assertThrows(ContextNotEmptyException.class, () -> context.destroySubcontext("context"));
}