1 22 package org.jboss.aop.deployment; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 32 import javassist.scopedpool.ScopedClassPoolFactory; 33 34 import javax.management.Attribute ; 35 import javax.management.AttributeNotFoundException ; 36 import javax.management.InstanceNotFoundException ; 37 import javax.management.InvalidAttributeValueException ; 38 import javax.management.MBeanException ; 39 import javax.management.Notification ; 40 import javax.management.ObjectName ; 41 import javax.management.ReflectionException ; 42 import org.jboss.aop.AspectManager; 43 import org.jboss.aop.AspectNotificationHandler; 44 import org.jboss.aop.AspectXmlLoader; 45 import org.jboss.aop.ClassLoaderValidation; 46 import org.jboss.aop.ClassicWeavingStrategy; 47 import org.jboss.aop.Deployment; 48 import org.jboss.aop.SuperClassesFirstWeavingStrategy; 49 import org.jboss.aop.hook.JDK14Transformer; 50 import org.jboss.aop.hook.JDK14TransformerManager; 51 import org.jboss.aop.instrument.InstrumentorFactory; 52 import org.jboss.aop.instrument.TransformerCommon; 53 import org.jboss.mx.server.ServerConstants; 54 import org.jboss.mx.util.ObjectNameFactory; 55 import org.jboss.system.ServiceMBeanSupport; 56 import org.jboss.system.server.ServerConfig; 57 58 63 public class AspectManagerService 64 extends ServiceMBeanSupport 65 implements AspectManagerServiceMBean, AspectNotificationHandler 66 { 67 68 static { 69 TransformerCommon common = new TransformerCommon(); 73 SuperClassesFirstWeavingStrategy strategy1 = new SuperClassesFirstWeavingStrategy(); 74 ClassicWeavingStrategy strategy2 = new ClassicWeavingStrategy(); 75 76 } 77 78 public static final ObjectName DEFAULT_LOADER_REPOSITORY = ObjectNameFactory.create(ServerConstants.DEFAULT_LOADER_NAME); 79 80 82 boolean created = false; 83 protected File tmpClassesDir; 84 protected boolean enableTransformer = false; 85 protected boolean enableLoadtimeWeaving = false; 86 protected boolean suppressTransformationErrors = true; 87 protected boolean suppressReferenceErrors = true; 88 protected String exclude; 89 protected String include; 90 protected String ignore; 91 private String baseXml = "base-aop.xml"; 92 93 95 public AspectManagerService() 97 { 98 } 99 100 public AspectManagerService(String baseXml) 101 { 102 this.baseXml = baseXml; 103 } 104 105 107 protected ScopedClassPoolFactory createFactory() throws Exception 108 { 109 return new JBossClassPoolFactory(tmpClassesDir); 110 } 111 112 protected ClassLoaderValidation createClassLoaderValidation() 113 { 114 return new JBossClassLoaderValidator(); 115 } 116 117 protected void createService() 118 throws Exception 119 { 120 if (tmpClassesDir == null) 122 { 123 String jbossTmpDir = System.getProperty(ServerConfig.SERVER_TEMP_DIR); 124 if (jbossTmpDir == null) 125 jbossTmpDir = System.getProperty("java.io.tmpdir"); 126 tmpClassesDir = new File (jbossTmpDir, "aopdynclasses"); 127 } 128 if (tmpClassesDir.exists() == false && tmpClassesDir.mkdirs() == false) 130 throw new FileNotFoundException ("Failed to create tmpClassesDir: " + tmpClassesDir.getAbsolutePath()); 131 AspectManager.setClassPoolFactory(createFactory()); 132 133 AspectManager.classLoaderValidator = createClassLoaderValidation(); 134 136 Deployment.searchClasspath = false; AspectManager.suppressTransformationErrors = suppressTransformationErrors; 138 if (enableTransformer && enableLoadtimeWeaving) throw new RuntimeException ("Cannot set both EnableTransformer and EnableLoadtimeWeaving"); 139 if (enableTransformer) 140 { 141 attachDeprecatedTranslator(); 142 } 143 if (enableLoadtimeWeaving) 144 { 145 attachTranslator(); 146 } 147 created = true; 148 AspectManager.notificationHandler = this; 149 150 AspectManager.scopedCLHelper = new JBossScopedClassLoaderHelper(); 151 152 baseAop(); 153 } 154 155 protected void baseAop() 156 { 157 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 158 URL base = cl.getResource(baseXml); 159 try 160 { 161 if (base != null) 162 { 163 AspectXmlLoader.deployXML(base); 164 } 165 else 166 { 167 System.out.println("Could not find " + baseXml + " file in the resources of " + cl); 168 } 169 } 170 catch (Exception e) 171 { 172 System.out.println("Error loading " + baseXml + " file" + e); 173 } 174 } 175 176 protected void attachDeprecatedTranslator() 177 { 178 log.warn("EnableTransformer has been deprecated, please use EnableLoadtimeWeaving. See docs for more details"); 179 AspectManager mgr = AspectManager.instance(); 180 try 181 { 182 server.setAttribute(DEFAULT_LOADER_REPOSITORY, new Attribute ("Translator", mgr)); 183 } 184 catch (InstanceNotFoundException e) 185 { 186 throw new RuntimeException (e); 187 } 188 catch (AttributeNotFoundException e) 189 { 190 throw new RuntimeException (e); 191 } 192 catch (InvalidAttributeValueException e) 193 { 194 throw new RuntimeException (e); 195 } 196 catch (MBeanException e) 197 { 198 throw new RuntimeException (e); 199 } 200 catch (ReflectionException e) 201 { 202 throw new RuntimeException (e); 203 } 204 } 205 206 protected void detachDeprecatedTranslator() 207 { 208 try 209 { 210 server.setAttribute(DEFAULT_LOADER_REPOSITORY, new Attribute ("Translator", null)); 211 } 212 catch (InstanceNotFoundException e) 213 { 214 throw new RuntimeException (e); 215 } 216 catch (AttributeNotFoundException e) 217 { 218 throw new RuntimeException (e); 219 } 220 catch (InvalidAttributeValueException e) 221 { 222 throw new RuntimeException (e); 223 } 224 catch (MBeanException e) 225 { 226 throw new RuntimeException (e); 227 } 228 catch (ReflectionException e) 229 { 230 throw new RuntimeException (e); 231 } 232 } 233 234 protected void attachTranslator() 235 { 236 JDK14TransformerManager.transformer = new JDK14Transformer() 237 { 238 public byte[] transform(ClassLoader loader, String classname, byte[] classBytes) 239 { 240 try 241 { 242 return AspectManager.instance(loader).translate(classname, loader, classBytes); 244 } 245 catch (Exception e) 246 { 247 throw new RuntimeException (e); 248 } 249 } 250 }; 251 } 252 253 protected void detachTranslator() 254 { 255 JDK14TransformerManager.transformer = null; 256 } 257 258 public void attachClass(String classname) 259 { 260 Notification msg = new Notification ("AOP class attached", this, getNextNotificationSequenceNumber()); 261 msg.setUserData(classname); 262 sendNotification(msg); 263 } 264 265 protected void startService() 266 throws Exception 267 { 268 } 269 270 protected void stopService() 271 { 272 } 273 274 public boolean getPrune() 275 { 276 return AspectManager.getPrune(); 277 } 278 279 public void setPrune(boolean prune) 280 { 281 AspectManager.setPrune(prune); 282 } 283 284 public String getExclude() 285 { 286 return exclude; 287 } 288 289 public void setExclude(String exclude) 290 { 291 this.exclude = exclude; 292 ArrayList list = new ArrayList (); 293 StringTokenizer tokenizer = new StringTokenizer (exclude, ","); 294 while (tokenizer.hasMoreTokens()) 295 { 296 list.add(tokenizer.nextToken().trim()); 297 } 298 AspectManager.instance().setExclude(list); 299 } 300 301 public String getInclude() 302 { 303 return include; 304 } 305 306 public void setInclude(String include) 307 { 308 this.include = include; 309 ArrayList list = new ArrayList (); 310 StringTokenizer tokenizer = new StringTokenizer (include, ","); 311 while (tokenizer.hasMoreTokens()) 312 { 313 list.add(tokenizer.nextToken().trim()); 314 } 315 AspectManager.instance().setInclude(list); 316 } 317 318 public String getIgnore() 319 { 320 return ignore; 321 } 322 323 public void setIgnore(String ignore) 324 { 325 this.ignore = ignore; 326 ArrayList list = new ArrayList (); 327 StringTokenizer tokenizer = new StringTokenizer (ignore, ","); 328 while (tokenizer.hasMoreTokens()) 329 { 330 list.add(tokenizer.nextToken().trim()); 331 } 332 AspectManager.instance().setIgnore(list); 333 } 334 335 336 341 public File getTmpClassesDir() 342 { 343 return tmpClassesDir; 344 } 345 346 351 public void setTmpClassesDir(File tmpClassesDir) 352 { 353 this.tmpClassesDir = tmpClassesDir; 354 } 355 356 361 public boolean getVerbose() 362 { 363 return AspectManager.verbose; 364 } 365 366 371 public void setVerbose(boolean verbose) 372 { 373 AspectManager.verbose = verbose; 374 } 375 376 381 public boolean getOptimized() 382 { 383 return AspectManager.optimize; 384 } 385 386 391 public void setOptimized(boolean verbose) 392 { 393 AspectManager.optimize = verbose; 394 } 395 396 399 public boolean getSuppressTransformationErrors() 400 { 401 return suppressTransformationErrors; 402 } 403 404 407 public void setSuppressTransformationErrors(boolean suppressTransformationErrors) 408 { 409 this.suppressTransformationErrors = suppressTransformationErrors; 410 AspectManager.suppressTransformationErrors = suppressTransformationErrors; 411 } 412 413 416 public boolean getSuppressReferenceErrors() 417 { 418 return suppressReferenceErrors; 419 } 420 421 424 public void setSuppressReferenceErrors(boolean suppressReferenceErrors) 425 { 426 this.suppressReferenceErrors = suppressReferenceErrors; 427 AspectManager.suppressReferenceErrors = suppressReferenceErrors; 428 } 429 430 435 public boolean getEnableTransformer() 436 { 437 return enableTransformer; 438 } 439 440 445 public String interceptorFactories() 446 { 447 Map factories = AspectManager.instance().getInterceptorFactories(); 448 Iterator it = factories.keySet().iterator(); 449 StringBuffer buffer = new StringBuffer (""); 450 while (it.hasNext()) 451 { 452 buffer.append(it.next() + "<br>"); 453 } 454 return buffer.toString(); 455 } 456 457 462 public String aspectDefinitions() 463 { 464 Map factories = AspectManager.instance().getAspectDefinitions(); 465 Iterator it = factories.keySet().iterator(); 466 StringBuffer buffer = new StringBuffer (""); 467 while (it.hasNext()) 468 { 469 buffer.append(it.next() + "<br>"); 470 } 471 return buffer.toString(); 472 } 473 474 477 public String introductions() 478 { 479 Map factories = AspectManager.instance().getInterfaceIntroductions(); 480 Iterator it = factories.keySet().iterator(); 481 StringBuffer buffer = new StringBuffer (""); 482 while (it.hasNext()) 483 { 484 buffer.append(it.next() + "<br>"); 485 } 486 return buffer.toString(); 487 } 488 489 494 public String stacks() 495 { 496 Map factories = AspectManager.instance().getInterceptorStacks(); 497 Iterator it = factories.keySet().iterator(); 498 StringBuffer buffer = new StringBuffer (""); 499 while (it.hasNext()) 500 { 501 buffer.append(it.next() + "<br>"); 502 } 503 return buffer.toString(); 504 } 505 506 511 public String bindings() 512 { 513 Map factories = AspectManager.instance().getBindings(); 514 Iterator it = factories.keySet().iterator(); 515 StringBuffer buffer = new StringBuffer (""); 516 while (it.hasNext()) 517 { 518 buffer.append(it.next() + "<br>"); 519 } 520 return buffer.toString(); 521 } 522 523 528 public String registeredClassLoaders() 529 { 530 Map factories = AspectManager.instance().getRegisteredCLs(); 531 Iterator it = factories.keySet().iterator(); 532 StringBuffer buffer = new StringBuffer (""); 533 while (it.hasNext()) 534 { 535 buffer.append(it.next() + "<br>"); 536 } 537 return buffer.toString(); 538 } 539 540 545 public void setEnableTransformer(boolean enableTransformer) 546 { 547 549 if (enableLoadtimeWeaving) 550 { 551 log.warn("enabledLoadtimeWeaving alread set"); 552 return; 553 } 554 if (this.enableTransformer == enableTransformer) return; 555 if (this.getState() == STARTED) 556 { 557 if (enableTransformer) 558 { 559 attachDeprecatedTranslator(); 560 } 561 else 562 { 563 detachDeprecatedTranslator(); 564 } 565 } 566 this.enableTransformer = enableTransformer; 567 } 568 569 public boolean getEnableLoadtimeWeaving() 570 { 571 return enableLoadtimeWeaving; 572 } 573 574 579 public void setEnableLoadtimeWeaving(boolean enableTransformer) 580 { 581 if (this.enableLoadtimeWeaving == enableTransformer) return; 582 if (this.getState() == STARTED) 583 { 584 if (enableTransformer) 585 { 586 attachTranslator(); 587 } 588 else 589 { 590 detachTranslator(); 591 } 592 } 593 this.enableLoadtimeWeaving = enableTransformer; 594 } 595 596 public String getInstrumentor() 597 { 598 return InstrumentorFactory.getInstrumentorName(); 599 } 600 601 public void setInstrumentor(String instrumentor) 602 { 603 InstrumentorFactory.initialise(instrumentor); 604 } 605 606 } 607 | Popular Tags |