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 java.lang.reflect.Method;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.aopalliance.intercept.MethodInterceptor;
  21. import org.springframework.aop.Advisor;
  22. import org.springframework.aop.IntroductionAdvisor;
  23. import org.springframework.aop.MethodMatcher;
  24. import org.springframework.aop.PointcutAdvisor;
  25. import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
  26. import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
  27. /**
  28. * Utility methods for use by AdviceChainFactory implementations.
  29. *
  30. * <p>The calculateInterceptorsAndDynamicInterceptionAdvice method is the
  31. * definitive way of working out an advice chain for a Method, given an
  32. * Advised object.
  33. *
  34. * @author Rod Johnson
  35. * @version $Id: AdvisorChainFactoryUtils.java,v 1.8 2004/05/23 20:13:06 jhoeller Exp $
  36. */
  37. public abstract class AdvisorChainFactoryUtils {
  38. public static final AdvisorChainFactory SIMPLE_ADVISOR_CHAIN_FACTORY = new AdvisorChainFactory() {
  39. public List getInterceptorsAndDynamicInterceptionAdvice(Advised config, Object proxy,
  40. Method method, Class targetClass) {
  41. return calculateInterceptorsAndDynamicInterceptionAdvice(config, proxy, method, targetClass);
  42. }
  43. public void activated(AdvisedSupport advisedSupport) {
  44. }
  45. public void adviceChanged(AdvisedSupport advisedSupport) {
  46. }
  47. };
  48. /**
  49. * Return the static interceptors and dynamic interception advice that may apply
  50. * to this method invocation.
  51. * @return list of MethodInterceptor and InterceptionAdvice (if there's a dynamic
  52. * method matcher that needs evaluation at runtime)
  53. */
  54. public static List calculateInterceptorsAndDynamicInterceptionAdvice(Advised config, Object proxy,
  55. Method method, Class targetClass) {
  56. List interceptors = new ArrayList(config.getAdvisors().length);
  57. AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
  58. for (int i = 0; i < config.getAdvisors().length; i++) {
  59. Advisor advisor = config.getAdvisors()[i];
  60. if (advisor instanceof PointcutAdvisor) {
  61. // Add it conditionally
  62. PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
  63. if (pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) {
  64. MethodInterceptor interceptor = (MethodInterceptor) registry.getInterceptor(advisor);
  65. MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
  66. if (mm.matches(method, targetClass)) {
  67. if (mm.isRuntime()) {
  68. // Creating a new object instance in the getInterceptor() method
  69. // isn't a problem as we normally cache created chains.
  70. interceptors.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm) );
  71. }
  72. else {
  73. interceptors.add(interceptor);
  74. }
  75. }
  76. }
  77. }
  78. else if (advisor instanceof IntroductionAdvisor) {
  79. IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
  80. if (ia.getClassFilter().matches(targetClass)) {
  81. MethodInterceptor interceptor = (MethodInterceptor) registry.getInterceptor(advisor);
  82. interceptors.add(interceptor);
  83. }
  84. }
  85. }
  86. return interceptors;
  87. }
  88. }