1 16 17 package org.springframework.aop.framework.adapter; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.aopalliance.aop.Advice; 23 import org.aopalliance.intercept.MethodInterceptor; 24 25 import org.springframework.aop.Advisor; 26 import org.springframework.aop.support.DefaultPointcutAdvisor; 27 28 38 public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry { 39 40 private final List adapters = new ArrayList (3); 41 42 43 46 public DefaultAdvisorAdapterRegistry() { 47 registerAdvisorAdapter(new MethodBeforeAdviceAdapter()); 48 registerAdvisorAdapter(new AfterReturningAdviceAdapter()); 49 registerAdvisorAdapter(new ThrowsAdviceAdapter()); 50 } 51 52 53 public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException { 54 if (adviceObject instanceof Advisor) { 55 return (Advisor) adviceObject; 56 } 57 if (!(adviceObject instanceof Advice)) { 58 throw new UnknownAdviceTypeException(adviceObject); 59 } 60 Advice advice = (Advice) adviceObject; 61 if (advice instanceof MethodInterceptor) { 62 return new DefaultPointcutAdvisor(advice); 64 } 65 for (int i = 0; i < this.adapters.size(); i++) { 66 AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i); 68 if (adapter.supportsAdvice(advice)) { 69 return new DefaultPointcutAdvisor(advice); 70 } 71 } 72 throw new UnknownAdviceTypeException(advice); 73 } 74 75 public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException { 76 List interceptors = new ArrayList (3); 77 Advice advice = advisor.getAdvice(); 78 if (advice instanceof MethodInterceptor) { 79 interceptors.add(advice); 80 } 81 for (int i = 0; i < this.adapters.size(); i++) { 82 AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i); 83 if (adapter.supportsAdvice(advice)) { 84 interceptors.add(adapter.getInterceptor(advisor)); 85 } 86 } 87 if (interceptors.isEmpty()) { 88 throw new UnknownAdviceTypeException(advisor.getAdvice()); 89 } 90 return (MethodInterceptor[]) interceptors.toArray(new MethodInterceptor[interceptors.size()]); 91 } 92 93 public void registerAdvisorAdapter(AdvisorAdapter adapter) { 94 this.adapters.add(adapter); 95 } 96 97 } 98 | Popular Tags |