use of javax.naming.LinkRef in project jetty.project by eclipse.
the class localContextRoot method lookup.
/**
*
*
* @see javax.naming.Context#lookup(javax.naming.Name)
*/
public Object lookup(Name name) throws NamingException {
synchronized (__root) {
if (__log.isDebugEnabled())
__log.debug("Looking up name=\"" + name + "\"");
Name cname = __root.toCanonicalName(name);
if ((cname == null) || (cname.size() == 0)) {
__log.debug("Null or empty name, returning copy of this context");
NamingContext ctx = new NamingContext(_env, null, null, __root.getNameParser(""));
ctx.setBindings(__root.getBindings());
return ctx;
}
if (cname.size() == 1) {
Binding binding = __root.getBinding(cname);
if (binding == null) {
NameNotFoundException nnfe = new NameNotFoundException();
nnfe.setRemainingName(cname);
throw nnfe;
}
Object o = binding.getObject();
//handle links by looking up the link
if (o instanceof LinkRef) {
//if link name starts with ./ it is relative to current context
String linkName = ((LinkRef) o).getLinkName();
if (linkName.startsWith("./"))
return lookup(linkName.substring(2));
else {
//link name is absolute
InitialContext ictx = new InitialContext();
return ictx.lookup(linkName);
}
} else if (o instanceof Reference) {
//deference the object
try {
return NamingManager.getObjectInstance(o, cname, __root, _env);
} catch (NamingException e) {
throw e;
} catch (final Exception e) {
throw new NamingException(e.getMessage()) {
{
initCause(e);
}
};
}
} else
return o;
}
//it is a multipart name, get the first subcontext
String firstComponent = cname.get(0);
Object ctx = null;
if (firstComponent.equals(""))
ctx = this;
else {
Binding binding = __root.getBinding(firstComponent);
if (binding == null) {
NameNotFoundException nnfe = new NameNotFoundException();
nnfe.setRemainingName(cname);
throw nnfe;
}
//as we have bound a reference to an object factory
//for the component specific contexts
//at "comp" we need to resolve the reference
ctx = binding.getObject();
if (ctx instanceof Reference) {
//deference the object
try {
ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
__log.warn("", e);
throw new NamingException(e.getMessage());
}
}
}
if (!(ctx instanceof Context))
throw new NotContextException();
return ((Context) ctx).lookup(cname.getSuffix(1));
}
}
use of javax.naming.LinkRef in project jetty.project by eclipse.
the class NamingEntry method bindToENC.
/**
* Add a <code>java:comp/env</code> binding for the object represented by this NamingEntry,
* but bind it as the name supplied
* @param localName the local name to bind
* @throws NamingException if unable to bind
*/
public void bindToENC(String localName) throws NamingException {
// TODO - check on the whole overriding/non-overriding thing
InitialContext ic = new InitialContext();
Context env = (Context) ic.lookup("java:comp/env");
__log.debug("Binding java:comp/env/" + localName + " to " + _objectNameString);
NamingUtil.bind(env, localName, new LinkRef(_objectNameString));
}
use of javax.naming.LinkRef in project jetty.project by eclipse.
the class Transaction method bindToENC.
/**
* Allow other bindings of UserTransaction.
*
* These should be in ADDITION to java:comp/UserTransaction
* @see NamingEntry#bindToENC(java.lang.String)
*/
public void bindToENC(String localName) throws NamingException {
InitialContext ic = new InitialContext();
Context env = (Context) ic.lookup("java:comp/env");
__log.debug("Binding java:comp/env" + getJndiName() + " to " + _objectNameString);
NamingUtil.bind(env, localName, new LinkRef(_objectNameString));
}
use of javax.naming.LinkRef in project geronimo-xbean by apache.
the class DefaultContext method lookup.
public Object lookup(String name) throws NamingException {
if (name.length() == 0) {
return this;
}
Object result = treeBindings.get(name);
if (result == null) {
result = bindings.get(name);
}
if (result == null) {
int pos = name.indexOf(':');
if (pos > 0) {
String scheme = name.substring(0, pos);
Context ctx = NamingManager.getURLContext(scheme, environment);
if (ctx == null) {
throw new NamingException("scheme " + scheme + " not recognized");
}
return ctx.lookup(name);
} else {
// Split out the first name of the path
// and look for it in the bindings map.
CompositeName path = new CompositeName(name);
if (path.size() == 0) {
return this;
} else {
String first = path.get(0);
Object obj = bindings.get(first);
if (obj == null) {
throw new NameNotFoundException(name);
} else if (obj instanceof Context && path.size() > 1) {
Context subContext = (Context) obj;
obj = subContext.lookup(path.getSuffix(1));
}
return obj;
}
}
}
if (result instanceof LinkRef) {
LinkRef ref = (LinkRef) result;
result = lookup(ref.getLinkName());
}
if (result instanceof Reference) {
try {
result = NamingManager.getObjectInstance(result, null, null, this.environment);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
}
}
if (result instanceof DefaultContext) {
String prefix = getNameInNamespace();
if (prefix.length() > 0) {
prefix = prefix + SEPARATOR;
}
result = new DefaultContext((DefaultContext) result, environment, prefix + name);
}
return result;
}
use of javax.naming.LinkRef in project geronimo-xbean by apache.
the class AbstractContext method lookup.
/**
* Finds the specified entry. Normally there is no need to override this method; instead you should
* simply implement the getDeepBindings(String) and getBindings(String) method.
*
* This method will follow links except for the final element which is always just returned without
* inspection. This means this method can be used to implement lookupLink.
*
* @param stringName the string version of the name; maybe null
* @param parsedName the parsed name; may be null
* @return the value bound to the name
* @throws NamingException if no value is bound to that name or if a problem occurs during the lookup
*/
protected Object lookup(String stringName, Name parsedName) throws NamingException {
if (stringName == null && parsedName == null) {
throw new IllegalArgumentException("Both stringName and parsedName are null");
}
if (stringName == null)
stringName = parsedName.toString();
// try to look up the name directly (this is the fastest path)
Object directLookup = getDeepBinding(stringName);
if (directLookup != null) {
return ContextUtil.resolve(directLookup, stringName, parsedName, this);
}
// if the parsed name has no parts, they are asking for the current context
if (parsedName == null)
parsedName = getNameParser().parse(stringName);
if (parsedName.isEmpty()) {
return this;
}
// we didn't find an entry, pop the first element off the parsed name and attempt to
// get a context from the bindings and delegate to that context
Object localValue;
String firstNameElement = parsedName.get(0);
if (firstNameElement.length() == 0) {
// the element is null... this is normally caused by looking up with a trailing '/' character
localValue = this;
} else {
localValue = getBinding(firstNameElement);
}
if (localValue != null) {
// if the name only had one part, we've looked up everything
if (parsedName.size() == 1) {
localValue = ContextUtil.resolve(localValue, stringName, parsedName, this);
return localValue;
}
// if we have a link ref, follow it
if (localValue instanceof LinkRef) {
LinkRef linkRef = (LinkRef) localValue;
localValue = lookup(linkRef.getLinkName());
}
// we have more to lookup so we better have a context object
if (!(localValue instanceof Context)) {
throw new NameNotFoundException(stringName);
}
// delegate to the sub-context
return ((Context) localValue).lookup(parsedName.getSuffix(1));
}
// if we didn't find an entry, it may be an absolute name
Object value = faultLookup(stringName, parsedName);
if (value != null) {
return value;
}
if (parsedName.size() > 1) {
throw new NotContextException(stringName);
} else {
throw new NameNotFoundException(stringName);
}
}
Aggregations