KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > AopContext


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.framework;
18
19 /**
20  * Class containing static methods used to obtain information about the current AOP invocation.
21  *
22  * <p>The <code>currentProxy()</code> method is usable if the AOP framework is configured to
23  * expose the current proxy (not the default). It returns the AOP proxy in use. Target objects
24  * or advice can use this to make advised calls, in the same way as <code>getEJBObject()</code>
25  * can be used in EJBs. They can also use it to find advice configuration.
26  *
27  * <p>Spring's AOP framework does not expose proxies by default, as there is a performance cost
28  * in doing so.
29  *
30  * <p>The functionality in this class might be used by a target object that needed access
31  * to resources on the invocation. However, this approach should not be used when there is
32  * a reasonable alternative, as it makes application code dependent on usage under AOP and
33  * the Spring AOP framework in particular.
34  *
35  * @author Rod Johnson
36  * @author Juergen Hoeller
37  * @since 13.03.2003
38  */

39 public abstract class AopContext {
40
41     /**
42      * ThreadLocal holder for AOP proxy associated with this thread.
43      * Will contain <code>null</code> unless the "exposeProxy" property on
44      * the controlling proxy configuration has been set to "true".
45      * @see ProxyConfig#setExposeProxy
46      */

47     private static final ThreadLocal JavaDoc currentProxy = new ThreadLocal JavaDoc();
48
49
50     /**
51      * Try to return the current AOP proxy. This method is usable only if the
52      * calling method has been invoked via AOP, and the AOP framework has been set
53      * to expose proxies. Otherwise, this method will throw an IllegalStateException.
54      * @return Object the current AOP proxy (never returns <code>null</code>)
55      * @throws IllegalStateException if the proxy cannot be found, because the
56      * method was invoked outside an AOP invocation context, or because the
57      * AOP framework has not been configured to expose the proxy
58      */

59     public static Object JavaDoc currentProxy() throws IllegalStateException JavaDoc {
60         Object JavaDoc proxy = currentProxy.get();
61         if (proxy == null) {
62             throw new IllegalStateException JavaDoc(
63                     "Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.");
64         }
65         return proxy;
66     }
67
68     /**
69      * Make the given proxy available via the <code>currentProxy()</code> method.
70      * <p>Note that the caller should be careful to keep the old value as appropriate.
71      * @param proxy the proxy to expose (or <code>null</code> to reset it)
72      * @return the old proxy, which may be <code>null</code> if none was bound
73      * @see #currentProxy()
74      */

75     public static Object JavaDoc setCurrentProxy(Object JavaDoc proxy) {
76         Object JavaDoc old = currentProxy.get();
77         currentProxy.set(proxy);
78         return old;
79     }
80
81 }
82
Popular Tags