Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

AS7 5030 fix, support for LinkRef and misc Reference naming lookups #9

Closed
wants to merge 2 commits into from

3 participants

@emmartins

No description provided.

@n1hility
Owner

I think we decided not to do this? Right?

@emmartins
@dmlloyd
Owner

Merged, thanks.

@dmlloyd dmlloyd closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
48 src/main/java/org/jboss/naming/remote/client/RemoteContext.java
@@ -29,14 +29,21 @@
import javax.naming.Binding;
import javax.naming.CompositeName;
import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.LinkRef;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
+import javax.naming.Reference;
+import javax.naming.spi.ObjectFactory;
+
import org.jboss.logging.Logger;
+
import static org.jboss.naming.remote.client.ClientUtil.isEmpty;
import static org.jboss.naming.remote.client.ClientUtil.namingEnumeration;
+import static org.jboss.naming.remote.client.ClientUtil.namingException;
/**
*
@@ -76,9 +83,48 @@ public Object lookup(final Name name) throws NamingException {
if (isEmpty(name)) {
return new RemoteContext(prefix, namingStore, environment);
}
- return namingStore.lookup(getAbsoluteName(name));
+ final Name absoluteName = getAbsoluteName(name);
+ Object result = namingStore.lookup(absoluteName);
+ if (result instanceof LinkRef) {
+ result = resolveLink((LinkRef)result);
+ }
+ else if (result instanceof Reference) {
+ result = getObjectInstance((Reference)result, name, environment);
+ if (result instanceof LinkRef) {
+ result = resolveLink((LinkRef)result);
+ }
+ }
+ return result;
+ }
+
+ private Object getObjectInstance(final Reference reference, final Name name, final Hashtable<?, ?> environment) throws NamingException {
+ try {
+ final Class<?> factoryClass = Thread.currentThread().getContextClassLoader().loadClass(reference.getFactoryClassName());
+ ObjectFactory factory = ObjectFactory.class.cast(factoryClass.newInstance());
+ return factory.getObjectInstance(reference, name, this, environment);
+ } catch(NamingException e) {
+ throw e;
+ } catch(Throwable t) {
+ throw namingException("failed to get object instance from reference", t);
+ }
}
+ private Object resolveLink(LinkRef result) throws NamingException {
+ final Object linkResult;
+ try {
+ final LinkRef linkRef = (LinkRef) result;
+ final String referenceName = linkRef.getLinkName();
+ if (referenceName.startsWith("./")) {
+ linkResult = lookup(referenceName.substring(2));
+ } else {
+ linkResult = new InitialContext().lookup(referenceName);
+ }
+ } catch (Throwable t) {
+ throw namingException("failed to deref link",t);
+ }
+ return linkResult;
+ }
+
public Object lookup(final String name) throws NamingException {
return lookup(parse(name));
}
View
39 src/test/java/org/jboss/naming/remote/ClientConnectionTest.java
@@ -25,18 +25,24 @@
import java.net.SocketAddress;
import java.net.URI;
import java.util.HashMap;
+import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.naming.Binding;
+import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.LinkRef;
+import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
+import javax.naming.spi.ObjectFactory;
import org.jboss.ejb.client.ContextSelector;
import org.jboss.ejb.client.EJBClientContext;
@@ -153,6 +159,39 @@ public void testLookupNested() throws Exception {
}
@Test
+ public void testLookupReference() throws Exception {
+ final Name name = new CompositeName("test");
+ final Reference reference = new Reference(String.class.getName(), new StringRefAddr("blah", "test"),
+ TestObjectFactory.class.getName(), null);
+ try {
+ localContext.bind(name, reference);
+ final Object result = remoteContext.lookup(name);
+ assertEquals("test", result);
+ } finally {
+ localContext.unbind(name);
+ }
+ }
+
+ public static class TestObjectFactory implements ObjectFactory {
+ @Override
+ public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
+ return ((Reference) obj).get(0).getContent();
+ }
+ }
+
+ @Test
+ public void testLookupLinkRef() throws Exception {
+ try {
+ localContext.bind("test", "testValue");
+ localContext.bind("link", new LinkRef("./test"));
+ assertEquals("testValue", remoteContext.lookup("link"));
+ } finally {
+ localContext.unbind("test");
+ localContext.unbind("link");
+ }
+ }
+
+ @Test
public void testBind() throws Exception {
remoteContext.bind("test", "TestValue");
assertEquals("TestValue", localContext.lookup("test"));
Something went wrong with that request. Please try again.