1 19 package fr.dyade.aaa.agent.conf; 20 21 import java.io.*; 22 import java.util.*; 23 24 import org.objectweb.util.monolog.api.BasicLevel; 25 import org.objectweb.util.monolog.api.Logger; 26 27 import fr.dyade.aaa.util.Transaction; 28 import fr.dyade.aaa.agent.Debug; 29 import fr.dyade.aaa.agent.AgentServer; 30 31 public class A3CMLConfig implements Serializable { 32 33 private static final long serialVersionUID = -2497621374376654935L; 34 35 36 public Hashtable domains = null; 37 38 public Hashtable servers = null; 39 40 public Hashtable properties = null; 41 42 public Hashtable clusters = null; 43 44 public A3CMLConfig() { 45 domains = new Hashtable(); 46 servers = new Hashtable(); 47 properties = new Hashtable(); 48 clusters = new Hashtable(); 49 } 50 51 58 public final void addDomain(A3CMLDomain domain) throws DuplicateDomainException { 59 if (domains.containsKey(domain.name)) 60 throw new DuplicateDomainException("Duplicate domain " + domain.name); 61 domains.put(domain.name, domain); 62 } 63 64 72 public final A3CMLDomain removeDomain(String name) throws UnknownDomainException { 73 A3CMLDomain domain = null; 74 if (domains.containsKey(name)) 75 domain = (A3CMLDomain) domains.remove(name); 76 else 77 throw new UnknownDomainException("Unknown domain " + name); 78 return domain; 79 } 80 81 87 public final boolean containsDomain(String name) { 88 return domains.containsKey(name); 89 } 90 91 99 public final A3CMLDomain getDomain(String name) throws UnknownDomainException { 100 A3CMLDomain domain = (A3CMLDomain) domains.get(name); 101 if (domain == null) 102 throw new UnknownDomainException("Unknown domain " + name); 103 return domain; 104 } 105 106 113 public final void addCluster(A3CMLCluster cluster) throws DuplicateClusterException { 114 Short id = new Short (cluster.sid); 115 if (clusters.containsKey(id)) 116 throw new DuplicateClusterException("Duplicate cluster " + cluster.sid); 117 clusters.put(id, cluster); 118 } 119 120 128 public final A3CMLCluster getCluster(short sid) throws UnknownClusterException { 129 A3CMLCluster cluster = (A3CMLCluster) clusters.get(new Short (sid)); 130 if (cluster == null) 131 throw new UnknownClusterException("Unknown cluster id. #" + sid); 132 return cluster; 133 } 134 135 143 public final A3CMLCluster getCluster(String name) throws UnknownClusterException { 144 for (Enumeration c = clusters.elements(); c.hasMoreElements(); ) { 145 A3CMLCluster cluster = (A3CMLCluster) c.nextElement(); 146 if (cluster.name.equals(name)) return cluster; 147 } 148 throw new UnknownClusterException("Unknown cluster id for cluster " + name); 149 } 150 151 159 public short getClusterIdByName(String name) throws UnknownClusterException { 160 for (Enumeration c = clusters.elements(); c.hasMoreElements(); ) { 161 A3CMLCluster cluster = (A3CMLCluster) c.nextElement(); 162 if (cluster.name.equals(name)) return cluster.sid; 163 } 164 throw new UnknownClusterException("Unknown cluster " + name); 165 } 166 167 168 174 public final boolean containsCluster(String name) { 175 try { 176 getClusterIdByName(name); 177 } catch (UnknownClusterException exc) { 178 return false; 179 } 180 return true; 181 } 182 183 189 public final boolean containsCluster(short sid) { 190 return clusters.containsKey(new Short (sid)); 191 } 192 193 201 public final A3CMLCluster removeCluster(short sid) throws UnknownClusterException { 202 A3CMLCluster cluster = null; 203 Short id = new Short (sid); 204 if (clusters.containsKey(id)) 205 cluster = (A3CMLCluster) clusters.remove(id); 206 else 207 throw new UnknownClusterException("Unknown cluster id. #" + sid); 208 return cluster; 209 } 210 211 219 public final A3CMLCluster removeCluster(String name) throws UnknownClusterException { 220 return removeCluster(getClusterIdByName(name)); 221 } 222 223 230 public final void addServer(A3CMLServer server) throws DuplicateServerException { 231 Short id = new Short (server.sid); 232 if (servers.containsKey(id)) 233 throw new DuplicateServerException("Duplicate server id. #" + server.sid); 234 servers.put(id, server); 235 } 236 237 245 public final A3CMLServer removeServer(short sid) throws UnknownServerException { 246 A3CMLServer server = null; 247 Short id = new Short (sid); 248 if (servers.containsKey(id)) { 249 server = (A3CMLServer) servers.remove(id); 250 } else { 251 throw new UnknownServerException("Unknown server id. #" + sid); 252 } 253 254 for (int i = 0; i < server.networks.size(); i++) { 255 A3CMLNetwork network = (A3CMLNetwork)server.networks.elementAt(i); 256 A3CMLDomain domain = (A3CMLDomain)domains.get(network.domain); 257 domain.removeServer(sid); 258 } 259 260 return server; 261 } 262 263 271 public final A3CMLServer removeServer(String name) throws UnknownServerException { 272 return removeServer(getServerIdByName(name)); 273 } 274 275 281 public final boolean containsServer(short sid) { 282 return servers.containsKey(new Short (sid)); 283 } 284 285 293 public short getServerIdByName(String name) throws UnknownServerException { 294 for (Enumeration s = servers.elements(); s.hasMoreElements(); ) { 295 A3CMLServer server = (A3CMLServer) s.nextElement(); 296 if (server.name.equals(name)) return server.sid; 297 } 298 throw new UnknownServerException("Unknown server " + name); 299 } 300 301 307 public final boolean containsServer(String name) { 308 try { 309 getServerIdByName(name); 310 } catch (UnknownServerException exc) { 311 return false; 312 } 313 return true; 314 } 315 316 324 public final A3CMLServer getServer(short sid) throws UnknownServerException { 325 return getServer(sid, AgentServer.NULL_ID); 326 } 327 328 337 public final A3CMLServer getServer(short sid, short cid) throws UnknownServerException { 338 A3CMLServer server = null; 339 if (cid == AgentServer.NULL_ID) 340 server = (A3CMLServer) servers.get(new Short (sid)); 341 else { 342 try { 343 A3CMLCluster cluster = getCluster(sid); 344 server = cluster.getServer(cid); 345 } catch (Exception exc) { 346 throw new UnknownServerException(exc.getMessage()); 347 } 348 } 349 350 if (server == null) 351 throw new UnknownServerException("Unknown server id. #" + sid); 352 return server; 353 } 354 355 363 public final A3CMLServer getServer(String name) throws UnknownServerException { 364 for (Enumeration s = servers.elements(); s.hasMoreElements(); ) { 365 A3CMLServer server = (A3CMLServer) s.nextElement(); 366 if (server.name.equals(name)) return server; 367 } 368 throw new UnknownServerException("Unknown server id for server " + name); 369 } 370 371 379 public final A3CMLProperty addProperty(A3CMLProperty prop) throws Exception { 380 return (A3CMLProperty) properties.put(prop.name, prop); 381 } 382 383 390 public final A3CMLProperty removeProperty(String name) { 391 return (A3CMLProperty) properties.remove(name); 392 } 393 394 400 public final boolean containsProperty(String name) { 401 return properties.containsKey(name); 402 } 403 404 407 public final A3CMLProperty getProperty(String name) { 408 return (A3CMLProperty) properties.get(name); 409 } 410 411 414 public final A3CMLProperty getProperty(String name, short sid, short cid) 415 throws Exception { 416 A3CMLProperty prop = null; 417 if (cid == AgentServer.NULL_ID) { 418 A3CMLServer server = getServer(sid); 419 prop = (A3CMLProperty) server.getProperty(name); 420 } else { 421 A3CMLCluster cluster = getCluster(sid); 422 A3CMLServer server = cluster.getServer(cid); 423 prop = (A3CMLProperty) server.getProperty(name); 424 if (prop == null) 425 prop = (A3CMLProperty) cluster.getProperty(name); 426 } 427 return prop; 428 } 429 430 438 public final String getJvmArgs(short sid) throws UnknownServerException { 439 A3CMLServer server = getServer(sid); 440 return server.getJvmArgs(); 441 } 442 443 451 public final String getJvmArgs(String name) throws UnknownServerException { 452 A3CMLServer server = getServer(name); 453 return server.getJvmArgs(); 454 } 455 456 468 public final String 469 getServiceArgs(short sid, String classname) 470 throws UnknownServerException, UnknownServiceException { 471 A3CMLServer server = getServer(sid); 472 return server.getServiceArgs(classname); 473 } 474 475 487 public final String 488 getServiceArgs(String name, String classname) 489 throws UnknownServerException, UnknownServiceException { 490 A3CMLServer server = getServer(name); 491 return server.getServiceArgs(classname); 492 } 493 494 495 496 497 498 501 public void configure(A3CMLServer root) throws Exception { 502 short rootid = root.sid; 503 Vector toExplore = new Vector(); 504 505 reset(); 507 508 for (Enumeration n = root.networks.elements(); n.hasMoreElements();) { 510 A3CMLNetwork network = (A3CMLNetwork) n.nextElement(); 511 512 A3CMLDomain domain = (A3CMLDomain) domains.get(network.domain); 513 domain.gateway = rootid; 514 domain.hops = 1; 515 toExplore.addElement(domain); 516 517 Log.logger.log(BasicLevel.DEBUG, 518 "configure - toExplore.add(" + domain + ")"); 519 } 520 521 root.visited = true; 522 root.gateway = -1; 523 root.hops = 0; 524 root.domain = "local"; 525 526 while (toExplore.size() > 0) { 527 A3CMLDomain domain = (A3CMLDomain) toExplore.elementAt(0); 528 toExplore.removeElementAt(0); 529 A3CMLServer gateway = (A3CMLServer) servers.get(new Short (domain.gateway)); 530 531 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 532 Log.logger.log(BasicLevel.DEBUG, "configure - explore(" + domain + ")"); 533 534 for (Enumeration s = domain.servers.elements(); 536 s.hasMoreElements();) { 537 A3CMLServer server = (A3CMLServer) s.nextElement(); 538 539 if (server.visited) continue; 540 541 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 542 Log.logger.log(BasicLevel.DEBUG, "configure - explore(" + server + ")"); 543 544 server.visited = true; 545 if (domain.gateway == rootid) { 546 server.gateway = -1; 548 server.domain = domain.name; 549 } else { 550 server.gateway = domain.gateway; 551 server.domain = gateway.domain; 552 } 553 server.hops = domain.hops; 554 555 for (Enumeration n = server.networks.elements(); 558 n.hasMoreElements();) { 559 A3CMLNetwork network = (A3CMLNetwork) n.nextElement(); 560 A3CMLDomain d2 = (A3CMLDomain) domains.get(network.domain); 561 562 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 563 Log.logger.log(BasicLevel.DEBUG, "configure - parse(" + d2 + ")"); 564 565 if (d2 == domain) { 566 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 567 Log.logger.log(BasicLevel.DEBUG, "configure - setPort(" + network.port + ")"); 568 572 server.port = network.port; 576 continue; 577 } 578 579 582 585 d2.hops = domain.hops +1; 588 589 if (server.gateway == -1) 591 d2.gateway = server.sid; else 593 d2.gateway = server.gateway; toExplore.addElement(d2); 595 596 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 597 Log.logger.log(BasicLevel.DEBUG, "configure - toExplore.add(" + d2 + ")"); 598 } 599 } 600 } 601 602 for (Enumeration s = servers.elements(); s.hasMoreElements();) { 604 A3CMLServer server = (A3CMLServer) s.nextElement(); 605 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 606 Log.logger.log(BasicLevel.DEBUG, "configure - verify " + server); 607 if (! server.visited) 608 throw new Exception (server + " inaccessible"); 609 } 610 611 for (Enumeration n = root.networks.elements(); n.hasMoreElements();) { 614 A3CMLNetwork network = (A3CMLNetwork) n.nextElement(); 615 A3CMLDomain domain = (A3CMLDomain) domains.get(network.domain); 616 617 if (("fr.dyade.aaa.agent.HttpNetwork".equals(domain.network)) || 618 ("fr.dyade.aaa.agent.HttpsNetwork".equals(domain.network))) { 619 620 short router = -1; 622 for (int i=0; i<domain.servers.size(); i++) { 623 A3CMLServer server = (A3CMLServer) domain.servers.elementAt(i); 624 if ((server.port > 0) && (server.sid != rootid)) { 625 router = server.sid; 626 break; 627 } 628 } 629 if (router != -1) { 631 for (int i=0; i<domain.servers.size(); i++) { 632 A3CMLServer server = (A3CMLServer) domain.servers.elementAt(i); 633 if ((server.sid != router) && (server.sid != rootid)) 634 server.gateway = router; 635 } 636 } 637 } 638 } 639 640 } 641 642 654 public A3CMLConfig getDomainConfig(String domainName) throws Exception { 655 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 656 Log.logger.log(BasicLevel.DEBUG, 657 "Config.getDomainConfig(" + domainName + ")"); 658 659 A3CMLConfig domainConf = new A3CMLConfig(); 660 661 A3CMLDomain dom = getDomain(domainName).duplicate(); 663 domainConf.addDomain(dom); 664 665 for (int i = 0; i < dom.servers.size(); i++) { 667 A3CMLServer server = (A3CMLServer) dom.servers.elementAt(i); 668 domainConf.servers.put(new Short (server.sid),server); 669 } 670 671 for (Enumeration p = properties.elements(); p.hasMoreElements(); ) { 673 A3CMLProperty property = (A3CMLProperty) p.nextElement(); 674 domainConf.addProperty(((A3CMLProperty) property).duplicate()); 675 } 676 677 try { 678 A3CMLDomain d0 = getDomain(AgentServer.ADMIN_DOMAIN); 681 domainConf.addDomain(new A3CMLDomain(d0.name,d0.network)); 682 A3CMLServer s0 = (A3CMLServer) domainConf.getServer(AgentServer.getServerId()); 683 d0 = domainConf.getDomain(AgentServer.ADMIN_DOMAIN); 684 d0.addServer(s0); 685 for (int i = 0; i < s0.networks.size(); ) { 686 A3CMLNetwork network = (A3CMLNetwork) s0.networks.elementAt(i); 687 if (!(network.domain.equals(AgentServer.ADMIN_DOMAIN) || 688 network.domain.equals(domainName))) { 689 s0.networks.removeElement(network); 690 } else 691 i++; 692 } 693 } catch (UnknownServerException exc) { 694 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 695 Log.logger.log(BasicLevel.DEBUG, "", exc); 696 } 697 698 return domainConf; 699 } 700 701 713 public A3CMLConfig getDomainConfig(String [] listDomainName) throws Exception { 714 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 715 Log.logger.log(BasicLevel.DEBUG, 716 "Config.getDomainConfig(" + listDomainName + ")"); 717 718 Hashtable context = new Hashtable(); 719 720 A3CMLConfig domainConf = new A3CMLConfig(); 721 Vector domainList = new Vector(); 722 for (int i = 0; i < listDomainName.length; i++) 723 domainList.addElement(listDomainName[i]); 724 725 for (int n = 0; n < listDomainName.length; n++) { 726 String domainName = listDomainName[n]; 727 728 A3CMLDomain dom = getDomain(domainName).duplicate(context); 730 domainConf.addDomain(dom); 731 732 for (int i = 0; i < dom.servers.size(); i++) { 734 A3CMLServer server = (A3CMLServer) dom.servers.elementAt(i); 735 for (int j = 0; j < server.networks.size(); ) { 736 A3CMLNetwork network = (A3CMLNetwork) server.networks.elementAt(j); 737 if (!(network.domain.equals(AgentServer.ADMIN_DOMAIN) || 738 network.domain.equals("transient") || 739 domainList.contains(network.domain))) { 740 server.networks.removeElement(network); 741 } else 742 j++; 743 } 744 domainConf.servers.put(new Short (server.sid),server); 745 } 746 } 747 748 for (Enumeration p = properties.elements(); p.hasMoreElements(); ) { 750 A3CMLProperty property = (A3CMLProperty) p.nextElement(); 751 domainConf.addProperty(((A3CMLProperty) property).duplicate()); 752 } 753 754 try { 755 A3CMLDomain d0 = getDomain(AgentServer.ADMIN_DOMAIN); 758 domainConf.addDomain(new A3CMLDomain(d0.name,d0.network)); 759 A3CMLServer s0 = (A3CMLServer) domainConf.getServer(AgentServer.getServerId()); 760 d0 = domainConf.getDomain(AgentServer.ADMIN_DOMAIN); 761 d0.addServer(s0); 762 for (int i = 0; i < s0.networks.size(); ) { 763 A3CMLNetwork network = (A3CMLNetwork) s0.networks.elementAt(i); 764 if (!(network.domain.equals(AgentServer.ADMIN_DOMAIN) || 765 domainList.contains(network.domain))) { 766 s0.networks.removeElement(network); 767 } else 768 i++; 769 } 770 } catch (UnknownServerException exc) { 771 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 772 Log.logger.log(BasicLevel.DEBUG, "", exc); 773 } 774 775 return domainConf; 776 } 777 778 785 public void save() throws IOException { 786 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 787 Log.logger.log(BasicLevel.DEBUG, "Config.save(" + this + ")"); 788 789 AgentServer.getTransaction().save(this, AgentServer.DEFAULT_SER_CFG_FILE); 790 } 791 792 801 public static A3CMLConfig load() throws Exception { 802 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 804 Log.logger.log(BasicLevel.DEBUG, "Config.load()"); 805 806 A3CMLConfig a3config = (A3CMLConfig) AgentServer.getTransaction().load(AgentServer.DEFAULT_SER_CFG_FILE); 807 808 if (a3config == null) { 809 Log.logger.log(BasicLevel.WARN, 810 "Unable to find configuration file."); 811 throw new IOException("Unable to find configuration file ."); 812 } 813 814 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 815 Log.logger.log(BasicLevel.DEBUG, "Config.load : a3cmlconfig = " + a3config); 816 return a3config; 817 } 818 819 829 public static A3CMLConfig getConfig(String path) throws Exception { 830 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 832 Log.logger.log(BasicLevel.DEBUG, "Config.load(" + path + ")"); 833 834 A3CMLConfig a3config = null; 835 836 File cfgFile = new File(path); 837 if (cfgFile.exists() && cfgFile.isFile()) { 838 if ((cfgFile.length() == 0)) { 839 Log.logger.log(BasicLevel.ERROR, 840 " \"" + cfgFile.getPath() + "\", is empty."); 841 throw new IOException(" \"" + cfgFile.getPath() + "\", is empty."); 842 } 843 844 FileInputStream fis = null; 845 try { 846 fis = new FileInputStream(cfgFile); 847 ObjectInputStream ois = new ObjectInputStream(fis); 848 a3config = (A3CMLConfig) ois.readObject(); 849 } catch (Exception exc) { 850 Log.logger.log(BasicLevel.WARN, "Can't load configuration: " + path, exc); 851 } finally { 852 if (fis != null) fis.close(); 853 } 854 855 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 856 Log.logger.log(BasicLevel.DEBUG, 857 "Config.load : a3cmlconfig = " + a3config); 858 return a3config; 859 } 860 861 ClassLoader classLoader = null; 863 InputStream is = null; 864 try { 865 classLoader = A3CMLConfig.class.getClassLoader(); 866 if (classLoader != null) { 867 Log.logger.log(BasicLevel.WARN, 868 "Trying to find [" + path + "] using " + 869 classLoader + " class loader."); 870 is = classLoader.getResourceAsStream(path); 871 } 872 } catch(Throwable t) { 873 Log.logger.log(BasicLevel.WARN, 874 "Can't find [" + path + "] using " + 875 classLoader + " class loader.", t); 876 is = null; 877 } 878 if (is == null) { 879 Log.logger.log(BasicLevel.WARN, 881 "Trying to find serialized config using ClassLoader.getSystemResource()."); 882 is = ClassLoader.getSystemResourceAsStream(path); 883 } 884 if (is != null) { 885 ObjectInputStream ois = new ObjectInputStream(is); 886 a3config = (A3CMLConfig) ois.readObject(); 887 } 888 889 if (a3config == null) { 890 Log.logger.log(BasicLevel.WARN, 891 "Unable to find configuration file: " + path); 892 throw new IOException("Unable to find configuration file: " + path); 893 } 894 895 if (Log.logger.isLoggable(BasicLevel.DEBUG)) 896 Log.logger.log(BasicLevel.DEBUG, "Config.load : a3cmlconfig = " + a3config); 897 return a3config; 898 } 899 900 915 920 923 940 946 public String toString() { 947 StringBuffer strBuf = new StringBuffer (); 948 strBuf.append("(").append(super.toString()); 949 strBuf.append(",properties=").append(properties); 950 strBuf.append(",domains=").append(domains); 951 strBuf.append(",servers=").append(servers); 952 strBuf.append(",clusters=").append(clusters); 953 strBuf.append(")"); 954 955 return strBuf.toString(); 956 } 957 958 959 960 961 962 963 964 965 975 public final String getServiceArgsHost(String hostname, 976 String classname) throws Exception { 977 for (Enumeration s = servers.elements(); s.hasMoreElements(); ) { 978 A3CMLServer server = (A3CMLServer) s.nextElement(); 979 if (server.hostname.equals(hostname)) { 980 try { 981 String args = getServiceArgs(server.sid, classname); 982 return args; 983 } catch (Exception exc) {} 984 } 985 } 986 throw new UnknownServiceException("Unknown service \"" + classname + 987 "\" on host " + hostname); 988 } 989 990 public boolean equals(Object obj) { 991 if (obj == null) return false; 992 993 if (obj instanceof A3CMLConfig) { 994 A3CMLConfig config = (A3CMLConfig) obj; 995 996 if (domains.equals(config.domains) && 997 servers.equals(config.servers) && 998 properties.equals(config.properties)) 999 return true; 1000 } 1001 return false; 1002 } 1003 1004 1007 public void reset() { 1008 for (Enumeration s = servers.elements(); s.hasMoreElements(); ) { 1009 A3CMLServer server = (A3CMLServer) s.nextElement(); 1010 server.visited = false; 1011 server.gateway = (short) -1; 1012 } 1013 for (Enumeration d = domains.elements(); d.hasMoreElements(); ) { 1014 A3CMLDomain domain = (A3CMLDomain) d.nextElement(); 1015 domain.gateway = (short) -1; 1016 } 1017 } 1018} 1019 | Popular Tags |