1 8 package org.codehaus.aspectwerkz.definition; 9 10 import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor; 11 12 import java.net.URL ; 13 import java.util.ArrayList ; 14 import java.util.Enumeration ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 import java.util.WeakHashMap ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.io.File ; 22 23 31 public class SystemDefinitionContainer { 32 33 37 public static final Map s_classLoaderSystemDefinitions = new WeakHashMap (); 38 42 public static Map s_classLoaderHierarchicalSystemDefinitions = new WeakHashMap (); 43 44 48 public static final Map s_classLoaderDefinitionLocations = new WeakHashMap (); 49 50 53 public static final String URL_JVM_OPTION_SYSTEM = System.getProperty( 54 "aspectwerkz.definition.file", 55 "no -Daspectwerkz.definition.file" 56 ); 57 58 61 public static final String AOP_META_INF_XML_FILE = "META-INF/aop.xml"; 62 63 66 public static final String AOP_WEB_INF_XML_FILE = "../aop.xml"; 67 68 public static final String WEB_WEB_INF_XML_FILE = "../web.xml"; 69 70 75 private static boolean s_disableSystemWideDefinition = false; 76 77 private static final String VIRTUAL_SYSTEM_ID_PREFIX = "virtual_"; 78 79 84 private static void registerClassLoader(final ClassLoader loader) { 85 synchronized (s_classLoaderSystemDefinitions) { 86 if (s_classLoaderSystemDefinitions.containsKey(loader)) { 87 return; 88 } 89 90 92 if (loader == null) { 94 Set defaults = new HashSet (); 96 defaults.add(SystemDefinition.createVirtualDefinitionAt(loader)); 97 s_classLoaderSystemDefinitions.put(loader, defaults); 98 s_classLoaderDefinitionLocations.put(loader, new ArrayList ()); 99 100 return; 101 } 102 103 registerClassLoader(loader.getParent()); 105 106 try { 108 final Set definitions = new HashSet (); 109 final List locationOfDefinitions = new ArrayList (); 110 111 s_classLoaderSystemDefinitions.put(loader, definitions); 113 s_classLoaderDefinitionLocations.put(loader, locationOfDefinitions); 114 115 if ((loader == ClassLoader.getSystemClassLoader()) && !s_disableSystemWideDefinition) { 117 definitions.addAll(DefinitionLoader.getDefaultDefinition(loader)); 119 locationOfDefinitions.add(new File (URL_JVM_OPTION_SYSTEM).toURL()); 120 } 121 if (loader.getResource(WEB_WEB_INF_XML_FILE) != null) { 122 Enumeration webres = loader.getResources(AOP_WEB_INF_XML_FILE); 123 while (webres.hasMoreElements()) { 124 URL def = (URL ) webres.nextElement(); 125 if (isDefinedBy(loader, def)) { 126 ; 127 } else { 128 definitions.addAll(XmlParser.parseNoCache(loader, def)); 129 locationOfDefinitions.add(def); 130 } 131 } 132 } 133 Enumeration res = loader.getResources(AOP_META_INF_XML_FILE); 134 while (res.hasMoreElements()) { 135 URL def = (URL ) res.nextElement(); 136 if (isDefinedBy(loader, def)) { 137 ; 138 } else { 139 definitions.addAll(XmlParser.parseNoCache(loader, def)); 140 locationOfDefinitions.add(def); 141 } 142 } 143 144 definitions.add(SystemDefinition.createVirtualDefinitionAt(loader)); 146 147 dump(loader); 148 } catch (Throwable t) { 149 t.printStackTrace(); 150 } 151 } 152 } 153 154 162 public static void deployDefinitions(final ClassLoader loader, final Set definitions) { 163 synchronized (s_classLoaderSystemDefinitions) { 164 165 registerClassLoader(loader); 167 168 170 flushHierarchicalSystemDefinitionsBelow(loader); 172 173 Set defs = (Set ) s_classLoaderSystemDefinitions.get(loader); 175 defs.addAll(definitions); 176 dump(loader); 177 } 178 } 179 180 private static void flushHierarchicalSystemDefinitionsBelow(ClassLoader loader) { 181 Map classLoaderHierarchicalSystemDefinitions = new WeakHashMap (); 184 for (Iterator iterator = s_classLoaderHierarchicalSystemDefinitions.entrySet().iterator(); iterator.hasNext();) { 185 Map.Entry entry = (Map.Entry ) iterator.next(); 186 ClassLoader currentLoader = (ClassLoader ) entry.getKey(); 187 if (isChildOf(currentLoader, loader)) { 188 ; } else { 190 classLoaderHierarchicalSystemDefinitions.put(currentLoader, entry.getValue()); 191 } 192 } 193 s_classLoaderHierarchicalSystemDefinitions = classLoaderHierarchicalSystemDefinitions; 194 } 196 197 206 public static SystemDefinition getDefinitionFor(final ClassLoader loader, final String uuid) { 207 for (Iterator defs = getDefinitionsFor(loader).iterator(); defs.hasNext();) { 208 SystemDefinition def = (SystemDefinition) defs.next(); 209 if (def.getUuid().equals(uuid)) { 210 return def; 211 } 212 } 213 return null; 214 } 215 216 224 public static Set getDefinitionsFor(final ClassLoader loader) { 225 return getHierarchicalDefinitionsFor(loader); 226 } 227 228 236 public static Set getDefinitionsAt(final ClassLoader loader) { 237 registerClassLoader(loader); 239 return (Set ) s_classLoaderSystemDefinitions.get(loader); 240 } 241 242 255 265 public static SystemDefinition getVirtualDefinitionAt(final ClassLoader loader) { 266 return getDefinitionFor(loader, getVirtualDefinitionUuid(loader)); 268 } 269 270 276 public static String getVirtualDefinitionUuid(ClassLoader loader) { 277 int hash = loader == null ? 0 : loader.hashCode(); 279 StringBuffer sb = new StringBuffer (VIRTUAL_SYSTEM_ID_PREFIX); 280 return sb.append(hash).toString(); 281 } 282 283 293 296 public static void disableSystemWideDefinition() { 297 s_disableSystemWideDefinition = true; 298 } 299 300 310 private static Set getHierarchicalDefinitionsFor(final ClassLoader loader) { 311 synchronized (s_classLoaderSystemDefinitions) { 312 if (s_classLoaderHierarchicalSystemDefinitions.containsKey(loader)) { 314 return (Set ) s_classLoaderHierarchicalSystemDefinitions.get(loader); 315 } else { 316 registerClassLoader(loader); 318 319 Set defs = new HashSet (); 320 s_classLoaderHierarchicalSystemDefinitions.put(loader, defs); 322 if (loader == null) { 323 ; } else { 325 ClassLoader parent = loader.getParent(); 326 defs.addAll(getHierarchicalDefinitionsFor(parent)); 327 } 328 defs.addAll((Set ) s_classLoaderSystemDefinitions.get(loader)); 329 330 return defs; 331 } 332 } 333 } 334 335 347 private static boolean isDefinedBy(final ClassLoader loader, final URL def) { 348 if (loader == null) { 349 return false; 350 } 351 ArrayList defLocation = (ArrayList ) s_classLoaderDefinitionLocations.get(loader); 352 if (defLocation != null) { 353 for (Iterator it = defLocation.iterator(); it.hasNext();) { 354 URL definedDef = (URL ) it.next(); 355 if (definedDef.sameFile(def)) { 356 return true; 357 } 358 } 359 } 360 return isDefinedBy(loader.getParent(), def); 361 } 362 363 368 private static void dump(final ClassLoader loader) { 369 if (!AspectWerkzPreProcessor.VERBOSE) { 370 return; 371 } 372 373 StringBuffer dump = new StringBuffer ("******************************************************************"); 374 dump.append("\n* ClassLoader = "); 375 376 if ((loader != null) && (loader.toString().length() < 120)) { 378 dump.append(loader.toString()).append("@").append(loader.hashCode()); 379 } else if (loader != null) { 380 dump.append(loader.getClass().getName()).append("@").append(loader.hashCode()); 381 } else { 382 dump.append("null"); 383 } 384 385 Set defs = (Set ) s_classLoaderSystemDefinitions.get(loader); 386 for (Iterator it = defs.iterator(); it.hasNext();) { 387 SystemDefinition def = (SystemDefinition) it.next(); 388 dump.append("\n* SystemID = ").append(def.getUuid()); 389 dump.append(", ").append(def.getAspectDefinitions().size()).append(" aspects."); 390 } 391 for (Iterator it = ((List ) s_classLoaderDefinitionLocations.get(loader)).iterator(); it.hasNext();) { 392 dump.append("\n* ").append(it.next()); 393 } 394 dump.append("\n******************************************************************"); 395 System.out.println(dump.toString()); 396 } 397 398 405 private static boolean isChildOf(ClassLoader loader, ClassLoader parentLoader) { 406 if (loader == null) { 407 if (parentLoader == null) { 408 return true; 409 } else { 410 return false; 411 } 412 } else if (loader.equals(parentLoader)) { 413 return true; 414 } else { 415 return isChildOf(loader.getParent(), parentLoader); 416 } 417 } 418 } | Popular Tags |