01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 currentProxy = new ThreadLocal();
48:
49: /**
50: * Try to return the current AOP proxy. This method is usable only if the
51: * calling method has been invoked via AOP, and the AOP framework has been set
52: * to expose proxies. Otherwise, this method will throw an IllegalStateException.
53: * @return Object the current AOP proxy (never returns <code>null</code>)
54: * @throws IllegalStateException if the proxy cannot be found, because the
55: * method was invoked outside an AOP invocation context, or because the
56: * AOP framework has not been configured to expose the proxy
57: */
58: public static Object currentProxy() throws IllegalStateException {
59: Object proxy = currentProxy.get();
60: if (proxy == null) {
61: throw new IllegalStateException(
62: "Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.");
63: }
64: return proxy;
65: }
66:
67: /**
68: * Make the given proxy available via the <code>currentProxy()</code> method.
69: * <p>Note that the caller should be careful to keep the old value as appropriate.
70: * @param proxy the proxy to expose (or <code>null</code> to reset it)
71: * @return the old proxy, which may be <code>null</code> if none was bound
72: * @see #currentProxy()
73: */
74: public static Object setCurrentProxy(Object proxy) {
75: Object old = currentProxy.get();
76: currentProxy.set(proxy);
77: return old;
78: }
79:
80: }
|