KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > CacheProxy


1 package org.shiftone.cache;
2
3
4
5 import org.shiftone.cache.util.CacheInvocationHandler;
6
7 import java.lang.reflect.InvocationHandler JavaDoc;
8 import java.lang.reflect.Proxy JavaDoc;
9
10
11 /**
12  * Static class CacheProxy can be used to create cache proxy objects of
13  * instances of objects that implement an interface.
14  * <pre>
15  * Thing thing = new ThingImpl();
16  * Thing cachedThing =
17  * (Thing)CacheProxy.newProxyInstance(thing, Thing.class, cache);
18  *
19  * cachedThing.doThing();
20  * </pre>
21  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
22  * @version $Revision: 1.6 $
23  */

24 public class CacheProxy
25 {
26
27     private static final ClassLoader JavaDoc DEFAULT_CLASS_LOADER = CacheProxy.class.getClassLoader();
28
29     public static Object JavaDoc newProxyInstance(ClassLoader JavaDoc loader, Object JavaDoc target, Class JavaDoc iface, Cache cache) throws IllegalArgumentException JavaDoc
30     {
31
32         InvocationHandler handler = null;
33         Class JavaDoc[] ifaces = new Class JavaDoc[]{ iface };
34
35         handler = new CacheInvocationHandler(target, cache);
36
37         return Proxy.newProxyInstance(loader, ifaces, handler);
38     }
39
40
41     public static Object JavaDoc newProxyInstance(Object JavaDoc target, Class JavaDoc iface, Cache cache) throws IllegalArgumentException JavaDoc
42     {
43         return newProxyInstance(DEFAULT_CLASS_LOADER, target, iface, cache);
44     }
45 }
46
Popular Tags