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.support;
  17. import java.util.HashSet;
  18. import java.util.Iterator;
  19. import java.util.Set;
  20. import org.aopalliance.aop.Advice;
  21. import org.springframework.aop.ClassFilter;
  22. import org.springframework.aop.IntroductionAdvisor;
  23. import org.springframework.aop.IntroductionInterceptor;
  24. import org.springframework.core.Ordered;
  25. /**
  26. * Simple IntroductionAdvisor implementation that by default applies to any class.
  27. * @author Rod Johnson
  28. * @since 11-Nov-2003
  29. * @version $Id: DefaultIntroductionAdvisor.java,v 1.6 2004/04/21 17:49:37 jhoeller Exp $
  30. */
  31. public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFilter, Ordered {
  32. private int order = Integer.MAX_VALUE;
  33. private IntroductionInterceptor interceptor;
  34. private Set interfaces = new HashSet();
  35. public DefaultIntroductionAdvisor(IntroductionInterceptor interceptor) {
  36. this.interceptor = interceptor;
  37. }
  38. public DefaultIntroductionAdvisor(IntroductionInterceptor interceptor, Class clazz) {
  39. this(interceptor);
  40. addInterface(clazz);
  41. }
  42. /**
  43. * Wrap the given interceptor and introduce all interfaces.
  44. */
  45. public DefaultIntroductionAdvisor(DelegatingIntroductionInterceptor dii) {
  46. this((IntroductionInterceptor) dii);
  47. Class[] introducedInterfaces = dii.getIntroducedInterfaces();
  48. for (int i = 0; i < introducedInterfaces.length; i++) {
  49. addInterface(introducedInterfaces[i]);
  50. }
  51. }
  52. public void setOrder(int order) {
  53. this.order = order;
  54. }
  55. public int getOrder() {
  56. return order;
  57. }
  58. public void addInterface(Class intf) {
  59. this.interfaces.add(intf);
  60. }
  61. public ClassFilter getClassFilter() {
  62. return this;
  63. }
  64. public Advice getAdvice() {
  65. return interceptor;
  66. }
  67. public Class[] getInterfaces() {
  68. return (Class[]) this.interfaces.toArray(new Class[this.interfaces.size()]);
  69. }
  70. public boolean matches(Class clazz) {
  71. return true;
  72. }
  73. /**
  74. * Default for an introduction is per-instance interception.
  75. */
  76. public boolean isPerInstance() {
  77. return true;
  78. }
  79. public void validateInterfaces() throws IllegalArgumentException {
  80. for (Iterator ut = this.interfaces.iterator(); ut.hasNext();) {
  81. Class intf = (Class) ut.next();
  82. if (!intf.isInterface()) {
  83. throw new IllegalArgumentException("Class '" + intf.getName() +
  84. "' is not an interface; cannot be used in an introduction");
  85. }
  86. if (!this.interceptor.implementsInterface(intf)) {
  87. throw new IllegalArgumentException("IntroductionInterceptor [" + this.interceptor + "] " +
  88. "does not implement interface '" + intf.getName() + "' specified in introduction advice");
  89. }
  90. }
  91. }
  92. }