01: package org.shiftone.cache;
02:
03: import org.shiftone.cache.util.CacheInvocationHandler;
04:
05: import java.lang.reflect.InvocationHandler;
06: import java.lang.reflect.Proxy;
07:
08: /**
09: * Static class CacheProxy can be used to create cache proxy objects of
10: * instances of objects that implement an interface.
11: * <pre>
12: * Thing thing = new ThingImpl();
13: * Thing cachedThing =
14: * (Thing)CacheProxy.newProxyInstance(thing, Thing.class, cache);
15: *
16: * cachedThing.doThing();
17: * </pre>
18: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
19: * @version $Revision: 1.6 $
20: */
21: public class CacheProxy {
22:
23: private static final ClassLoader DEFAULT_CLASS_LOADER = CacheProxy.class
24: .getClassLoader();
25:
26: public static Object newProxyInstance(ClassLoader loader,
27: Object target, Class iface, Cache cache)
28: throws IllegalArgumentException {
29:
30: InvocationHandler handler = null;
31: Class[] ifaces = new Class[] { iface };
32:
33: handler = new CacheInvocationHandler(target, cache);
34:
35: return Proxy.newProxyInstance(loader, ifaces, handler);
36: }
37:
38: public static Object newProxyInstance(Object target, Class iface,
39: Cache cache) throws IllegalArgumentException {
40: return newProxyInstance(DEFAULT_CLASS_LOADER, target, iface,
41: cache);
42: }
43: }
|