01: /*
02: * Created on 17-Mar-2006
03: */
04: package uk.org.ponder.rsac;
05:
06: import java.lang.reflect.Method;
07:
08: import net.sf.cglib.proxy.Callback;
09: import net.sf.cglib.proxy.CallbackFilter;
10: import net.sf.cglib.proxy.Enhancer;
11: import net.sf.cglib.proxy.MethodInterceptor;
12: import net.sf.cglib.proxy.MethodProxy;
13: import net.sf.cglib.proxy.NoOp;
14:
15: import org.springframework.aop.TargetSource;
16:
17: /**
18: * A "Pea interceptor" that forces the single "get" method of a pea to return
19: * "this", in spite of interference from Spring's
20: * <code>massageReturnTypeIfNecessary</code> method.
21: *
22: * @author Antranig Basman (antranig@caret.cam.ac.uk)
23: *
24: */
25: // http://www.ociweb.com/jnb/jnbNov2005.html
26: public class RSACPeaProxyFactory implements MethodInterceptor {
27:
28: private Class targetclass;
29: private TargetSource targetsource;
30:
31: public void setTargetClass(Class targetclass) {
32: this .targetclass = targetclass;
33: }
34:
35: public void setTargetSource(TargetSource targetsource) {
36: this .targetsource = targetsource;
37: }
38:
39: public RSACPeaProxyFactory(Class targetclass,
40: TargetSource targetsource) {
41: this .targetclass = targetclass;
42: this .targetsource = targetsource;
43: }
44:
45: public Object getProxy() {
46: Enhancer enhancer = new Enhancer();
47: enhancer.setSuperclass(targetclass);
48:
49: enhancer.setCallbackFilter(new CallbackFilter() {
50: public int accept(Method method) {
51: return method.getName().equals("get") ? 1 : 0;
52: }
53: });
54:
55: enhancer.setCallbacks(new Callback[] { NoOp.INSTANCE, this });
56: return enhancer.create();
57: }
58:
59: public Object intercept(Object obj, Method method, Object[] args,
60: MethodProxy proxy) throws Throwable {
61: Object target = targetsource.getTarget();
62: return proxy.invoke(target, args);
63: }
64:
65: }
|