1 16 17 package org.springframework.aop.aspectj.autoproxy; 18 19 import java.util.Comparator ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 24 import org.aopalliance.aop.Advice; 25 import org.aspectj.util.PartialOrder; 26 import org.aspectj.util.PartialOrder.PartialComparable; 27 28 import org.springframework.aop.Advisor; 29 import org.springframework.aop.aspectj.AbstractAspectJAdvice; 30 import org.springframework.aop.aspectj.AspectJProxyUtils; 31 import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator; 32 import org.springframework.aop.interceptor.ExposeInvocationInterceptor; 33 import org.springframework.core.Ordered; 34 import org.springframework.util.ClassUtils; 35 36 45 public class AspectJAwareAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator { 46 47 private static final Comparator DEFAULT_PRECEDENCE_COMPARATOR = new AspectJPrecedenceComparator(); 48 49 50 65 protected List sortAdvisors(List advisors) { 66 List partiallyComparableAdvisors = new LinkedList (); 68 for (Iterator it = advisors.iterator(); it.hasNext();) { 69 Advisor element = (Advisor) it.next(); 70 PartiallyComparableAdvisorHolder advisor = 71 new PartiallyComparableAdvisorHolder(element, DEFAULT_PRECEDENCE_COMPARATOR); 72 partiallyComparableAdvisors.add(advisor); 73 } 74 75 List sorted = PartialOrder.sort(partiallyComparableAdvisors); 77 if (sorted == null) { 78 throw new IllegalArgumentException ("Advice precedence circularity error"); 80 } 81 82 List result = new LinkedList (); 84 for (Iterator it = sorted.iterator(); it.hasNext();) { 85 PartiallyComparableAdvisorHolder pcAdvisor = (PartiallyComparableAdvisorHolder) it.next(); 86 result.add(pcAdvisor.getAdvisor()); 87 } 88 89 return result; 90 } 91 92 97 protected void extendAdvisors(List candidateAdvisors) { 98 AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(candidateAdvisors); 99 } 100 101 102 105 private static class PartiallyComparableAdvisorHolder implements PartialComparable { 106 107 private final Advisor advisor; 108 109 private final Comparator comparator; 110 111 public PartiallyComparableAdvisorHolder(Advisor advisor, Comparator comparator) { 112 this.advisor = advisor; 113 this.comparator = comparator; 114 } 115 116 public int compareTo(Object obj) { 117 Advisor otherAdvisor = ((PartiallyComparableAdvisorHolder) obj).advisor; 118 return this.comparator.compare(this.advisor, otherAdvisor); 119 } 120 121 public int fallbackCompareTo(Object obj) { 122 return 0; 123 } 124 125 public Advisor getAdvisor() { 126 return this.advisor; 127 } 128 129 public String toString() { 130 StringBuffer sb = new StringBuffer (); 131 Advice advice = this.advisor.getAdvice(); 132 sb.append(ClassUtils.getShortName(advice.getClass())); 133 sb.append(": "); 134 if (this.advisor instanceof Ordered) { 135 sb.append("order " + ((Ordered) this.advisor).getOrder() + ", "); 136 } 137 if (advice instanceof AbstractAspectJAdvice) { 138 AbstractAspectJAdvice ajAdvice = (AbstractAspectJAdvice) advice; 139 sb.append(ajAdvice.getAspectName()); 140 sb.append(", declaration order "); 141 sb.append(ajAdvice.getDeclarationOrder()); 142 } 143 return sb.toString(); 144 } 145 } 146 147 } 148 | Popular Tags |