/**
* 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<T> {
+public class LookupProxyFactory<T> {
+
+ /**
+ * 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);
}
}
*
* @author Erik Brakkee
*/
- private class JndiInvocationHandler implements InvocationHandler {
+ private class LookupInvocationHandler implements InvocationHandler {
@Override
/**
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;
}
}
- private String jndi;
+ private Lookup lookup;
private Class clazz;
/**
* @param aJndi JNDI name of the object to lookup.
*
*/
- public JndiProxyFactory(Class<T> aClass, String aJndi) {
+ public LookupProxyFactory(Class<T> aClass, Lookup aLookup) {
if (!aClass.isInterface()) {
throw new IllegalArgumentException("Class " + aClass.getName() +
" is not an interface");
}
clazz = aClass;
- jndi = aJndi;
+ lookup = aLookup;
}
/**
* {@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;