1 16 17 package org.springframework.aop.support; 18 19 import java.util.Map ; 20 import java.util.WeakHashMap ; 21 22 import org.aopalliance.intercept.MethodInvocation; 23 24 import org.springframework.aop.DynamicIntroductionAdvice; 25 import org.springframework.aop.IntroductionInterceptor; 26 import org.springframework.aop.ProxyMethodInvocation; 27 28 53 public class DelegatePerTargetObjectIntroductionInterceptor extends IntroductionInfoSupport 54 implements IntroductionInterceptor { 55 56 59 private Map delegateMap = new WeakHashMap (); 60 61 private Class defaultImplType; 62 63 private Class interfaceType; 64 65 66 public DelegatePerTargetObjectIntroductionInterceptor(Class defaultImplType, Class interfaceType) { 67 this.defaultImplType = defaultImplType; 68 this.interfaceType = interfaceType; 69 Object delegate = createNewDelegate(); 74 implementInterfacesOnObject(delegate); 75 suppressInterface(IntroductionInterceptor.class); 76 suppressInterface(DynamicIntroductionAdvice.class); 77 } 78 79 80 85 public Object invoke(MethodInvocation mi) throws Throwable { 86 if (isMethodOnIntroducedInterface(mi)) { 87 Object delegate = getIntroductionDelegateFor(mi.getThis()); 88 89 Object retVal = AopUtils.invokeJoinpointUsingReflection(delegate, mi.getMethod(), mi.getArguments()); 93 94 if (retVal == delegate && mi instanceof ProxyMethodInvocation) { 97 retVal = ((ProxyMethodInvocation) mi).getProxy(); 98 } 99 return retVal; 100 } 101 102 return doProceed(mi); 103 } 104 105 112 protected Object doProceed(MethodInvocation mi) throws Throwable { 113 return mi.proceed(); 115 } 116 117 private Object getIntroductionDelegateFor(Object targetObject) { 118 synchronized(this.delegateMap) { 119 if (this.delegateMap.containsKey(targetObject)) { 120 return this.delegateMap.get(targetObject); 121 } 122 else { 123 Object delegate = createNewDelegate(); 124 this.delegateMap.put(targetObject, delegate); 125 return delegate; 126 } 127 } 128 } 129 130 private Object createNewDelegate() { 131 try { 132 return this.defaultImplType.newInstance(); 133 } 134 catch (Throwable ex) { 135 throw new IllegalArgumentException ("Cannot create default implementation for '" + 136 this.interfaceType.getName() + "' mixin (" + this.defaultImplType.getName() + "): " + ex); 137 } 138 } 139 140 } 141 | Popular Tags |