* Lookup an object in the context. Returns a copy of this context if the
* name is empty, or the specified resource (if we have it). If the name is
* unknown, throws a NameNotFoundException.
*/
public Object lookup(Name name) throws NamingException {
Name searchName = validateName(name);
// If null, it means we don't know how to handle this -> throw to the
// parent
if (searchName == null)
return this.parent.lookup(name);
// If empty name, return a copy of this Context
else if (searchName.isEmpty())
return new WinstoneContext(this.environment, this.parent,
this.myAbsoluteName, this.contextLock, this.bindings);
String thisName = searchName.get(0);
synchronized (this.contextLock) {
Object thisValue = bindings.get(thisName);
// If the name points to something in this level, try to find it,
// and give
// an error if not available
if (searchName.size() == 1) {
if (thisValue == null)
throw new NameNotFoundException(ContainerJNDIManager.JNDI_RESOURCES.getString(
"WinstoneContext.NameNotFound", name.toString()));
try {
return NamingManager.getObjectInstance(thisValue,
new CompositeName().add(thisName), this,
this.environment);
} catch (Exception e) {
NamingException ne = new NamingException(ContainerJNDIManager.JNDI_RESOURCES
.getString("WinstoneContext.FailedToGetInstance"));
ne.setRootCause(e);
throw ne;
}
}
else if (thisValue == null)
throw new NameNotFoundException(ContainerJNDIManager.JNDI_RESOURCES.getString(
"WinstoneContext.NameNotFound", thisName.toString()));
// If it's not in this level and what we found is not a context,
// complain
else if (!(thisValue instanceof Context))
throw new NotContextException(ContainerJNDIManager.JNDI_RESOURCES.getString(
"WinstoneContext.NotContext", new String[] {
thisName.toString(),
thisValue.getClass().getName() }));
// Open the context, perform a lookup, then close the context we
// opened
else
try {
return ((Context) thisValue)
.lookup(searchName.getSuffix(1));
} finally {
((Context) thisValue).close();
}
}
}