| 1 25 package fr.dyade.aaa.agent; 26 27 import java.io.*; 28 import java.util.*; 29 30 import org.objectweb.util.monolog.api.BasicLevel; 31 import org.objectweb.util.monolog.api.Logger; 32 import org.objectweb.util.monolog.api.LoggerFactory; 33 34 import fr.dyade.aaa.util.*; 35 import fr.dyade.aaa.agent.conf.*; 36 import fr.dyade.aaa.util.management.MXWrapper; 37 38 130 public final class AgentServer { 131 public final static short NULL_ID = -1; 132 133 public final static String ADMIN_DOMAIN = "D0"; 134 public final static String ADMIN_SERVER = "s0"; 135 136 private static short serverId = NULL_ID; 137 138 private static Logger logmon = null; 139 140 public final static String CFG_DIR_PROPERTY = "fr.dyade.aaa.agent.A3CONF_DIR"; 141 public final static String DEFAULT_CFG_DIR = null; 142 143 public final static String CFG_FILE_PROPERTY = "fr.dyade.aaa.agent.A3CONF_FILE"; 144 public final static String DEFAULT_CFG_FILE = "a3servers.xml"; 145 public final static String DEFAULT_SER_CFG_FILE = "a3cmlconfig"; 146 147 public final static String CFG_NAME_PROPERTY = "fr.dyade.aaa.agent.A3CONF_NAME"; 148 public final static String DEFAULT_CFG_NAME = "default"; 149 150 public final static String A3CMLWRP_PROPERTY = "fr.dyade.aaa.agent.A3CMLWrapper"; 151 public final static String DEFAULT_A3CMLWRP = "fr.dyade.aaa.agent.conf.A3CMLSaxWrapper"; 152 153 static ThreadGroup tgroup = null; 154 155 public static ThreadGroup getThreadGroup() { 156 return tgroup; 157 } 158 159 166 static Engine engine = null; 167 168 171 public static Engine getEngine() { 172 return engine; 173 } 174 175 176 private static Transaction transaction = null; 177 178 181 public static Transaction getTransaction() { 182 return transaction; 183 } 184 185 private static JGroups jgroups = null; 186 private static short clusterId = NULL_ID; 187 188 private static ConfigController configController; 189 190 public static ConfigController getConfigController() { 191 return configController; 192 } 193 194 198 private static Hashtable consumers = null; 199 200 static void addConsumer(String domain, 201 MessageConsumer cons) throws Exception { 202 if (consumers.containsKey(domain)) 203 throw new Exception ("Consumer for domain " + domain + " already exist"); 204 205 consumers.put(domain, cons); 206 207 try { 208 MXWrapper.registerMBean(cons, 209 "AgentServer", 210 "server=" + getName() + ",cons=" + cons.getName()); 211 } catch (Exception exc) { 212 logmon.log(BasicLevel.ERROR, getName() + " jmx failed", exc); 213 } 214 } 215 216 static Enumeration getConsumers() { 217 if (consumers == null) 218 return null; 219 else 220 return consumers.elements(); 221 } 222 223 static MessageConsumer getConsumer(String domain) throws Exception { 224 if (! consumers.containsKey(domain)) 225 throw new Exception ("Unknown consumer for domain " + domain); 226 return (MessageConsumer) consumers.get(domain); 227 } 228 229 static void removeConsumer(String domain) { 230 MessageConsumer cons = (MessageConsumer) consumers.remove(domain); 231 if (cons != null) cons.stop(); 232 233 try { 234 MXWrapper.unregisterMBean("AgentServer", 235 "server=" + getName() + ",cons=" + cons.getName()); 236 } catch (Exception exc) { 237 logmon.log(BasicLevel.ERROR, getName() + " jmx failed", exc); 238 } 239 } 240 241 242 private static A3CMLConfig a3config = null; 243 244 251 public final static void setConfig(A3CMLConfig a3config) throws Exception { 252 setConfig(a3config, false); 253 } 254 255 final static void setConfig(A3CMLConfig a3config, 256 boolean force) throws Exception { 257 if (! force) { 258 synchronized(status) { 259 if (status.value != Status.INSTALLED) 260 throw new Exception ("cannot set config, bad status: " + status.value); 261 } 262 } 263 AgentServer.a3config = a3config; 264 } 265 266 271 public final static A3CMLConfig getConfig() throws Exception { 272 if (a3config == null) throw new Exception ("Server not configured"); 273 return a3config; 274 } 275 276 284 public static A3CMLConfig getAppConfig(String [] domains) throws Exception { 285 return getConfig().getDomainConfig(domains); 286 } 287 288 public final static short getServerId() { 289 return serverId; 290 } 291 292 public final static short getClusterId() { 293 return clusterId; 294 } 295 296 private static String name = null; 297 298 public final static String getName() { 299 return name; 300 } 301 302 309 public static short getServerIdByName(String name) throws Exception { 310 return getConfig().getServerIdByName(name); 311 } 312 313 320 public static String getProperty(String key) { 321 return System.getProperty(key); 322 } 323 324 332 public static String getProperty(String key, String value) { 333 return System.getProperty(key, value); 334 } 335 336 343 public static Integer getInteger(String key) { 344 try { 345 return Integer.getInteger(key); 346 } catch (Exception exc) { 347 return null; 348 } 349 } 350 351 359 public static Integer getInteger(String key, int value) { 360 try { 361 return Integer.getInteger(key, value); 362 } catch (Exception exc) { 363 return null; 364 } 365 } 366 367 368 private static ServersHT servers = null; 369 370 static void addServerDesc(ServerDesc desc) throws Exception { 371 if (desc == null) return; 372 servers.put(desc); 373 } 374 375 static ServerDesc removeServerDesc(short sid) throws Exception { 376 return servers.remove(sid); 377 } 378 379 public static Enumeration elementsServerDesc() { 380 return servers.elements(); 381 } 382 383 public static Enumeration getServersIds() { 384 return servers.keys(); 385 } 386 387 392 final static int getServerNb() { 393 return servers.size(); 394 } 395 396 402 final static ServerDesc getServerDesc(short sid) throws UnknownServerException { 403 ServerDesc serverDesc = servers.get(sid); 404 if (serverDesc == null) 405 throw new UnknownServerException("Unknow server id. #" + sid); 406 return serverDesc; 407 } 408 409 415 final static MessageConsumer getConsumer(short sid) throws UnknownServerException { 416 return getServerDesc(sid).domain; 417 } 418 419 425 public final static String getHostname(short sid) throws UnknownServerException { 426 return getServerDesc(sid).getHostname(); 427 } 428 429 434 final static ServiceDesc[] getServices() throws UnknownServerException { 435 return getServerDesc(getServerId()).services; 436 } 437 438 456 public final static 457 String getServiceArgs(short sid, 458 String classname) throws Exception { 459 return getConfig().getServiceArgs(sid, classname); 460 } 461 462 479 public final static 480 String getServiceArgs(String hostname, 481 String classname) throws Exception { 482 return getConfig().getServiceArgsHost(hostname, classname); 483 } 484 485 491 private static void configure() throws Exception { 492 A3CMLServer root = getConfig().getServer(serverId, clusterId); 493 servers = new ServersHT(); 495 ServerDesc local = new ServerDesc(root.sid, root.name, root.hostname, -1); 498 servers.put(local); 499 500 getConfig().configure(root); 503 504 createConsumers(root); 506 507 for (Enumeration s = getConfig().servers.elements(); 508 s.hasMoreElements();) { 509 A3CMLServer server = (A3CMLServer) s.nextElement(); 510 if (server.sid == root.sid) continue; 511 512 ServerDesc desc = createServerDesc(server); 513 addServerDesc(desc); 514 } 515 516 for (Enumeration c = getConfig().clusters.elements(); 518 c.hasMoreElements();) { 519 A3CMLCluster cluster = (A3CMLCluster) c.nextElement(); 520 521 for (Enumeration s = cluster.servers.elements(); 522 s.hasMoreElements();) { 523 A3CMLServer server = (A3CMLServer) s.nextElement(); 524 if (server.sid == root.sid) continue; 525 526 ServerDesc desc = servers.get(server.sid); 527 if (desc == null) { 528 desc = createServerDesc(server); 529 addServerDesc(desc); 530 } else { 531 desc.addSockAddr(server.hostname, server.port); 532 } 533 } 534 } 535 536 initServices(root, local); 537 local.domain = engine; 538 539 550 return; 551 } 552 553 private static void createConsumers(A3CMLServer root) throws Exception { 554 consumers = new Hashtable(); 555 556 engine = Engine.newInstance(); 558 addConsumer("local", engine); 559 560 if (clusterId > NULL_ID) { 562 jgroups = new JGroups(); 563 if (engine instanceof HAEngine) { 564 jgroups.setEngine((HAEngine) engine); 565 ((HAEngine) engine).setJGroups(jgroups); 566 } else 567 logmon.log(BasicLevel.ERROR, getName() + ", createConsumers(" + root + ")\n" + 568 "engine [" + engine + "] is not a HAEngine"); 569 } 570 571 for (Enumeration n = root.networks.elements(); 573 n.hasMoreElements();) { 574 A3CMLNetwork network = (A3CMLNetwork) n.nextElement(); 575 576 A3CMLDomain domain = getConfig().getDomain(network.domain); 577 try { 579 Network consumer = (Network) Class.forName(domain.network).newInstance(); 580 consumer.init(domain.name, network.port, domain.getServersId()); 583 if (consumer instanceof SimpleNetwork && 584 jgroups != null) { ((SimpleNetwork) consumer).setJGroups(jgroups); 586 jgroups.setNetWork((SimpleNetwork) consumer); 587 } 588 addConsumer(network.domain, consumer); 590 } catch (ClassNotFoundException exc) { 591 throw exc; 592 } catch (InstantiationException exc) { 593 throw exc; 594 } catch (IllegalAccessException exc) { 595 throw exc; 596 } 597 } 598 } 599 600 static void initServerDesc(ServerDesc desc, 601 A3CMLServer server) throws Exception { 602 desc.gateway = server.gateway; 603 if ((desc.gateway == -1) || (desc.gateway == server.sid)) { 606 desc.gateway = server.sid; 607 desc.updateSockAddr(desc.getHostname(), server.port); 608 A3CMLServer current = getConfig().getServer(getServerId(),getClusterId()); 609 if (current.containsNat(server.sid)) { 610 A3CMLNat nat = current.getNat(server.sid); 611 desc.updateSockAddr(nat.host, nat.port); 612 if (logmon.isLoggable(BasicLevel.DEBUG)) 613 logmon.log(BasicLevel.DEBUG, getName() + " : NAT sDesc = " + desc); 614 } 615 } 616 desc.domain = getConsumer(server.domain); 617 } 618 619 private static ServerDesc 620 createServerDesc(A3CMLServer server) throws Exception { 621 if (! server.visited) 622 throw new Exception (server + " inaccessible"); 623 624 ServerDesc desc = new ServerDesc(server.sid, 625 server.name, 626 server.hostname, 627 -1); 628 629 initServerDesc(desc, server); 630 initServices(server, desc); 631 632 return desc; 633 } 634 635 private static void initServices(A3CMLServer server, ServerDesc desc) { 636 if (server.services != null) { 637 ServiceDesc services[] = new ServiceDesc[server.services.size()]; 638 int idx = 0; 639 for (Enumeration x = server.services.elements(); 640 x.hasMoreElements();) { 641 A3CMLService service = (A3CMLService) x.nextElement(); 642 services[idx++] = new ServiceDesc(service.classname, service.args); 643 } 644 desc.services = services; 645 } 646 } 647 648 private static void setProperties(short sid, short cid) throws Exception { 649 if (a3config == null) return; 650 651 if (a3config.properties != null) { 653 for (Enumeration e = a3config.properties.elements(); e.hasMoreElements();) { 654 A3CMLProperty p = (A3CMLProperty) e.nextElement(); 655 System.getProperties().put(p.name,p.value); 656 } 657 } 658 659 A3CMLServer server = null; 660 if (cid != NULL_ID) { 661 A3CMLCluster cluster = null; 662 cluster = a3config.getCluster(sid); 663 664 if (cluster != null 666 && cluster.properties != null 667 && cluster.properties.size() > 0) { 668 Enumeration e = cluster.properties.elements(); 669 do { 670 A3CMLProperty p = (A3CMLProperty) e.nextElement(); 671 System.getProperties().put(p.name,p.value); 672 } while (e.hasMoreElements()); 673 } 674 server = cluster.getServer(cid); 675 } else { 676 server = a3config.getServer(sid); 677 } 678 679 if (server != null && server.properties != null) { 681 Enumeration e = server.properties.elements(); 682 do { 683 A3CMLProperty p = (A3CMLProperty) e.nextElement(); 684 System.getProperties().put(p.name,p.value); 685 } while (e.hasMoreElements()); 686 } 687 } 688 689 public static class Status { 690 public static final int INSTALLED = 0; 691 public static final int INITIALIZING = 0x1; 692 public static final int INITIALIZED = 0x2; 693 public static final int STARTING = 0x3; 694 public static final int STARTED = 0x4; 695 public static final int STOPPING = 0x5; 696 public static final int STOPPED = 0x6; 697 public static final int RESETING = 0x7; 698 699 private int value = INSTALLED; 700 701 public static String [] info = {"installed", 702 "initializing", "initialized", 703 "starting", "started", 704 "stopping", "stopped", 705 "reseting"}; 706 } 707 708 static Status status = new Status(); 709 710 public static int getStatus() { 711 return status.value; 712 } 713 714 public static String getStatusInfo() { 715 return Status.info[status.value]; 716 } 717 718 732 public static int init(String args[]) throws Exception { 733 if (args.length < 2) 734 throw new Exception ("usage: java <main> sid storage"); 735 short sid = NULL_ID; 736 try { 737 sid = (short) Integer.parseInt(args[0]); 738 } catch (NumberFormatException exc) { 739 throw new Exception ("usage: java <main> sid storage"); 740 } 741 String path = args[1]; 742 short cid = NULL_ID; 743 try { 744 if (args.length == 3) 745 cid = (short) Integer.parseInt(args[2]); 746 } catch (NumberFormatException exc) {} 747 748 init(sid, path, null, cid); 749 750 configController = new ConfigController(); 751 752 return 2; 753 } 754 755 public static void reset(boolean force) { 756 if (force) { 757 synchronized(status) { 758 if (status.value != Status.STOPPED) { 759 logmon.log(BasicLevel.WARN, 760 getName() + ", force status: " + status.value); 761 } 762 status.value = Status.STOPPED; 763 } 764 } 765 reset(); 766 } 767 768 772 public static void reset() { 773 synchronized(status) { 774 if (status.value != Status.STOPPED) { 775 logmon.log(BasicLevel.WARN, 776 getName() + ", cannot reset, bad status: " + status.value); 777 return; 778 } 779 status.value = Status.RESETING; 780 } 781 782 Enumeration e = getConsumers(); 784 if (e != null) { 785 for (; e.hasMoreElements();) { 786 MessageConsumer cons = (MessageConsumer) e.nextElement(); 787 try { 788 MXWrapper.unregisterMBean( 789 "AgentServer", 790 "server=" + getName() + ",cons=" + cons.getName()); 791 } catch (Exception exc) { 792 logmon.log(BasicLevel.DEBUG, 793 getName() + ", jmx failed: " + 794 "server=" + getName() + ",cons=" + cons.getName(), exc); 795 } 796 } 797 consumers = null; 798 } 799 800 try { 801 MXWrapper.unregisterMBean("AgentServer", 802 "server=" + getName() + ",cons=Transaction"); 803 } catch (Exception exc) { 804 logmon.log(BasicLevel.DEBUG, 805 getName() + ", jmx failed: " + 806 "server=" + getName() + ",cons=Transaction", exc); 807 } 808 809 if (transaction != null) transaction.close(); 810 transaction = null; 811 812 try { 813 MXWrapper.unregisterMBean("AgentServer", "server=" + getName()); 814 } catch (Exception exc) { 815 logmon.log(BasicLevel.DEBUG, 816 getName() + " jmx failed: "+ "server=" + getName(), exc); 817 } 818 819 a3config = null; 820 821 synchronized(status) { 822 status.value = Status.INSTALLED; 823 } 824 } 825 826 841 public static void init(short sid, 842 String path, 843 LoggerFactory loggerFactory) throws Exception { 844 init(sid, path, loggerFactory, NULL_ID); 845 } 846 847 863 public static void init(short sid, 864 String path, 865 LoggerFactory loggerFactory, 866 short cid) throws Exception { 867 name = new StringBuffer ("AgentServer#").append(sid).toString(); 868 869 if (loggerFactory != null) Debug.setLoggerFactory(loggerFactory); 870 logmon = Debug.getLogger(Debug.A3Debug + ".AgentServer.#" + sid); 871 872 if (logmon.isLoggable(BasicLevel.DEBUG)) 873 logmon.log(BasicLevel.DEBUG, getName() + ", init()", new Exception ()); 874 else 875 logmon.log(BasicLevel.WARN, getName() + ", init()"); 876 877 synchronized(status) { 878 if (status.value == Status.STOPPED) { 879 logmon.log(BasicLevel.DEBUG, getName() + ", reset configuration"); 880 reset(); 881 } 882 if (status.value != Status.INSTALLED) 883 throw new Exception ("cannot initialize, bad status: " + status.value); 884 status.value = Status.INITIALIZING; 885 } 886 887 try { 888 serverId = sid; 889 890 tgroup = new ThreadGroup (getName()) { 891 public void uncaughtException(Thread t, Throwable e) { 892 if (logmon.isLoggable(BasicLevel.WARN)) { 893 logmon.log(BasicLevel.WARN, 894 "Abnormal termination for " + 895 t.getThreadGroup().getName() + "." + t.getName(), 896 e); 897 } 898 } 899 }; 900 901 File dir = new File(path); 904 if (dir.exists() && dir.isDirectory()) { 905 File tfc = new File(dir, "TFC"); 906 if (tfc.exists()) { 907 DataInputStream dis = null; 908 try { 909 dis = new DataInputStream(new FileInputStream(tfc)); 910 String tname = dis.readUTF(); 911 Class tclass = Class.forName(tname); 912 transaction = (Transaction) tclass.newInstance(); 913 } catch (Exception exc) { 914 logmon.log(BasicLevel.FATAL, 915 getName() + ", can't instanciate transaction manager", 916 exc); 917 throw new Exception ("Can't instanciate transaction manager"); 918 } finally { 919 if (dis != null) dis.close(); 920 } 921 try { 922 transaction.init(path); 923 } catch (IOException exc) { 924 logmon.log(BasicLevel.FATAL, 925 getName() + ", can't start transaction manager", exc); 926 throw new Except
|