From 90588ca47c0a3fa9e4f46d506a6e36fbbda5aeaf Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Fri, 30 Jul 2010 18:44:22 +0000 Subject: [PATCH] --- ...xyFactory.java => LookupProxyFactory.java} | 55 +++++++++++-------- .../org/wamblee/test/jndi/package-info.java | 2 - 2 files changed, 32 insertions(+), 25 deletions(-) rename test/enterprise/src/main/java/org/wamblee/test/jndi/{JndiProxyFactory.java => LookupProxyFactory.java} (72%) diff --git a/test/enterprise/src/main/java/org/wamblee/test/jndi/JndiProxyFactory.java b/test/enterprise/src/main/java/org/wamblee/test/jndi/LookupProxyFactory.java similarity index 72% rename from test/enterprise/src/main/java/org/wamblee/test/jndi/JndiProxyFactory.java rename to test/enterprise/src/main/java/org/wamblee/test/jndi/LookupProxyFactory.java index d326fa02..0c53d0cf 100644 --- a/test/enterprise/src/main/java/org/wamblee/test/jndi/JndiProxyFactory.java +++ b/test/enterprise/src/main/java/org/wamblee/test/jndi/LookupProxyFactory.java @@ -25,28 +25,40 @@ import javax.naming.NamingException; /** * Proxy factory that can provide contextual references to objects retrieved - * from JNDI. - * - * NOTE: This class is probably better suited as a production class, not a test support class. - * It needs to find a new home in a Java EE production utilities library. + * through a lookup mechanism. * * @param T * Interface to proxy. * @author Erik Brakkee * */ -public class JndiProxyFactory { +public class LookupProxyFactory { + + /** + * Interface to lookup the object to delegate to. + * + * @author Erik Brakkee + */ + public static interface Lookup { + /** + * Looks up the object. + * @return Object (non-null) + * @throws Any exception in case the object cannot be found. + */ + Object lookup() throws Exception; + } + /** * Exception thrown in case an object cannot be retrieved from JNDI. * * @author Erik Brakkee */ - public static class JndiWiringException extends RuntimeException { - public JndiWiringException(String aMsg, Throwable aCause) { + public static class LookupException extends RuntimeException { + public LookupException(String aMsg, Throwable aCause) { super(aMsg, aCause); } - public JndiWiringException(String aMsg) { + public LookupException(String aMsg) { super(aMsg); } } @@ -57,7 +69,7 @@ public class JndiProxyFactory { * * @author Erik Brakkee */ - private class JndiInvocationHandler implements InvocationHandler { + private class LookupInvocationHandler implements InvocationHandler { @Override /** @@ -67,19 +79,16 @@ public class JndiProxyFactory { throws Throwable { Object svcObj = null; try { - InitialContext ctx = new InitialContext(); - svcObj = ctx.lookup(jndi); - } catch (NamingException e) { - throw new JndiWiringException( - "Error looking up object in JNDI at '" + jndi + "'", e); + svcObj = lookup.lookup(); + } catch (Exception e) { + throw new LookupException( + "Error looking up object", e); } if (svcObj == null) { - throw new JndiWiringException("Object at '" + jndi + - "' is null"); + throw new LookupException("Object at is null"); } if (!clazz.isInstance(svcObj)) { - throw new JndiWiringException("Object in JNDI tree at '" + - jndi + "' not of type " + clazz.getName() + + throw new LookupException("Object '" + svcObj + "' is not of type " + clazz.getName() + " but of type " + svcObj.getClass().getName()); } T svc = (T) svcObj; @@ -91,7 +100,7 @@ public class JndiProxyFactory { } } - private String jndi; + private Lookup lookup; private Class clazz; /** @@ -102,13 +111,13 @@ public class JndiProxyFactory { * @param aJndi JNDI name of the object to lookup. * */ - public JndiProxyFactory(Class aClass, String aJndi) { + public LookupProxyFactory(Class aClass, Lookup aLookup) { if (!aClass.isInterface()) { throw new IllegalArgumentException("Class " + aClass.getName() + " is not an interface"); } clazz = aClass; - jndi = aJndi; + lookup = aLookup; } /** @@ -116,12 +125,12 @@ public class JndiProxyFactory { * {@link #set(Object)} * * When at runtime the proxy cannot find lookup the object in JNDI, it - * throws {@link JndiWiringException}. + * throws {@link LookupException}. * * @return Proxy. */ public T getProxy() { - InvocationHandler handler = new JndiInvocationHandler(); + InvocationHandler handler = new LookupInvocationHandler(); Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(), new Class[] { clazz }); T proxy; diff --git a/test/enterprise/src/main/java/org/wamblee/test/jndi/package-info.java b/test/enterprise/src/main/java/org/wamblee/test/jndi/package-info.java index 8777cdbb..7fd7ddbc 100644 --- a/test/enterprise/src/main/java/org/wamblee/test/jndi/package-info.java +++ b/test/enterprise/src/main/java/org/wamblee/test/jndi/package-info.java @@ -17,7 +17,5 @@ * This package provides utilities for JNDI testing. * * Using {@link StubInitialContextFactory}, JNDI can be mocked in junit tests. - * {@link JndiProxyFactory} provides a proxy that transparently delegates to an object in the - * JNDI tree. */ package org.wamblee.test.jndi; \ No newline at end of file -- 2.31.1