KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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 java.io.Serializable JavaDoc;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23
24 import org.springframework.aop.Advisor;
25 import org.springframework.aop.support.DefaultPointcutAdvisor;
26
27 /**
28  * Interceptor that exposes the current MethodInvocation.
29  * We occasionally need to do this; for example, when a pointcut
30  * or target object needs to know the Invocation context.
31  *
32  * <p>Don't use this interceptor unless this is really necessary.
33  * Target objects should not normally know about Spring AOP,
34  * as this creates a dependency on Spring. Target objects
35  * should be plain POJOs as far as possible.
36  *
37  * <p>If used, this interceptor will normally be the first
38  * in the interceptor chain.
39  *
40  * @author Rod Johnson
41  */

42 public class ExposeInvocationInterceptor implements MethodInterceptor, Serializable JavaDoc {
43     
44     /** Singleton instance of this class */
45     public static final ExposeInvocationInterceptor INSTANCE = new ExposeInvocationInterceptor();
46     
47     /**
48      * Singleton advisor for this class. Use in preference to INSTANCE when using
49      * Spring AOP, as it prevents the need to create a new Advisor to wrap the instance.
50      */

51     public static final Advisor ADVISOR = new DefaultPointcutAdvisor(INSTANCE) {
52         public int getOrder() {
53             return Integer.MIN_VALUE;
54         }
55         public String JavaDoc toString() {
56             return ExposeInvocationInterceptor.class.getName() +".ADVISOR";
57         }
58     };
59
60     private static final ThreadLocal JavaDoc invocation = new ThreadLocal JavaDoc();
61
62
63     /**
64      * Return the AOP Alliance MethodInvocation object associated with the current invocation.
65      * @return the invocation object associated with the current invocation
66      * @throws IllegalStateException if there is no AOP invocation in progress,
67      * or if the ExposeInvocationInterceptor was not added to this interceptor chain
68      */

69     public static MethodInvocation currentInvocation() throws IllegalStateException JavaDoc {
70         MethodInvocation mi = (MethodInvocation) invocation.get();
71         if (mi == null)
72             throw new IllegalStateException JavaDoc(
73                     "No MethodInvocation found: Check that an AOP invocation is in progress, " +
74                     "and that the ExposeInvocationInterceptor is in the interceptor chain.");
75         return mi;
76     }
77
78
79     /**
80      * Ensure that only the canonical instance can be created.
81      */

82     private ExposeInvocationInterceptor() {
83     }
84
85     public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
86         Object JavaDoc old = invocation.get();
87         invocation.set(mi);
88         try {
89             return mi.proceed();
90         }
91         finally {
92             invocation.set(old);
93         }
94     }
95     
96     /**
97      * Required to support serialization. Replaces with canonical instance
98      * on deserialization, protecting Singleton pattern.
99      * Alternative to overriding the <code>equals</code> method.
100      */

101     private Object JavaDoc readResolve() {
102         return INSTANCE;
103     }
104
105 }
106
Popular Tags