1 16 17 package org.springframework.aop.interceptor; 18 19 import org.aopalliance.intercept.MethodInterceptor; 20 import org.aopalliance.intercept.MethodInvocation; 21 22 import org.springframework.aop.Advisor; 23 import org.springframework.aop.ProxyMethodInvocation; 24 import org.springframework.aop.support.DefaultIntroductionAdvisor; 25 import org.springframework.aop.support.DefaultPointcutAdvisor; 26 import org.springframework.aop.support.DelegatingIntroductionInterceptor; 27 import org.springframework.beans.factory.NamedBean; 28 29 42 public abstract class ExposeBeanNameAdvisors { 43 44 48 private static final String BEAN_NAME_ATTRIBUTE = ExposeBeanNameAdvisors.class.getName() + ".beanName"; 49 50 51 58 public static String getBeanName() throws IllegalStateException { 59 return getBeanName(ExposeInvocationInterceptor.currentInvocation()); 60 } 61 62 69 public static String getBeanName(MethodInvocation mi) throws IllegalStateException { 70 if (!(mi instanceof ProxyMethodInvocation)) { 71 throw new IllegalArgumentException ("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); 72 } 73 ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi; 74 String beanName = (String ) pmi.getUserAttribute(BEAN_NAME_ATTRIBUTE); 75 if (beanName == null) { 76 throw new IllegalStateException ("Cannot get bean name; not set on MethodInvocation: " + mi); 77 } 78 return beanName; 79 } 80 81 86 public static Advisor createAdvisorWithoutIntroduction(String beanName) { 87 return new DefaultPointcutAdvisor(new ExposeBeanNameInterceptor(beanName)); 88 } 89 90 96 public static Advisor createAdvisorIntroducingNamedBean(String beanName) { 97 return new DefaultIntroductionAdvisor(new ExposeBeanNameIntroduction(beanName)); 98 } 99 100 101 104 private static class ExposeBeanNameInterceptor implements MethodInterceptor { 105 106 private final String beanName; 107 108 public ExposeBeanNameInterceptor(String beanName) { 109 this.beanName = beanName; 110 } 111 112 public Object invoke(MethodInvocation mi) throws Throwable { 113 if (!(mi instanceof ProxyMethodInvocation)) { 114 throw new IllegalStateException ("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); 115 } 116 ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi; 117 pmi.setUserAttribute(BEAN_NAME_ATTRIBUTE, beanName); 118 return mi.proceed(); 119 } 120 } 121 122 123 126 private static class ExposeBeanNameIntroduction extends DelegatingIntroductionInterceptor implements NamedBean { 127 128 private final String beanName; 129 130 public ExposeBeanNameIntroduction(String beanName) { 131 this.beanName = beanName; 132 } 133 134 public Object invoke(MethodInvocation mi) throws Throwable { 135 if (!(mi instanceof ProxyMethodInvocation)) { 136 throw new IllegalStateException ("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); 137 } 138 ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi; 139 pmi.setUserAttribute(BEAN_NAME_ATTRIBUTE, beanName); 140 return super.invoke(mi); 141 } 142 143 public String getBeanName() { 144 return this.beanName; 145 } 146 } 147 148 } 149 | Popular Tags |