*/
package org.wamblee.general;
+import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 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.
*
* @author Erik Brakkee
*/
- public static interface Lookup {
+ public static interface Lookup extends Serializable {
/**
* Looks up the object.
* @return Object (non-null)
*
* @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
/**
"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() +
* @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;