1 17 18 package org.objectweb.jac.core.dist; 19 20 import gnu.regexp.*; 21 import java.io.Serializable ; 22 import java.lang.reflect.Method ; 23 import java.util.Arrays ; 24 import java.util.Hashtable ; 25 import java.util.List ; 26 import java.util.Vector ; 27 import org.apache.log4j.Level; 28 import org.apache.log4j.Logger; 29 import org.objectweb.jac.core.ACConfiguration; 30 import org.objectweb.jac.core.ACManager; 31 import org.objectweb.jac.core.Application; 32 import org.objectweb.jac.core.ApplicationRepository; 33 import org.objectweb.jac.core.NameRepository; 34 import org.objectweb.jac.core.Wrappee; 35 import org.objectweb.jac.core.Wrapping; 36 import org.objectweb.jac.util.ExtArrays; 37 38 46 47 public class Topology implements Serializable { 48 static Logger logger = Logger.getLogger("topology"); 49 static Logger loggerDist = Logger.getLogger("dist"); 50 static Logger loggerApp = Logger.getLogger("application"); 51 static Logger loggerAspects = Logger.getLogger("aspects"); 52 53 public boolean bootstrapFlag = false; 54 55 57 59 60 protected static Topology globalTopology = null; 61 62 static Hashtable applicationsTopologies = new Hashtable (); 63 64 70 public static Topology getTopology(Application application) { 71 if( application == null ) return null; 72 return (Topology)applicationsTopologies.get(application); 73 } 74 75 81 public static void setTopology(Application application, 82 Topology topology) { 83 if(application == null || topology == null) return; 84 loggerApp.debug("setting "+topology+" to "+application); 85 applicationsTopologies.put(application,topology); 86 } 87 88 98 public static Topology get() { 99 if (globalTopology == null) { 100 101 new Topology(); 103 } 104 return globalTopology; 105 } 106 107 111 public static void reset() { 112 globalTopology=null; 113 get(); 114 } 115 116 124 public static Topology getPartialTopology(RE regexp) { 125 return new Topology(get().getContainers(regexp)); 126 } 127 128 132 public RemoteContainer getFirstContainer(String sregexp) { 133 RE regexp = null; 134 try { 135 regexp = new RE(sregexp); 136 } catch(Exception e) { 137 return null; 138 } 139 for (int i=0; i<containers.size(); i++) { 140 if (regexp.isMatch(((RemoteContainer)containers.get(i)).getName())) { 141 return (RemoteContainer)containers.get(i); 142 } 143 } 144 return null; 145 } 146 147 155 public static Topology getPartialTopology(String regexp) { 156 if (globalTopology == null) 157 return null; 158 RE re = null; 159 try { 160 re = new RE(regexp); 161 } catch(Exception e) { 162 logger.error("getPartialTopology "+regexp,e); 163 return null; 164 } 165 RemoteContainer[] containers = globalTopology.getContainers(re); 167 logger.debug("get partial topology found "+Arrays.asList(containers)); 168 return new Topology(containers); 169 } 170 171 172 public List containers = new Vector (); 173 174 176 public List nameReps = new Vector (); 177 178 184 public Topology() { 185 logger.debug("Creating Topology..."); 186 if (globalTopology == null) { 187 NameRepository.get().register("JAC_topology", this); 188 globalTopology = this; 189 } 190 createNameReps(); 191 logger.debug("Topology created"); 192 } 193 194 204 public Topology(String [] someNames) { 205 for(int i=0; i<someNames.length; i++) { 206 containers.add(RemoteContainer.resolve(someNames[i])); 207 } 208 createNameReps(); 209 } 210 211 222 public void createNameReps() { 223 nameReps.clear(); 224 for (int i=0; i<containers.size(); i++) { 225 RemoteContainer cc = (RemoteContainer) containers.get(i); 226 RemoteRef rr = null; 227 if (!cc.isLocal()) { 228 rr = cc.bindTo ("JAC_name_repository"); 229 } else { 230 rr = RemoteRef.create("JAC_name_repository", 231 (Wrappee)NameRepository.get()); 232 } 233 nameReps.add(rr); 234 } 235 } 236 237 246 public Topology(RemoteContainer[] someContainers) { 247 containers.addAll(Arrays.asList(someContainers)); 248 } 249 250 258 public RemoteContainer[] getContainers() { 259 RemoteContainer[] res = new RemoteContainer[containers.size()]; 260 System.arraycopy(containers.toArray(), 0, res, 0, res.length); 261 return res; 262 } 263 264 273 public RemoteContainer getContainer(int index) { 274 return (RemoteContainer)containers.get(index); 275 } 276 277 291 public RemoteContainer[] getDistContainers() { 292 Vector ret = new Vector (); 293 for ( int i=0; i<containers.size(); i++) { 294 RemoteContainer cc = (RemoteContainer)containers.get(i); 295 if (!cc.isLocal()) { 296 ret.add (cc); 297 } 298 } 299 RemoteContainer[] rcs = new RemoteContainer[ret.size()]; 300 System.arraycopy(ret.toArray(), 0, rcs, 0, ret.size()); 301 return rcs; 302 } 303 304 309 public boolean containsLocal() { 310 for (int i=0; i<containers.size(); i++) { 311 RemoteContainer cc = (RemoteContainer)containers.get(i); 312 if (cc.isLocal()) { 313 return true; 314 } 315 } 316 return false; 317 } 318 319 327 public Vector getReplicas(Wrappee localObject) { 328 String name = NameRepository.get().getName( localObject ); 329 Vector vRes = new Vector (); 330 for ( int i = 0; i < countContainers(); i++ ) { 331 logger.debug("try to find "+name+" on "+getContainer(i)+ 332 " -- local container is: "+Distd.getLocalContainerName()); 333 RemoteRef rr=null; 334 if(!getContainer(i).getName().equals(Distd.getLocalContainerName())) { 335 rr=getContainer(i).bindTo(name); 336 } 337 if( rr != null ) { 338 vRes.add( rr ); 339 } else { 340 logger.debug("remote object not found!"); 341 } 342 } 343 return vRes; 344 } 345 346 354 public boolean exist(String name) { 355 if ( nameReps == null || nameReps.size() == 0 ) { 356 createNameReps(); 357 } 358 try { 360 for ( int i = 0; i < nameReps.size(); i++ ) { 361 RemoteRef nr = (RemoteRef) nameReps.get(i); 362 if ( ! nr.getRemCont().isLocal() ) { 363 if ( ((Boolean )nr.invoke ( "isRegistered", 364 new Object [] { name } )) 365 . booleanValue() ) { 366 return true; 367 } 368 } 369 } 370 } catch (Exception e) { 371 System.out.println(e); 372 return true; 373 } 374 return false; 375 } 376 377 386 public int getContainerIndex(RemoteContainer container) { 387 return containers.indexOf(container); 388 } 389 390 401 public int getContainerIndex(String name) { 402 for ( int i = 0; i < containers.size(); i++ ) { 403 if ( ((RemoteContainer)containers.get( i )).getName().equals ( name ) ) { 404 return i; 405 } 406 } 407 return -1; 408 } 409 410 421 public int[] getContainerIndexes(RE regexp) { 422 if ( regexp == null ) { 423 return null; 424 } 425 Vector temp = new Vector (); 426 for ( int i = 0; i < containers.size(); i++ ) { 427 if ( regexp.isMatch( ((RemoteContainer)containers.get( i )).getName() ) ) { 428 temp.add( new Integer ( i ) ); 429 } 430 } 431 int[] res = new int[temp.size()]; 432 for ( int i = 0; i < res.length; i++ ) { 433 res[i] = ((Integer )temp.get(i)).intValue(); 434 } 435 return res; 436 } 437 438 449 public RemoteContainer[] getContainers(RE regexp) { 450 if ( regexp == null ) { 451 return null; 452 } 453 Vector temp = new Vector (); 454 for ( int i = 0; i < containers.size(); i++ ) { 455 if ( regexp.isMatch( ((RemoteContainer)containers.get(i)).getName() ) ) { 456 temp.add(containers.get( i )); 457 } 458 } 459 RemoteContainer[] res = new RemoteContainer[temp.size()]; 460 for ( int i = 0; i < res.length; i++ ) { 461 res[i] = (RemoteContainer)temp.get(i); 462 } 463 return res; 464 } 465 466 476 public int[] getContainerIndexes(String [] names) { 477 int[] res = null; 478 Vector vres = new Vector (); 479 if ( names != null && names.length != 0 ) { 480 for ( int i = 0; i < names.length; i++ ) { 481 int index = getContainerIndex( 482 RemoteContainer.resolve( names[i] ) ); 483 if ( index != -1 ) { 484 vres.add( new Integer ( index ) ); 485 } 486 } 487 res = new int[vres.size()]; 488 for ( int i = 0; i < res.length; i++ ) { 489 res[i] = ((Integer )vres.get(i)).intValue(); 490 } 491 } 492 return res; 493 } 494 495 502 public void addContainer(RemoteContainer container) { 503 if( containers == null ) return; 504 if( containers.contains( container ) ) return; 505 containers.add(container); 506 fireTopologyChangeEvent(); 507 } 508 509 514 public void addContainer(String container) { 515 RemoteContainer rc = RemoteContainer.resolve( container ); 516 addContainer( rc ); 517 } 518 519 526 public void addContainers(RemoteContainer[] someContainers) { 527 if( containers == null ) return; 528 for( int i=0; i < someContainers.length; i++ ) { 529 addContainer(someContainers[i]); 530 } 531 } 532 533 538 public void removeContainer(RemoteContainer container) { 539 containers.remove(container); 540 fireTopologyChangeEvent(); 541 } 542 543 550 public boolean isContainer(RemoteContainer container) { 551 return containers.contains(container); 552 } 553 554 562 public void replaceContainer(int index, RemoteContainer newContainer) { 563 if( newContainer == null ) return; 564 if( index < 0 || index >= containers.size() ) return; 565 if( containers.contains(newContainer) ) { 566 containers.remove(index); 567 fireTopologyChangeEvent(); 568 return; 569 } 570 containers.remove(index); 571 containers.add(index,newContainer); 572 fireTopologyChangeEvent(); 573 } 574 575 583 public void replaceContainer(RemoteContainer oldContainer, 584 RemoteContainer newContainer) { 585 replaceContainer(containers.indexOf(oldContainer),newContainer); 586 } 587 588 594 public RemoteContainer getContainer(String name) { 595 RemoteContainer rc = RemoteContainer.resolve(name); 596 int index = containers.indexOf(rc); 597 if( index == -1 ) return null; 598 return (RemoteContainer)containers.get(index); 599 } 600 601 606 public int countContainers() { 607 return containers.size(); 608 } 609 610 613 protected void fireTopologyChangeEvent() { 614 if( bootstrapFlag == false && this == Topology.get() ) { 615 loggerDist.debug("topology changed"); 616 Wrapping.invokeRoleMethod((Wrappee)ApplicationRepository.get(), 618 619 "invalidateTopology",ExtArrays.emptyObjectArray); 620 Wrapping.invokeRoleMethod((Wrappee)Topology.get(), 621 622 "invalidateTopology",ExtArrays.emptyObjectArray); 623 ((ACManager)ACManager.get()).whenTopologyChanged(); 625 } 626 } 627 628 629 632 public void dump () { 633 System.out.println ("Dumping the topology:"); 634 System.out.println (containers); 635 } 636 637 640 public void launchGUI(String programName) { 641 Object guiAC = ACManager.get().getObject("gui"); 642 Object display = null; 643 try { 644 display = guiAC.getClass().getMethod("createSwingDisplay", 645 new Class [] { Class .class } ) 646 .invoke(guiAC, 647 new Object [] {Class.forName("org.objectweb.jac.aspects.gui.ProgramView")}); 648 display.getClass().getMethod("setProgram",new Class [] {String .class}) 649 .invoke( display, new Object [] {programName}); 650 } catch (Exception e) { 651 logger.error("launchGUI "+programName,e); 652 } 653 } 654 655 658 public void launchGUI() { 659 Object guiAC = ACManager.get().getObject("gui"); 660 Object display = null; 661 try { 662 display = guiAC.getClass().getMethod("createSwingDisplays", 663 new Class [] { String [].class }) 664 .invoke(guiAC, new Object [] {new String [] {"admin"}}); 665 } catch (Exception e) { 666 logger.error("launchGUI",e); 667 } 668 } 669 670 677 public void startSwingGUI(String application, String [] guiNames) { 678 loggerAspects.debug( 679 "Start Swing GUI: "+application+" "+Arrays.asList(guiNames)); 680 ACManager acm = (ACManager)ACManager.get(); 681 Object guiAC = acm.getObject(application+".gui"); 682 try { 685 Class guiACClass = Class.forName("org.objectweb.jac.aspects.gui.GuiAC"); 686 Method method = guiACClass.getMethod("createSwingDisplays", 687 new Class [] {String [].class}); 688 method.invoke(guiAC,new Object [] {guiNames}); 689 } catch (Exception e) { 690 logger.error("startSwingGUI "+application+","+Arrays.asList(guiNames),e); 691 } 692 } 693 694 697 public void reloadAspect(String applicationName, String aspectName) { 698 ACManager.getACM().reloadAspect(applicationName,aspectName); 699 } 700 701 public void unweaveAspect(String applicationName, String aspectName) { 702 ACManager.getACM().unregister(applicationName+"."+aspectName); 704 } 705 706 public void weaveAspect(String applicationName, String aspectClassName, String configPath) { 707 Application app=ApplicationRepository.get().getApplication(applicationName); 708 new ACConfiguration(app,aspectClassName,configPath,true).weave(); 709 } 710 711 715 public void setTrace(String application, String category, int level) { 716 loggerAspects.debug("Setting trace "+category+"="+level); 717 Logger.getLogger(category).setLevel(Level.toLevel(level)); 718 } 719 720 724 725 public String toString() { 726 if( containers == null ) return "null"; 727 return containers.toString(); 728 } 729 730 } 731 | Popular Tags |