1 4 package com.tc.aspectwerkz.definition; 5 6 7 import java.net.MalformedURLException ; 8 import java.net.URL ; 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.Set ; 14 import java.util.WeakHashMap ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.io.File ; 18 19 import com.tc.aspectwerkz.transform.Properties; 20 21 30 public class SystemDefinitionContainer { 31 32 36 public static final Map s_classLoaderSystemDefinitions = new WeakHashMap (); 37 41 public static Map s_classLoaderHierarchicalSystemDefinitions = new WeakHashMap (); 42 43 47 public static final Map s_classLoaderDefinitionLocations = new WeakHashMap (); 48 49 52 public static final String URL_JVM_OPTION_SYSTEM = System.getProperty( 53 "aspectwerkz.definition.file", 54 "-Daspectwerkz.definition.file option is not used" 55 ); 56 57 60 public static final String AOP_META_INF_XML_FILE = "META-INF/aop_xml"; 61 62 65 public static final String AOP_WEB_INF_XML_FILE = "../aop.xml"; 66 67 public static final String WEB_WEB_INF_XML_FILE = "../web.xml"; 68 69 72 public static final String DEFAULT_SYSTEM = "default"; 73 76 public static final String DEFINITION_FILE = System.getProperty("aspectwerkz.definition.file", null); 77 80 public static final String DEFAULT_DEFINITION_FILE_NAME = "aspectwerkz.xml"; 81 82 87 private static boolean s_disableSystemWideDefinition = false; 88 89 private static final String VIRTUAL_SYSTEM_ID_PREFIX = "virtual_"; 90 91 96 private static void registerClassLoader(final ClassLoader loader) { 97 synchronized (s_classLoaderSystemDefinitions) { 98 if (s_classLoaderSystemDefinitions.containsKey(loader)) { 99 return; 100 } 101 102 104 if (loader == null) { 106 Set defaults = new HashSet (); 108 defaults.add(SystemDefinition.createVirtualDefinitionAt(loader)); 110 s_classLoaderSystemDefinitions.put(loader, defaults); 111 s_classLoaderDefinitionLocations.put(loader, new ArrayList ()); 112 113 return; 114 } 115 116 registerClassLoader(loader.getParent()); 118 119 try { 121 final Set definitions = new HashSet (); 122 final List locationOfDefinitions = new ArrayList (); 123 124 s_classLoaderSystemDefinitions.put(loader, definitions); 126 s_classLoaderDefinitionLocations.put(loader, locationOfDefinitions); 127 128 if ((loader == ClassLoader.getSystemClassLoader()) && !s_disableSystemWideDefinition) { 130 definitions.addAll(getDefaultDefinition(loader)); 132 locationOfDefinitions.add(new File (URL_JVM_OPTION_SYSTEM).toURL()); 133 } 134 135 160 SystemDefinition virtualDef = SystemDefinitionContainer.getVirtualDefinitionFor(loader); 162 if (virtualDef == null) { 163 definitions.add(SystemDefinition.createVirtualDefinitionAt(loader)); 164 } 165 166 printDeploymentInfoFor(loader); 167 } catch (Throwable t) { 168 t.printStackTrace(); 169 } 170 } 171 } 172 173 181 public static void deployDefinitions(final ClassLoader loader, final Set definitions) { 182 synchronized (s_classLoaderSystemDefinitions) { 183 184 registerClassLoader(loader); 186 187 189 flushHierarchicalSystemDefinitionsBelow(loader); 191 192 Set defs = (Set ) s_classLoaderSystemDefinitions.get(loader); 194 defs.addAll(definitions); 195 printDeploymentInfoFor(loader); 196 } 197 } 198 199 208 public static SystemDefinition getDefinitionFor(final ClassLoader loader, final String uuid) { 209 for (Iterator defs = getDefinitionsFor(loader).iterator(); defs.hasNext();) { 210 SystemDefinition def = (SystemDefinition) defs.next(); 211 if (def.getUuid().equals(uuid)) { 212 return def; 213 } 214 } 215 return null; 216 } 217 218 226 public static Set getDefinitionsFor(final ClassLoader loader) { 227 return getHierarchicalDefinitionsFor(loader); 228 } 229 230 238 public static Set getAllDefinitionsFor(final ClassLoader loader) { 239 registerClassLoader(loader); 241 return (Set ) s_classLoaderSystemDefinitions.get(loader); 242 } 243 244 254 public static SystemDefinition getVirtualDefinitionFor(final ClassLoader loader) { 255 return getDefinitionFor(loader, getVirtualDefinitionUuidFor(loader)); 257 } 258 259 265 public static String getVirtualDefinitionUuidFor(final ClassLoader loader) { 266 int hash = loader == null ? 0 : loader.hashCode(); 268 StringBuffer sb = new StringBuffer (VIRTUAL_SYSTEM_ID_PREFIX); 269 return sb.append(hash).toString(); 270 } 271 272 275 public static void disableSystemWideDefinition() { 276 synchronized (s_classLoaderSystemDefinitions) { 277 s_disableSystemWideDefinition = true; 278 } 279 } 280 281 286 public static void printDeploymentInfoFor(final ClassLoader loader) { 287 if (!Properties.PRINT_DEPLOYMENT_INFO) { 288 return; 289 } 290 291 StringBuffer dump = new StringBuffer ("******************************************************************"); 292 dump.append("\n* class loader = "); 293 294 if ((loader != null) && (loader.toString().length() < 120)) { 296 dump.append(loader.toString()).append("@").append(loader.hashCode()); 297 } else if (loader != null) { 298 dump.append(loader.getClass().getName()).append("@").append(loader.hashCode()); 299 } else { 300 dump.append("null"); 301 } 302 303 Set defs = (Set ) s_classLoaderSystemDefinitions.get(loader); 304 for (Iterator it = defs.iterator(); it.hasNext();) { 305 SystemDefinition def = (SystemDefinition) it.next(); 306 Collection aspects = def.getAspectDefinitions(); 307 dump.append("\n* system id = ").append(def.getUuid()); 308 dump.append(", ").append(aspects.size()).append(" aspects."); 309 for (Iterator it2 = aspects.iterator(); it2.hasNext();) { 310 AspectDefinition aspect = (AspectDefinition) it2.next(); 311 dump.append("\n* aspect: " + aspect.getClassName()); 312 } 313 } 314 for (Iterator it = ((List ) s_classLoaderDefinitionLocations.get(loader)).iterator(); it.hasNext();) { 315 dump.append("\n* ").append(it.next()); 316 } 317 dump.append("\n******************************************************************"); 318 System.out.println(dump.toString()); 319 } 320 321 328 public static boolean isChildOf(final ClassLoader loader, final ClassLoader parentLoader) { 329 if (loader == null) { 330 if (parentLoader == null) { 331 return true; 332 } else { 333 return false; 334 } 335 } else if (loader.equals(parentLoader)) { 336 return true; 337 } else { 338 return isChildOf(loader.getParent(), parentLoader); 339 } 340 } 341 342 343 353 private static Set getHierarchicalDefinitionsFor(final ClassLoader loader) { 354 synchronized (s_classLoaderSystemDefinitions) { 355 registerClassLoader(loader); 357 358 Set defs = new HashSet (); 359 s_classLoaderHierarchicalSystemDefinitions.put(loader, defs); 361 if (loader == null) { 362 } else { 364 ClassLoader parent = loader.getParent(); 365 defs.addAll(getHierarchicalDefinitionsFor(parent)); 366 } 367 defs.addAll((Set ) s_classLoaderSystemDefinitions.get(loader)); 368 369 return defs; 370 } 371 } 372 373 385 private static boolean isDefinedBy(final ClassLoader loader, final URL def) { 386 if (loader == null) { 387 return false; 388 } 389 ArrayList defLocation = (ArrayList ) s_classLoaderDefinitionLocations.get(loader); 390 if (defLocation != null) { 391 for (Iterator it = defLocation.iterator(); it.hasNext();) { 392 URL definedDef = (URL ) it.next(); 393 if (definedDef.sameFile(def)) { 394 return true; 395 } 396 } 397 } 398 return isDefinedBy(loader.getParent(), def); 399 } 400 401 private static void flushHierarchicalSystemDefinitionsBelow(final ClassLoader loader) { 402 Map classLoaderHierarchicalSystemDefinitions = new WeakHashMap (); 403 for (Iterator iterator = s_classLoaderHierarchicalSystemDefinitions.entrySet().iterator(); iterator.hasNext();) { 404 Map.Entry entry = (Map.Entry ) iterator.next(); 405 ClassLoader currentLoader = (ClassLoader ) entry.getKey(); 406 if (isChildOf(currentLoader, loader)) { 407 } else { 409 classLoaderHierarchicalSystemDefinitions.put(currentLoader, entry.getValue()); 410 } 411 } 412 s_classLoaderHierarchicalSystemDefinitions = classLoaderHierarchicalSystemDefinitions; 413 } 414 415 421 public static Set getDefaultDefinition(final ClassLoader loader) { 422 if (DEFINITION_FILE != null) { 423 File file = new File (DEFINITION_FILE); 424 if (file.canRead()) { 425 try { 426 return XmlParser.parseNoCache(loader, file.toURL()); 427 } catch (MalformedURLException e) { 428 System.err.println("<WARN> Cannot read " + DEFINITION_FILE); 429 e.printStackTrace(); 430 } 431 } else { 432 System.err.println("<WARN> Cannot read " + DEFINITION_FILE); 433 } 434 } 435 return new HashSet (); 436 } 437 438 } | Popular Tags |