in xbean-naming/src/main/java/org/apache/xbean/naming/context/AbstractContext.java [227:285]
protected void addDeepBinding(Name name, Object value, boolean rebind, boolean createIntermediateContexts) throws NamingException {
if (name == null) throw new NullPointerException("name is null");
if (value == null) throw new NullPointerException("value is null for name: " + name);
if (name.isEmpty()) {
throw new InvalidNameException("Name is empty");
}
if (name.size() == 1) {
addBinding(name.get(0), value, rebind);
return;
}
if (!createIntermediateContexts) {
Context context = lookupFinalContext(name);
String lastSegment = name.get(name.size() - 1);
addBinding(context, lastSegment, value, rebind);
} else {
Context currentContext = this;
for (int i = 0; i < name.size(); i++) {
String part = name.get(i);
// empty path parts are not allowed
if (part.length() == 0) {
// this could be supported but it would be tricky
throw new InvalidNameException("Name part " + i + " is empty: " + name);
}
// Is this the last element in the name?
if (i == name.size() - 1) {
// we're at the end... (re)bind the value into the parent context
addBinding(currentContext, part, value, rebind);
// all done... this is redundant but makes the code more readable
break;
} else {
Object currentValue = getBinding(currentContext, part);
if (currentValue == null) {
// the next step in the tree is not present, so create everything down
// and add it to the current bindings
Context subcontext = createSubcontextTree(name.getPrefix(i).toString(), name.getSuffix(i), value);
addBinding(currentContext, part, subcontext, rebind);
// all done
break;
} else {
// the current value must be a nested subcontext
if (!(currentValue instanceof Context)) {
throw new NotContextException("Expected an instance of context to be bound at " +
part + " but found an instance of " + currentValue.getClass().getName());
}
currentContext = (Context) currentValue;
// now we recurse into the current context
}
}
}
}
}