1 18 19 package org.objectweb.jac.core; 20 21 import java.util.*; 22 import org.apache.log4j.Logger; 23 import org.objectweb.jac.core.rtti.*; 24 import org.objectweb.jac.util.*; 25 26 29 30 41 42 public class ACManager extends OrderedRepository 43 implements BaseProgramListener 44 { 45 static Logger logger = Logger.getLogger("jac"); 46 static Logger loggerAspects = Logger.getLogger("aspects"); 47 static Logger loggerACM = Logger.getLogger("acm"); 48 static Logger loggerWuni = Logger.getLogger("wuni"); 49 50 52 53 public boolean registering = false; 54 55 67 68 public static Repository get() { 69 if ( acManager == null ) { 70 acManager = new ACManager(); 71 } 72 return acManager; 73 } 74 75 public static ACManager getACM() { 76 return acManager; 77 } 78 79 public AspectComponent[] getAspectComponents() { 80 return (AspectComponent[])objects.values().toArray(new AspectComponent[0]); 81 } 82 83 84 protected static ACManager acManager = null; 85 86 87 protected Hashtable declaredACs = (Hashtable) JacPropLoader.declaredACs; 88 89 static { 90 Runtime.getRuntime().addShutdownHook( 91 new Thread () { 92 public void run() { 93 if (acManager!=null) { 94 logger.info("JAC system shutdown: notifying all ACs..."); 95 acManager.onExit(); 96 } 97 logger.info("Bye bye."); 98 } 99 } 100 ); 101 } 103 104 110 111 public void declareAC(String name, String path) { 112 loggerAspects.debug("declaring "+path+" as "+name); 113 declaredACs.put(name, path); 114 } 115 116 public boolean isACDeclared(String name) { 117 if (name==null) return false; 118 return (declaredACs.get(name) != null); 119 } 120 121 126 127 public String getACPathFromName(String acName) { 128 return (String )declaredACs.get(acName); 129 } 130 131 135 136 public Set getDeclaredACs() { 137 return declaredACs.keySet(); 138 } 139 140 145 146 public AspectComponent registerDeclaredAC(String name) { 147 AspectComponent ac = null; 148 try { 149 if ( !acManager.isRegistered(name) ) { 150 String className = getACPathFromName(name); 151 loggerAspects.debug("registering the "+name+" ac"); 152 acManager.register( 153 name, ac = (AspectComponent) Class.forName(className).newInstance()); 154 } else { 155 loggerAspects.debug(name+" is already registered"); 156 } 157 } catch (Exception e) { 158 e.printStackTrace(); 159 } 160 return ac; 161 } 162 163 166 167 public static void main(String [] args) throws Throwable { 168 169 ACManager acManager = (ACManager)get(); 170 171 loggerAspects.debug("initializing the AC manager"); 172 173 try { 174 175 AspectComponent ac = null; 178 try { 179 loggerAspects.debug("compositionAspect = "+ 180 JacPropLoader.compositionAspect); 181 loggerAspects.debug("JacPropLoader = "+ 182 Strings.hex(JacPropLoader.class)); 183 JacPropLoader.loadProps(); 184 ac = (AspectComponent) Class.forName( 185 JacPropLoader.compositionAspect ).newInstance(); 186 } catch (Exception e) { 187 e.printStackTrace(); 188 System.exit(1); 189 } 190 acManager.register("JAC_composition_aspect", ac); 191 192 193 194 acManager.registerDeclaredAC("naming"); 195 acManager.registerDeclaredAC("binding"); 196 acManager.registerDeclaredAC("rtti"); 197 199 } catch (Exception e) { 200 e.printStackTrace(); 201 } 202 203 } 204 205 209 public AspectComponent getAC(String name) { 210 String appName = Collaboration.get().getCurApp(); 211 if (appName==null) { 212 loggerAspects.error("getAC("+name+") cannot work because curApp==null"); 213 } 214 return (AspectComponent)getObject(appName+"."+name); 215 } 216 217 221 public AspectComponent getACFromFullName(String fullName) { 222 return (AspectComponent)getObject(fullName); 223 } 224 225 231 232 public ACManager () { 233 } 234 235 242 243 public final void whenRemoteInstantiation(Wrappee newInstance, 244 String name) { 245 System.out.println("------ REMINST "+name+"-> "+newInstance); 246 Iterator it = orderedObjects.iterator(); 247 while (it.hasNext()) { 248 AspectComponent curac = (AspectComponent)it.next(); 249 curac.whenRemoteInstantiation(newInstance, name); 250 } 251 } 252 253 260 261 public final void whenUsingNewInstance(Interaction interaction) { 262 loggerWuni.debug("whenUsingNewInstance "+Strings.hex(interaction.wrappee)); 263 try { 266 ClassItem cli = interaction.getClassItem(); 267 boolean isJacClass = cli.getName().startsWith("org.objectweb.jac.core"); 268 Vector toNotify = (Vector)orderedObjects.clone(); 269 for (int i=0; i<toNotify.size(); i++) { 270 AspectComponent curac = (AspectComponent)toNotify.get(i); 271 loggerAspects.debug("curac = "+names.get(curac)); 272 if ((!isJacClass) || curac.isSystemListener()) { 274 loggerWuni.debug(" wuni "+curac.getName()); 275 curac.whenUsingNewInstance(interaction); 276 } 277 } 278 } catch (Exception e) { 279 loggerWuni.error("ACManager.whenUsingNewInstance "+interaction,e); 280 281 } 286 } 287 288 HashSet initializedClasses = new HashSet(); 289 public final synchronized void whenUsingNewClass(ClassItem cl) { 290 if (!initializedClasses.contains(cl)) { 291 loggerAspects.debug("whenUsingNewClass "+cl); 292 try { 293 Vector toNotify = (Vector)orderedObjects.clone(); 294 for (int i=0; i<toNotify.size(); i++) { 295 AspectComponent curac = (AspectComponent)toNotify.get(i); 296 loggerAspects.debug("curac = "+names.get(curac)); 297 curac.whenUsingNewClass(cl); 298 } 299 } catch ( Exception e ) { 300 e.printStackTrace(); 301 } 302 initializedClasses.add(cl); 303 } 304 } 305 306 317 318 public final Wrappee whenSerialized(Wrappee orgObject, 319 SerializedJacObject finalObject) { 320 Iterator it = orderedObjects.iterator(); 321 while (it.hasNext()) { 322 AspectComponent curac = (AspectComponent)it.next(); 323 orgObject = curac.whenSerialized(orgObject,finalObject); 324 } 325 return orgObject; 326 } 327 328 339 340 public final Wrappee whenDeserialized(SerializedJacObject orgObject, 341 Wrappee finalObject) { 342 Iterator it = orderedObjects.iterator(); 343 while (it.hasNext()) { 344 AspectComponent curac = (AspectComponent)it.next(); 345 finalObject = curac.whenDeserialized(orgObject,finalObject); 346 } 347 return finalObject; 348 } 349 350 355 356 public final void simulateUsingNewInstance(Wrappee wrappee) { 357 if (wrappee==null) 358 return; 359 360 Iterator it=((Vector)orderedObjects.clone()).iterator(); 361 while(it.hasNext()) { 362 AspectComponent curac = (AspectComponent)it.next(); 363 curac.simulateUsingNewInstance(wrappee); 364 } 365 } 366 367 375 376 public final void whenClone(Wrappee cloned, Wrappee clone) { 377 Object [] acs = orderedObjects.toArray(); 378 for ( int i=0; i<acs.length; i++) { 379 AspectComponent curac = (AspectComponent)acs[i]; 380 curac.whenClone(cloned, clone); 381 } 382 } 383 384 public final void whenDeleted(Wrappee object) { 385 Object [] acs = orderedObjects.toArray(); 386 for (int i=0; i<acs.length; i++) { 387 AspectComponent curac = (AspectComponent)acs[i]; 388 curac.whenDeleted(object); 389 } 390 } 391 392 public final void whenFree(Wrappee object) { 393 Object [] acs = orderedObjects.toArray(); 394 for (int i=0; i<acs.length; i++) { 395 AspectComponent curac = (AspectComponent)acs[i]; 396 curac.whenFree(object); 397 } 398 } 399 400 405 406 public final void afterApplicationStarted() { 407 Object [] acs = orderedObjects.toArray(); 408 for ( int i = 0; i < acs.length; i++ ) { 409 AspectComponent curac = (AspectComponent)acs[i]; 410 curac.afterApplicationStarted(); 411 } 412 } 413 414 public final void onExit() { 415 Object [] acs = orderedObjects.toArray(); 416 for ( int i = 0; i < acs.length; i++ ) { 417 AspectComponent curac = (AspectComponent)acs[i]; 418 curac.onExit(); 419 } 420 } 421 422 423 428 429 public final void whenTopologyChanged() { 430 Object [] acs = orderedObjects.toArray(); 431 for ( int i = 0; i < acs.length; i++ ) { 432 AspectComponent curac = (AspectComponent)acs[i]; 433 curac.whenTopologyChanged(); 434 } 435 } 436 437 442 443 public final void whenCloseDisplay(Display display) { 444 Object [] acs = orderedObjects.toArray(); 445 for ( int i = 0; i < acs.length; i++ ) { 446 AspectComponent curac = (AspectComponent)acs[i]; 447 curac.whenCloseDisplay(display); 448 } 449 } 450 451 459 460 public final boolean beforeRunningWrapper(Wrapper wrapper, 461 String wrappingMethod) { 462 boolean ret = true; 463 AspectComponent ac = wrapper.getAspectComponent(); 464 465 if (ac != null) { 466 if (!ac.beforeRunningWrapper(wrapper,wrappingMethod)) { 467 ret = false; 468 } 469 } 470 return ret; 471 } 472 473 481 482 public final void afterRunningWrapper(Wrapper wrapper, 483 String wrappingMethod) { 484 AspectComponent ac = wrapper.getAspectComponent(); 485 if (ac != null) { 486 ac.afterRunningWrapper(wrapper, wrappingMethod); 487 } 488 } 489 490 495 496 public final void afterWrap(Wrappee wrappee, Wrapper wrapper, 497 String [] wrapping_methods, 498 String [][] wrapped_methods) { 499 Object [] acs = orderedObjects.toArray(); 500 for (int i=0; i<acs.length; i++) { 501 AspectComponent curac = (AspectComponent)acs[i]; 502 curac.afterWrap(wrappee, wrapper, wrapping_methods, wrapped_methods); 503 } 504 } 505 506 public void whenGetObjects(Collection objects, ClassItem cl) 507 { 508 Iterator it = orderedObjects.iterator(); 509 while (it.hasNext()) { 510 AspectComponent curac = (AspectComponent)it.next(); 511 try { 512 curac.whenGetObjects(objects,cl); 513 } catch (Exception e) { 514 loggerAspects.error("whenGetObjects failed on "+curac,e); 515 } 516 } 517 } 518 519 public String whenNameObject(Object object, String name) 520 { 521 Iterator it = orderedObjects.iterator(); 522 while (it.hasNext()) { 523 AspectComponent curac = (AspectComponent)it.next(); 524 name = curac.whenNameObject(object,name); 525 } 526 return name; 527 } 528 529 public void getNameCounters(Map counters) { 530 Iterator it = orderedObjects.iterator(); 531 while (it.hasNext()) { 532 AspectComponent curac = (AspectComponent)it.next(); 533 curac.getNameCounters(counters); 534 } 535 } 536 537 540 public void updateNameCounters(Map counters) { 541 Iterator it = orderedObjects.iterator(); 542 while (it.hasNext()) { 543 AspectComponent curac = (AspectComponent)it.next(); 544 curac.updateNameCounters(counters); 545 } 546 } 547 548 public void whenObjectMiss(String name) 549 { 550 loggerACM.debug("whenObjectMiss("+name+")"); 551 Iterator it = orderedObjects.iterator(); 552 while (it.hasNext()) { 553 AspectComponent curac = (AspectComponent)it.next(); 554 logger.debug("whenObjectMiss("+curac+")"); 555 curac.whenObjectMiss (name); 556 } 557 } 558 559 571 572 public final boolean register(String name, Object aspectComponent) { 573 loggerAspects.debug("registering AC "+name); 574 Object o = names.get(name); 575 if (o != null) { 576 return false; 578 } 579 AspectComponent ac = (AspectComponent) aspectComponent; 581 583 586 boolean ret = super.register(name, ac); 587 588 loggerAspects.debug("memory objects = "+ObjectRepository.getMemoryObjects()); 589 Iterator it = ObjectRepository.getMemoryObjects().iterator(); 590 while( it.hasNext() ) { 591 Wrappee cur = (Wrappee) it.next(); 592 String className = cur.getClass().getName(); 593 if ( ! ( className.equals("org.objectweb.jac.core.NameRepository") || 594 className.equals("org.objectweb.jac.core.ACManager") || 595 className.equals("org.objectweb.jac.core.dist.Topology") ) ) { 596 if ( (! className.startsWith("org.objectweb.jac.core")) || ac.isSystemListener() ) { 597 loggerAspects.debug("simulateUsingNewInstance on "+cur.getClass()); 598 ac.simulateUsingNewInstance(cur); 599 } 600 } 601 } 602 return ret; 606 } 607 608 618 619 public final boolean registerWithName(String name, String className) { 620 boolean ret = false; 621 try { 622 ret = register(name, Class.forName(className).newInstance()); 623 } catch (Exception e) { 624 e.printStackTrace(); 625 } 626 return ret; 627 } 628 629 636 637 public final void weave(AspectComponent ac) { 638 if (names.get(ac) == null) { 639 register(ac.toString(),ac); 640 } 641 } 642 643 649 650 public final void unregister(String name) { 651 AspectComponent ac = (AspectComponent)objects.get(name); 652 if (ac == null) { 653 loggerAspects.debug("'"+name+"' aspect component not found. ("+names+")"); 654 return; 655 } 656 ac.unweave(); 657 super.unregister(name); 658 } 659 660 662 public final void attrdef( String name, Object value ) { 663 Collaboration.get().addAttribute( name, value ); 664 } 665 666 public final Object attr(String name) { 667 return Collaboration.get().getAttribute(name); 668 } 669 670 675 public void reloadAspect(String application, String aspect) { 676 String fullName = application+"."+aspect; 677 logger.info("Reloading "+fullName); 678 AspectComponent ac = getACFromFullName(fullName); 679 if (ac != null) { 680 ApplicationRepository.get().unextend(application,aspect); 681 ac.beforeReload(); 682 ApplicationRepository.get().extend(application,aspect); 683 ac.whenReload(); 684 } 685 } 686 687 691 public void reloadAspect(String aspect) throws Exception { 692 String application = Collaboration.get().getCurApp(); 693 if (application==null) { 694 throw new Exception ("No current application in context"); 695 } 696 reloadAspect(application,aspect); 697 } 698 699 714 715 public final void beforeWrappeeInit(Wrappee wrappee) { 716 Object [] acs = orderedObjects.toArray(); 717 for (int i = 0; i < acs.length; i++) { 718 AspectComponent curac = (AspectComponent)acs[i]; 719 curac.beforeWrappeeInit(wrappee); 720 } 721 } 722 723 public final void afterWrappeeInit(Wrappee wrappee) { 724 Object [] acs = orderedObjects.toArray(); 725 for (int i = 0; i < acs.length; i++) { 726 AspectComponent curac = (AspectComponent)acs[i]; 727 curac.afterWrappeeInit(wrappee); 728 } 729 } 730 731 } 732 | Popular Tags |