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.adapter;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.aopalliance.aop.Advice;
  20. import org.aopalliance.intercept.Interceptor;
  21. import org.springframework.aop.Advisor;
  22. import org.springframework.aop.support.DefaultPointcutAdvisor;
  23. /**
  24. * Default implementation of the AdvisorAdapterRegistry interface.
  25. * @author Rod Johnson
  26. * @version $Id: DefaultAdvisorAdapterRegistry.java,v 1.11 2004/05/23 20:50:25 jhoeller Exp $
  27. */
  28. public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry {
  29. private final List adapters = new ArrayList();
  30. public DefaultAdvisorAdapterRegistry() {
  31. // register well-known adapters
  32. registerAdvisorAdapter(new BeforeAdviceAdapter());
  33. registerAdvisorAdapter(new AfterReturningAdviceAdapter());
  34. registerAdvisorAdapter(new ThrowsAdviceAdapter());
  35. }
  36. public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
  37. if (adviceObject instanceof Advisor) {
  38. return (Advisor) adviceObject;
  39. }
  40. if (!(adviceObject instanceof Advice)) {
  41. throw new UnknownAdviceTypeException(adviceObject);
  42. }
  43. Advice advice = (Advice) adviceObject;
  44. if (advice instanceof Interceptor) {
  45. // So well-known it doesn't even need an adapter
  46. return new DefaultPointcutAdvisor(advice);
  47. }
  48. for (int i = 0; i < this.adapters.size(); i++) {
  49. // Check that it is supported
  50. AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i);
  51. if (adapter.supportsAdvice(advice)) {
  52. return new DefaultPointcutAdvisor(advice);
  53. }
  54. }
  55. throw new UnknownAdviceTypeException(advice);
  56. }
  57. public Interceptor getInterceptor(Advisor advisor) throws UnknownAdviceTypeException {
  58. Advice advice = advisor.getAdvice();
  59. if (advice instanceof Interceptor) {
  60. return (Interceptor) advice;
  61. }
  62. for (int i = 0; i < this.adapters.size(); i++) {
  63. AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i);
  64. if (adapter.supportsAdvice(advice)) {
  65. return adapter.getInterceptor(advisor);
  66. }
  67. }
  68. throw new UnknownAdviceTypeException(advisor.getAdvice());
  69. }
  70. public void registerAdvisorAdapter(AdvisorAdapter adapter) {
  71. this.adapters.add(adapter);
  72. }
  73. }