KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > interceptor > ExposeBeanNameAdvisors


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
30  * Convenient methods for creating advisors that may be used when autoproxying beans
31  * created with the Spring IoC container, binding the bean name to the current
32  * invocation. May support a <code>bean()</code> pointcut designator with AspectJ.
33  *
34  * <p>Typically used in Spring auto-proxying, where the bean name is known
35  * at proxy creation time.
36  *
37  * @author Rod Johnson
38  * @author Juergen Hoeller
39  * @since 2.0
40  * @see org.springframework.beans.factory.NamedBean
41  */

42 public abstract class ExposeBeanNameAdvisors {
43
44     /**
45      * Binding for the bean name of the bean which is currently being invoked
46      * in the ReflectiveMethodInvocation userAttributes Map.
47      */

48     private static final String JavaDoc BEAN_NAME_ATTRIBUTE = ExposeBeanNameAdvisors.class.getName() + ".beanName";
49
50
51     /**
52      * Find the bean name for the current invocation. Assumes that an ExposeBeanNameAdvisor
53      * has been included in the interceptor chain, and that the invocation is exposed
54      * with ExposeInvocationInterceptor.
55      * @return the bean name (never <code>null</code>)
56      * @throws IllegalStateException if the bean name has not been exposed
57      */

58     public static String JavaDoc getBeanName() throws IllegalStateException JavaDoc {
59         return getBeanName(ExposeInvocationInterceptor.currentInvocation());
60     }
61
62     /**
63      * Find the bean name for the given invocation. Assumes that an ExposeBeanNameAdvisor
64      * has been included in the interceptor chain.
65      * @param mi MethodInvocation that should contain the bean name as an attribute
66      * @return the bean name (never <code>null</code>)
67      * @throws IllegalStateException if the bean name has not been exposed
68      */

69     public static String JavaDoc getBeanName(MethodInvocation mi) throws IllegalStateException JavaDoc {
70         if (!(mi instanceof ProxyMethodInvocation)) {
71             throw new IllegalArgumentException JavaDoc("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
72         }
73         ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi;
74         String JavaDoc beanName = (String JavaDoc) pmi.getUserAttribute(BEAN_NAME_ATTRIBUTE);
75         if (beanName == null) {
76             throw new IllegalStateException JavaDoc("Cannot get bean name; not set on MethodInvocation: " + mi);
77         }
78         return beanName;
79     }
80
81     /**
82      * Create a new advisor that will expose the given bean name,
83      * with no introduction
84      * @param beanName bean name to expose
85      */

86     public static Advisor createAdvisorWithoutIntroduction(String JavaDoc beanName) {
87         return new DefaultPointcutAdvisor(new ExposeBeanNameInterceptor(beanName));
88     }
89
90     /**
91      * Create a new advisor that will expose the given bean name, introducing
92      * the NamedBean interface to make the bean name accessible without forcing
93      * the target object to be aware of this Spring IoC concept.
94      * @param beanName the bean name to expose
95      */

96     public static Advisor createAdvisorIntroducingNamedBean(String JavaDoc beanName) {
97         return new DefaultIntroductionAdvisor(new ExposeBeanNameIntroduction(beanName));
98     }
99
100
101     /**
102      * Interceptor that exposes the specified bean name as invocation attribute.
103      */

104     private static class ExposeBeanNameInterceptor implements MethodInterceptor {
105
106         private final String JavaDoc beanName;
107
108         public ExposeBeanNameInterceptor(String JavaDoc beanName) {
109             this.beanName = beanName;
110         }
111
112         public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
113             if (!(mi instanceof ProxyMethodInvocation)) {
114                 throw new IllegalStateException JavaDoc("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     /**
124      * Introduction that exposes the specified bean name as invocation attribute.
125      */

126     private static class ExposeBeanNameIntroduction extends DelegatingIntroductionInterceptor implements NamedBean {
127
128         private final String JavaDoc beanName;
129
130         public ExposeBeanNameIntroduction(String JavaDoc beanName) {
131             this.beanName = beanName;
132         }
133
134         public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
135             if (!(mi instanceof ProxyMethodInvocation)) {
136                 throw new IllegalStateException JavaDoc("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 JavaDoc getBeanName() {
144             return this.beanName;
145         }
146     }
147
148 }
149
Popular Tags