1 22 package org.jboss.aop.advice; 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Comparator ; 27 import java.util.Iterator ; 28 import java.util.LinkedHashMap ; 29 30 import org.jboss.aop.AspectManager; 31 32 37 public class PrecedenceSorter 38 { 39 static Comparator interceptorComparator = new Comparator () 40 { 41 public int compare(Object objA, Object objB) 42 { 43 InterceptorEntry entryA = (InterceptorEntry)objA; 44 InterceptorEntry entryB = (InterceptorEntry)objB; 45 46 return entryA.precedenceOrder - entryB.precedenceOrder; 47 } 48 }; 49 50 static class InterceptorEntry 51 { 52 Interceptor interceptor; 53 GeneratedAdvisorInterceptor factoryWrapper; 54 int originalOrder; 55 int precedenceOrder = -1; 56 String classname; 57 String method; 58 59 InterceptorEntry(GeneratedAdvisorInterceptor factoryWrapper) 60 { 61 this.factoryWrapper = factoryWrapper; 62 classname = factoryWrapper.getName(); 63 64 InterceptorFactory ifac = factoryWrapper.getDelegate(); 65 66 if (ifac instanceof GenericInterceptorFactory) 67 { 68 classname = ((GenericInterceptorFactory)ifac).getClassName(); 70 } 71 else 72 { 73 AspectFactory af = factoryWrapper.getAspect().getFactory(); 74 classname = af.getName(); 75 } 76 77 if (ifac instanceof AdviceFactory) 78 { 79 method = ((AdviceFactory)ifac).getAdvice(); 80 } 81 } 82 83 84 InterceptorEntry(Interceptor interceptor) 85 { 86 this.interceptor = interceptor; 87 88 String interceptorName = null; 89 if (interceptor instanceof PerInstanceInterceptor) 90 { 91 PerInstanceInterceptor icptr = (PerInstanceInterceptor)interceptor; 92 interceptorName = icptr.getName(); 93 } 94 else if (interceptor instanceof PerJoinpointInterceptor) 95 { 96 PerJoinpointInterceptor icptr = (PerJoinpointInterceptor)interceptor; 97 interceptorName = icptr.getName(); 98 } 99 else if (interceptor instanceof CFlowInterceptor) 100 { 101 CFlowInterceptor icptr = (CFlowInterceptor)interceptor; 102 interceptorName = icptr.getName(); 103 } 104 else 105 { 106 interceptorName = interceptor.getClass().getName(); 107 } 108 109 try 110 { 111 boolean isAdvice = interceptorName.startsWith("org.jboss.aop.advice."); 112 if (isAdvice) 113 { 114 String name = interceptor.getName(); 115 int index = name.lastIndexOf("."); 116 classname = name.substring(0, index); 117 method = name.substring(index + 1); 118 } 119 else 120 { 121 classname = interceptorName; 122 } 123 } 124 catch (RuntimeException e) 125 { 126 System.err.print(interceptor.getName()); 127 throw e; 128 } 129 } 130 131 public String toString() 132 { 133 return "Entry: " + precedenceOrder + " (" + originalOrder + ")interceptorClass=" + classname + "; adviceMethod=" + method; 134 } 135 136 } 137 138 private static boolean matches(InterceptorEntry ientry, PrecedenceDefEntry pentry) 139 { 140 if (ientry.classname.equals(pentry.interceptorClass)) 141 { 142 if (ientry.method == null) 143 { 144 if (pentry.adviceMethod == null) 145 { 146 return true; 147 } 148 } 149 else if (pentry.adviceMethod != null) 150 { 151 return ientry.method.equals(pentry.adviceMethod); 154 } 155 } 156 return false; 157 } 158 159 160 public static PrecedenceDefEntry[] createOverallPrecedence(AspectManager manager) 161 { 162 ArrayList overall = new ArrayList (); 163 164 LinkedHashMap precedenceDefs = manager.getPrecedenceDefs(); 165 boolean first = true; 166 for (Iterator it = precedenceDefs.values().iterator() ; it.hasNext(); ) 167 { 168 PrecedenceDef precedenceDef = (PrecedenceDef)it.next(); 169 PrecedenceDefEntry[] entries = precedenceDef.getEntries(); 170 171 if (first) 172 { 173 for (int i = 0 ; i < entries.length ; i++) 175 { 176 overall.add(entries[i]); 177 } 178 first = false; 179 continue; 180 } 181 182 overall = mergePrecedenceDef(overall, precedenceDef); 183 } 184 return (PrecedenceDefEntry[])overall.toArray(new PrecedenceDefEntry[overall.size()]); 186 } 187 188 public static ArrayList mergePrecedenceDef(ArrayList overall, PrecedenceDef precedenceDef) 189 { 190 PrecedenceDefEntry[] entries = precedenceDef.getEntries(); 201 int start = 0, end = 0; 202 int size = overall.size(); 203 for (int i = 0 ; i < size ; i++) 204 { 205 PrecedenceDefEntry global = (PrecedenceDefEntry)overall.get(i); 206 boolean found = false; 207 208 for (int j = start ; j < entries.length ; j++) 210 { 211 PrecedenceDefEntry cur = entries[j]; 212 213 if (cur.equals(global)) 214 { 215 found = true; 216 end = j; 217 break; 218 } 219 } 220 221 if (found) 224 { 225 int insert = i; 226 for (int j = start ; j < end ; j++) 227 { 228 overall.add(insert++, entries[j]); 229 } 230 end++; 231 start = end; 232 } 233 } 234 235 for (int j = start ; j < entries.length ; j++) 236 { 237 overall.add(entries[j]); 238 } 239 240 return overall; 241 } 242 243 public static Interceptor[] applyPrecedence(Interceptor[] interceptors, AspectManager manager) 244 { 245 if (interceptors.length == 0) 246 return interceptors; 247 248 ArrayList all = new ArrayList (interceptors.length); 249 ArrayList precedence = new ArrayList (interceptors.length); 250 PrecedenceDefEntry[] precedenceEntries = manager.getSortedPrecedenceDefEntries(); 251 252 for (int i = 0 ; i < interceptors.length ; i++) 254 { 255 InterceptorEntry interceptorEntry = new InterceptorEntry(interceptors[i]); 256 all.add(interceptorEntry); 257 for (int j = 0 ; j < precedenceEntries.length ; j++) 258 { 259 if (matches(interceptorEntry, precedenceEntries[j])) 260 { 261 interceptorEntry.originalOrder = i; 263 interceptorEntry.precedenceOrder = j; 264 precedence.add(interceptorEntry); 265 break; 266 } 267 } 268 } 269 270 Collections.sort(precedence, interceptorComparator); 272 Interceptor[] sortedInterceptors = new Interceptor[interceptors.length]; 273 274 int prec = 0; 276 int allSize = all.size(); 277 int precedenceSize = precedence.size(); 278 279 for (int i = 0 ; i < allSize ; i++) 280 { 281 InterceptorEntry entry = (InterceptorEntry)all.get(i); 282 283 if (entry.precedenceOrder >= 0 && prec < precedenceSize) 284 { 285 entry = (InterceptorEntry)precedence.get(prec++); 286 } 287 sortedInterceptors[i] = entry.interceptor; 288 } 289 290 return sortedInterceptors; 291 } 292 293 public static GeneratedAdvisorInterceptor[] applyPrecedence(GeneratedAdvisorInterceptor[] interceptors, AspectManager manager) 294 { 295 ArrayList all = new ArrayList (interceptors.length); 296 ArrayList precedence = new ArrayList (interceptors.length); 297 PrecedenceDefEntry[] precedenceEntries = manager.getSortedPrecedenceDefEntries(); 298 299 for (int i = 0 ; i < interceptors.length ; i++) 301 { 302 InterceptorEntry interceptorEntry = new InterceptorEntry(interceptors[i]); 303 all.add(interceptorEntry); 304 for (int j = 0 ; j < precedenceEntries.length ; j++) 305 { 306 if (matches(interceptorEntry, precedenceEntries[j])) 307 { 308 interceptorEntry.originalOrder = i; 310 interceptorEntry.precedenceOrder = j; 311 precedence.add(interceptorEntry); 312 break; 313 } 314 } 315 } 316 317 Collections.sort(precedence, interceptorComparator); 319 GeneratedAdvisorInterceptor[] sortedInterceptors = new GeneratedAdvisorInterceptor[interceptors.length]; 320 321 int prec = 0; 323 int allSize = all.size(); 324 int precedenceSize = precedence.size(); 325 326 for (int i = 0 ; i < allSize ; i++) 327 { 328 InterceptorEntry entry = (InterceptorEntry)all.get(i); 329 330 if (entry.precedenceOrder >= 0 && prec < precedenceSize) 331 { 332 entry = (InterceptorEntry)precedence.get(prec++); 333 } 334 sortedInterceptors[i] = entry.factoryWrapper; 335 } 336 337 return sortedInterceptors; 338 } 339 340 374 } 375 | Popular Tags |