X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fgeneral%2FLookupProxyFactory.java;h=4841859a812ea1d88826b2c5aa363ed8e96bd845;hb=e9e0d2ee81f5b2b10c3dc2f9e0ee4b522682722c;hp=5315186259443eadc8dae4ad73cf2858e664f63e;hpb=2367974fc98e6e1775b28958817a0d8ec981b0b8;p=utils diff --git a/support/general/src/main/java/org/wamblee/general/LookupProxyFactory.java b/support/general/src/main/java/org/wamblee/general/LookupProxyFactory.java index 53151862..4841859a 100644 --- a/support/general/src/main/java/org/wamblee/general/LookupProxyFactory.java +++ b/support/general/src/main/java/org/wamblee/general/LookupProxyFactory.java @@ -15,6 +15,7 @@ */ package org.wamblee.general; +import java.io.Serializable; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -25,7 +26,7 @@ import javax.naming.NamingException; /** * Proxy factory that can provide contextual references to objects retrieved - * through a lookup mechanism. + * through a lookup mechanism. The returned proxies are serializable. * * @param T * Interface to proxy. @@ -33,23 +34,25 @@ import javax.naming.NamingException; * */ public class LookupProxyFactory { - + /** - * Interface to lookup the object to delegate to. + * 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; + public static interface Lookup extends Serializable { + /** + * 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. + * Exception thrown in case an object cannot be retrieved from JNDI. * * @author Erik Brakkee */ @@ -64,12 +67,27 @@ public class LookupProxyFactory { } /** - * Invocation handler that does a lookup in JNDI and invokes the method on the - * object it found. + * Invocation handler that does a lookup in JNDI and invokes the method on + * the object it found. * * @author Erik Brakkee */ - private class LookupInvocationHandler implements InvocationHandler { + private static class LookupInvocationHandler implements + InvocationHandler, Serializable { + + private Class clazz; + private Lookup lookup; + + /** + * Constructs the invocation handler. + * + * @param aLookup + * Lookup class. + */ + public LookupInvocationHandler(Class aClass, Lookup aLookup) { + clazz = aClass; + lookup = aLookup; + } @Override /** @@ -81,15 +99,15 @@ public class LookupProxyFactory { try { svcObj = lookup.lookup(); } catch (Exception e) { - throw new LookupException( - "Error looking up object", e); + throw new LookupException("Error looking up object", e); } if (svcObj == null) { - throw new LookupException("Object at is null"); + throw new LookupException("Object is null"); } if (!clazz.isInstance(svcObj)) { - throw new LookupException("Object '" + svcObj + "' is not of type " + clazz.getName() + - " but of type " + svcObj.getClass().getName()); + throw new LookupException("Object '" + svcObj + + "' is not of type " + clazz.getName() + " but of type " + + svcObj.getClass().getName()); } T svc = (T) svcObj; try { @@ -108,7 +126,8 @@ public class LookupProxyFactory { * * @param aClass * Interface class of the service to proxy. - * @param aJndi JNDI name of the object to lookup. + * @param aJndi + * JNDI name of the object to lookup. * */ public LookupProxyFactory(Class aClass, Lookup aLookup) { @@ -117,20 +136,20 @@ public class LookupProxyFactory { " is not an interface"); } clazz = aClass; - lookup = aLookup; + lookup = aLookup; } /** - * Gets the proxy that delegates to the thread-specific instance set by - * {@link #set(Object)} + * Gets the proxy that delegates to the instance obtained through the + * lookup interface. * - * When at runtime the proxy cannot find lookup the object in JNDI, it - * throws {@link LookupException}. + * When at runtime the proxy cannot lookup the object, it + * throws {@link LookupException}. * * @return Proxy. */ public T getProxy() { - InvocationHandler handler = new LookupInvocationHandler(); + InvocationHandler handler = new LookupInvocationHandler(clazz, lookup); Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(), new Class[] { clazz }); T proxy;