1 22 package org.jboss.aop.advice; 23 24 import org.jboss.aop.Advised; 25 import org.jboss.aop.Advisor; 26 import org.jboss.aop.InstanceAdvisor; 27 import org.jboss.aop.joinpoint.CallerInvocation; 28 import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation; 29 import org.jboss.aop.joinpoint.Invocation; 30 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation; 31 import org.jboss.aop.proxy.container.ClassProxyContainer; 32 import org.jboss.aop.proxy.container.ContainerProxyMethodInvocation; 33 34 import java.lang.reflect.InvocationTargetException ; 35 import java.lang.reflect.Method ; 36 37 44 public class PerInstanceAdvice extends AbstractAdvice 45 { 46 private boolean initialized = false; 47 AspectDefinition aspectDefinition; 48 49 public PerInstanceAdvice(String adviceName, AspectDefinition a, Advisor advisor) 50 { 51 aspectDefinition = a; 52 this.adviceName = adviceName; 53 advisor.addPerInstanceAspect(a); 54 } 55 56 public String getName() 57 { 58 return aspectDefinition.getName() + "." + adviceName; 59 } 60 61 public Object invoke(Invocation invocation) throws Throwable 62 { 63 Object aspect = null; 64 if (invocation instanceof CallerInvocation) 65 { 66 Object callingObject = null; 69 70 if (invocation instanceof ConstructorCalledByMethodInvocation) 71 { 72 callingObject = ((ConstructorCalledByMethodInvocation)invocation).getCallingObject(); 73 } 74 else if (invocation instanceof MethodCalledByMethodInvocation) 75 { 76 callingObject = ((MethodCalledByMethodInvocation)invocation).getCallingObject(); 77 } 78 79 if (callingObject == null) return invocation.invokeNext(); 81 Advised advised = (Advised) callingObject; 82 InstanceAdvisor advisor = advised._getInstanceAdvisor(); 83 aspect = advisor.getPerInstanceAspect(aspectDefinition); 84 } 85 else 86 { 87 Object targetObject = invocation.getTargetObject(); 88 if (targetObject == null) return invocation.invokeNext(); 90 InstanceAdvisor instanceAdvisor = null; 91 if (targetObject instanceof Advised) 92 { 93 Advised advised = (Advised)targetObject; 94 instanceAdvisor = advised._getInstanceAdvisor(); 95 } 96 else 97 { 98 Advisor advisor = invocation.getAdvisor(); 99 if (advisor == null) 100 { 101 return invocation.invokeNext(); 102 } 103 else if (advisor instanceof InstanceAdvisor) 104 { 105 instanceAdvisor = (InstanceAdvisor) advisor; 106 } 107 else if (advisor instanceof ClassProxyContainer && invocation instanceof ContainerProxyMethodInvocation) 108 { 109 ContainerProxyMethodInvocation pi = (ContainerProxyMethodInvocation)invocation; 110 instanceAdvisor = pi.getProxy().getInstanceAdvisor(); 111 } 112 else 113 { 114 return invocation.invokeNext(); 115 } 116 } 117 aspect = instanceAdvisor.getPerInstanceAspect(aspectDefinition); 118 } 119 120 if (!initialized) 121 { 122 init(adviceName, aspect.getClass()); 123 initialized = true; 124 } 125 Method advice = resolveAdvice(invocation); 126 Object [] args = {invocation}; 127 128 try 129 { 130 return advice.invoke(aspect, args); 131 } 132 catch (InvocationTargetException e) 133 { 134 throw e.getCause(); } 136 } 137 138 } 139 | Popular Tags |