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.intercept.Interceptor;
  18. import org.springframework.aop.Advisor;
  19. import org.springframework.aop.AfterReturningAdvice;
  20. import org.springframework.aop.MethodBeforeAdvice;
  21. import org.springframework.aop.TargetSource;
  22. import org.springframework.aop.ThrowsAdvice;
  23. /**
  24. * Interface to be implemented by classes that hold the configuration
  25. * of a factory of AOP proxies. This configuration includes
  26. * the Interceptors and Advisors, and the proxied interfaces.
  27. *
  28. * <p>Any AOP proxy obtained from Spring can be cast to this interface to allow
  29. * manipulation of its AOP advice.
  30. *
  31. * @author Rod Johnson
  32. * @since 13-Mar-2003
  33. * @version $Id: Advised.java,v 1.12 2004/04/14 18:55:18 johnsonr Exp $
  34. * @see org.springframework.aop.framework.AdvisedSupport
  35. */
  36. public interface Advised {
  37. /**
  38. * Return the TargetSource used by this Advised object
  39. * @return the TargetSource used by this advised object
  40. */
  41. TargetSource getTargetSource();
  42. /**
  43. * Get whether the factory should expose the proxy as a ThreadLocal.
  44. * This can be necessary if a target object needs to invoke a method on itself
  45. * benefitting from advice. (If it invokes a method on <code>this</code> no advice
  46. * will apply.) Getting the proxy is analogous to an EJB calling getEJBObject().
  47. * @return whether the factory should expose the proxy as a ThreadLocal
  48. * @see AopContext
  49. */
  50. boolean getExposeProxy();
  51. /**
  52. * Should we proxy the target class as well as any interfaces?
  53. * Can use this to force CGLIB proxying.
  54. * @return whether we proxy the target class as well as any interfaces
  55. */
  56. boolean getProxyTargetClass();
  57. /**
  58. * Return the Advisors applying to this proxy.
  59. * @return a list of Advisors applying to this proxy. Cannot return null,
  60. * but may return the empty array.
  61. */
  62. Advisor[] getAdvisors();
  63. /**
  64. * Return the interfaces proxied by the AOP proxy. Will not
  65. * include the target class, which may also be proxied.
  66. * @return the interfaces proxied by the AOP proxy
  67. */
  68. Class[] getProxiedInterfaces();
  69. /**
  70. * Return whether this interface is proxied
  71. * @param intf interface to test
  72. * @return whether the interface is proxied
  73. */
  74. boolean isInterfaceProxied(Class intf);
  75. /**
  76. * Add the given AOP Alliance interceptor to the tail of the advice (interceptor) chain.
  77. * This will be wrapped in a DefaultPointcutAdvistor with a pointcut that
  78. * always applies, and returned from the getAdvisors() method in this
  79. * wrapped form.
  80. * @param interceptor to add to the tail of the chain
  81. * @see #addInterceptor(int, Interceptor)
  82. * @see org.springframework.aop.support.DefaultPointcutAdvisor
  83. */
  84. void addInterceptor(Interceptor interceptor) throws AopConfigException;
  85. /**
  86. * Add the given AOP Alliance interceptor at the specified position in the interceptor chain.
  87. * @param pos index from 0 (head)
  88. * @param interceptor interceptor to add at the specified position in the
  89. * interceptor chain
  90. */
  91. void addInterceptor(int pos, Interceptor interceptor) throws AopConfigException;
  92. /**
  93. * Add an AfterReturningAdvice to the tail of the advice chain
  94. * @param afterReturningAdvice AfterReturningAdvice to add
  95. * @throws AopConfigException if the advice cannot be added--for example, because
  96. * the proxy configuration is frozen
  97. */
  98. void addAfterReturningAdvice(AfterReturningAdvice afterReturningAdvice) throws AopConfigException;
  99. /**
  100. * Add a MethodBeforeAdvice to the tail of the advice chain
  101. * @param beforeAdvice MethodBeforeAdvice to add
  102. * @throws AopConfigException if the advice cannot be added--for example, because
  103. * the proxy configuration is frozen
  104. */
  105. void addBeforeAdvice(MethodBeforeAdvice beforeAdvice) throws AopConfigException;
  106. /**
  107. * Add a ThrowsAdvice to the tail of the advice chain
  108. * @param throwsAdvice ThrowsAdvice to add
  109. * @throws AopConfigException if the advice cannot be added--for example, because
  110. * the proxy configuration is frozen
  111. */
  112. void addThrowsAdvice(ThrowsAdvice throwsAdvice) throws AopConfigException;
  113. /**
  114. * Add an Advisor at the end of the advisor chain.
  115. * The Advisor may be an IntroductionAdvisor, in which new interfaces
  116. * will be available when a proxy is next obtained from the relevant factory.
  117. * @param advisor Advisor to add to the end of the chain
  118. */
  119. void addAdvisor(Advisor advisor) throws AopConfigException;
  120. /**
  121. * Add an Advisor at the specified position in the chain
  122. * @param advisor advisor to add at the specified position in the chain
  123. * @param pos position in chain (0 is head). Must be valid.
  124. */
  125. void addAdvisor(int pos, Advisor advisor) throws AopConfigException;
  126. /**
  127. * Return the index (from 0) of the given advisor,
  128. * or -1 if no such advisor applies to this proxy.
  129. * The return value of this method can be used to index into
  130. * the Advisors array.
  131. * @param advisor advisor to search for
  132. * @return index from 0 of this advisor, or -1 if there's
  133. * no such advisor.
  134. */
  135. int indexOf(Advisor advisor);
  136. /**
  137. * Remove the given advisor
  138. * @param advisor advisor to remove
  139. * @return true if the advisor was removed; false if the
  140. * advisor was not found and hence could not be removed
  141. */
  142. boolean removeAdvisor(Advisor advisor) throws AopConfigException;
  143. /**
  144. * Remove the advisor at the given index
  145. * @param index index of advisor to remove
  146. * @throws AopConfigException if the index is invalid
  147. */
  148. void removeAdvisor(int index) throws AopConfigException;
  149. /**
  150. * Replace the given advisor.
  151. * <b>NB:</b>If the advisor is an IntroductionAdvisor
  152. * and the replacement is not or implements different interfaces,
  153. * the proxy will need to be re-obtained or the old interfaces
  154. * won't be supported and the new interface won't be implemented.
  155. * @param a advisor to replace
  156. * @param b advisor to replace it with
  157. * @return whether it was replaced. If the advisor wasn't found in the
  158. * list of advisors, this method returns false and does nothing.
  159. */
  160. boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException;
  161. /**
  162. * @return whether the Advised configuration is frozen, and no
  163. * advice changes can be made
  164. */
  165. boolean isFrozen();
  166. /**
  167. * As toString() will normally pass to the target,
  168. * this returns the equivalent for the AOP proxy
  169. * @return a string description of the proxy configuration
  170. */
  171. String toProxyConfigString();
  172. }