(no commit message)
[utils] / support / general / src / main / java / org / wamblee / general / LookupProxyFactory.java
index 5315186259443eadc8dae4ad73cf2858e664f63e..286e5f5651ead211874d53773753be89a7a56431 100644 (file)
@@ -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.
@@ -39,7 +40,7 @@ public class LookupProxyFactory<T> {
      * 
      * @author Erik Brakkee
      */
-    public static interface Lookup { 
+    public static interface Lookup extends Serializable 
          /**
           * Looks up the object. 
           * @return Object (non-null)
@@ -69,7 +70,19 @@ public class LookupProxyFactory<T> {
      * 
      * @author Erik Brakkee
      */
-    private class LookupInvocationHandler implements InvocationHandler {
+    private static class LookupInvocationHandler<T> 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
         /**
@@ -85,7 +98,7 @@ public class LookupProxyFactory<T> {
                     "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() +
@@ -130,7 +143,7 @@ public class LookupProxyFactory<T> {
      * @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;