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. * @version $Id: AopContext.java,v 1.7 2004/04/01 15:35:46 jhoeller Exp $
  40. */
  41. public abstract class AopContext {
  42. /**
  43. * AOP proxy associated with this thread. Will be null unless the
  44. * exposeInvocation property on the controlling proxy has been set to true.
  45. * The default value for this property is false, for performance reasons.
  46. */
  47. private static ThreadLocal currentProxy = new ThreadLocal();
  48. /**
  49. * Try to return the current AOP proxy. This method is usable
  50. * only if the calling method has been invoked via AOP, and the
  51. * AOP framework has been set to expose proxies. Otherwise,
  52. * this method will throw an AspectException.
  53. * @return Object the current AOP proxy (never returns null)
  54. * @throws AspectException if the proxy cannot be found,
  55. * because the method was invoked outside an AOP invocation
  56. * context, or because the AOP framework has not been configured
  57. * to expose the proxy
  58. */
  59. public static Object currentProxy() throws AspectException {
  60. if (currentProxy.get() == null) {
  61. throw new AspectException("Cannot find proxy: set 'exposeProxy' property on Advised to make it available");
  62. }
  63. return currentProxy.get();
  64. }
  65. /**
  66. * Make the given proxy available via the currentProxy method.
  67. * Note that the caller should be careful to return the old value
  68. * before it's done.
  69. * @param proxy the proxy to expose
  70. * @return the old proxy, which may be null if none was bound
  71. */
  72. static Object setCurrentProxy(Object proxy) {
  73. Object old = currentProxy.get();
  74. currentProxy.set(proxy);
  75. return old;
  76. }
  77. }