AopContext.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.aop.framework;
  17. import org.aopalliance.aop.AspectException;
  18. /**
  19.  * Class containing static methods used to obtain information about the
  20.  * current AOP invocation. 
  21.  *
  22.  * <p>The currentProxy() method is usable if the AOP framework is configured
  23.  * to expose the current proxy (not the default). It returns the AOP proxy in 
  24.  * use. Target objects or advice can use this to make advised calls, in the same way 
  25.  * as getEJBObject() can be used in EJBs. They can also use it to find advice
  26.  * configuration.
  27.  *
  28.  * <p>The AOP framework does not expose proxies by default, as there is a performance cost
  29.  * in doing so.
  30.  *
  31.  * <p>The functionality in this class might be used by a target object
  32.  * that needed access to resources on the invocation. However, this
  33.  * approach should not be used when there is a reasonable alternative,
  34.  * as it makes application code dependent on usage under AOP and
  35.  * the Spring AOP framework.
  36.  *
  37.  * @author Rod Johnson
  38.  * @since 13-Mar-2003
  39.  */
  40. public abstract class AopContext {
  41. /**
  42.  * AOP proxy associated with this thread. Will be null unless the
  43.  * exposeInvocation property on the controlling proxy has been set to true.
  44.  * The default value for this property is false, for performance reasons.
  45.  */
  46. private static ThreadLocal currentProxy = new ThreadLocal();
  47. /**
  48.  * Try to return the current AOP proxy. This method is usable
  49.  * only if the calling method has been invoked via AOP, and the
  50.  * AOP framework has been set to expose proxies. Otherwise,
  51.  * this method will throw an AspectException.
  52.  * @return Object the current AOP proxy (never returns null)
  53.  * @throws AspectException if the proxy cannot be found,
  54.  * because the method was invoked outside an AOP invocation
  55.  * context, or because the AOP framework has not been configured
  56.  * to expose the proxy
  57.  */
  58. public static Object currentProxy() throws AspectException {
  59. if (currentProxy.get() == null) {
  60. throw new AspectException("Cannot find proxy: set 'exposeProxy' property on Advised to make it available");
  61. }
  62. return currentProxy.get();
  63. }
  64. /**
  65.  * Make the given proxy available via the currentProxy method. 
  66.  * Note that the caller should be careful to return the old value
  67.  * before it's done.
  68.  * @param proxy the proxy to expose
  69.  * @return the old proxy, which may be null if none was bound
  70.  */
  71. static Object setCurrentProxy(Object proxy) {
  72. Object old = currentProxy.get();
  73. currentProxy.set(proxy);
  74. return old;
  75. }
  76. }