use of javax.naming.LinkRef in project geronimo-xbean by apache.
the class AbstractContext method lookup.
public Object lookup(Name name) throws NamingException {
if (name == null)
throw new NullPointerException("name is null");
Object value = lookup(null, name);
// if we got a link back we need to resolve it
if (value instanceof LinkRef) {
LinkRef linkRef = (LinkRef) value;
value = lookup(linkRef.getLinkName());
}
return value;
}
use of javax.naming.LinkRef in project tomcat by apache.
the class NamingContext method lookup.
/**
* Retrieves the named object.
*
* @param name the name of the object to look up
* @param resolveLinks If true, the links will be resolved
* @return the object bound to name
* @exception NamingException if a naming exception is encountered
*/
protected Object lookup(Name name, boolean resolveLinks) throws NamingException {
// Removing empty parts
while ((!name.isEmpty()) && (name.get(0).length() == 0)) {
name = name.getSuffix(1);
}
if (name.isEmpty()) {
// If name is empty, a newly allocated naming context is returned
return new NamingContext(env, this.name, bindings);
}
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) {
// number of subcontexts.
if (entry.type != NamingEntry.CONTEXT) {
throw new NamingException(sm.getString("namingContext.contextExpected"));
}
return ((Context) entry.value).lookup(name.getSuffix(1));
} else {
if ((resolveLinks) && (entry.type == NamingEntry.LINK_REF)) {
String link = ((LinkRef) entry.value).getLinkName();
if (link.startsWith(".")) {
// Link relative to this context
return lookup(link.substring(1));
} else {
return new InitialContext(env).lookup(link);
}
} else if (entry.type == NamingEntry.REFERENCE) {
try {
Object obj = null;
if (!GRAAL) {
obj = NamingManager.getObjectInstance(entry.value, name, this, env);
} else {
// NamingManager.getObjectInstance would simply return the reference here
// Use the configured object factory to resolve it directly if possible
// Note: This may need manual constructor reflection configuration
Reference reference = (Reference) entry.value;
Class<?> factoryClass = getClass().getClassLoader().loadClass(reference.getFactoryClassName());
ObjectFactory factory = (ObjectFactory) factoryClass.getDeclaredConstructor().newInstance();
obj = factory.getObjectInstance(entry.value, name, this, env);
}
if (entry.value instanceof ResourceRef) {
boolean singleton = Boolean.parseBoolean((String) ((ResourceRef) entry.value).get("singleton").getContent());
if (singleton) {
entry.type = NamingEntry.ENTRY;
entry.value = obj;
}
}
if (obj == null) {
throw new NamingException(sm.getString("namingContext.failResolvingReference"));
}
return obj;
} catch (NamingException e) {
throw e;
} catch (Exception e) {
String msg = sm.getString("namingContext.failResolvingReference");
log.warn(msg, e);
NamingException ne = new NamingException(msg);
ne.initCause(e);
throw ne;
}
} else {
return entry.value;
}
}
}
use of javax.naming.LinkRef in project wildfly by wildfly.
the class NamingContext method lookupLink.
/**
* {@inheritDoc}
*/
public Object lookupLink(Name name) throws NamingException {
check(name, JndiPermission.ACTION_LOOKUP);
if (name.isEmpty()) {
return lookup(name);
}
try {
final Name absoluteName = getAbsoluteName(name);
Object link = namingStore.lookup(absoluteName);
if (!(link instanceof LinkRef) && link instanceof Reference) {
link = getObjectInstance(link, name, null);
}
return link;
} catch (Exception e) {
throw namingException(NamingLogger.ROOT_LOGGER.cannotLookupLink(), e, name);
}
}
use of javax.naming.LinkRef in project wildfly by wildfly.
the class NamingContext method lookup.
public Object lookup(final Name name, boolean dereference) throws NamingException {
check(name, JndiPermission.ACTION_LOOKUP);
if (isEmpty(name)) {
return new NamingContext(prefix, namingStore, environment);
}
final Name absoluteName = getAbsoluteName(name);
Object result;
try {
result = namingStore.lookup(absoluteName, dereference);
} catch (CannotProceedException cpe) {
final Context continuationContext = NamingManager.getContinuationContext(cpe);
if (continuationContext instanceof NamingContext) {
result = ((NamingContext) continuationContext).lookup(cpe.getRemainingName(), dereference);
} else {
result = continuationContext.lookup(cpe.getRemainingName());
}
}
if (result instanceof ResolveResult) {
final ResolveResult resolveResult = (ResolveResult) result;
final Object resolvedObject = resolveResult.getResolvedObj();
Object context;
if (resolvedObject instanceof Context) {
context = resolvedObject;
} else if (resolvedObject instanceof LinkRef) {
context = resolveLink(resolvedObject, dereference);
} else {
context = getObjectInstance(resolvedObject, absoluteName, environment);
}
if (!(context instanceof Context)) {
throw notAContextException(absoluteName.getPrefix(absoluteName.size() - resolveResult.getRemainingName().size()));
}
final Context namingContext = (Context) context;
if (namingContext instanceof NamingContext) {
return ((NamingContext) namingContext).lookup(resolveResult.getRemainingName(), dereference);
} else {
return namingContext.lookup(resolveResult.getRemainingName());
}
} else if (result instanceof LinkRef) {
result = resolveLink(result, dereference);
} else if (result instanceof Reference) {
result = getObjectInstance(result, absoluteName, environment);
if (result instanceof LinkRef) {
result = resolveLink(result, dereference);
}
}
return result;
}
use of javax.naming.LinkRef in project wildfly by wildfly.
the class NamingContextTestCase method testLookupLink.
@Test
public void testLookupLink() throws Exception {
final Name name = new CompositeName("test");
namingStore.bind(name, "testValue", String.class);
final Name linkName = new CompositeName("link");
namingStore.bind(linkName, new LinkRef("./test"));
Object result = namingContext.lookup(linkName);
assertEquals("testValue", result);
// the same with security permissions
result = testActionPermission(JndiPermission.ACTION_LOOKUP, Arrays.asList(new JndiPermission("test", "lookup")), namingContext, "link");
assertEquals("testValue", result);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, InitialContextFactory.class.getName());
namingStore.rebind(linkName, new LinkRef(name));
result = namingContext.lookup(linkName);
assertEquals("testValue", result);
// the same with security permissions
result = testActionPermission(JndiPermission.ACTION_LOOKUP, Arrays.asList(new JndiPermission("test", "lookup")), namingContext, "link");
assertEquals("testValue", result);
}
Aggregations