1 22 package fr.dyade.aaa.agent; 23 24 import java.util.*; 25 import java.lang.*; 26 27 import org.objectweb.util.monolog.api.BasicLevel; 28 import org.objectweb.util.monolog.api.Logger; 29 30 import fr.dyade.aaa.admin.cmd.*; 31 import fr.dyade.aaa.admin.script.*; 32 import fr.dyade.aaa.agent.conf.*; 33 34 35 final public class AgentAdmin extends Agent { 36 static Logger logmon; 37 38 public static final int NONE = 0; 40 public static final int CONFIGURED = 1; 41 public static final int STOPED = 2; 42 public static final int STARTED = 3; 43 static String [] statusName = {"NONE", "CONFIGURED", "STOPED", "STARTED"}; 44 45 46 private boolean silence = false; 47 48 private transient A3CMLConfig a3cmlConfig = null; 49 50 private StartScript startScript = null; 51 52 private StopScript stopScript = null; 53 54 private Script rollback = null; 55 56 private short maxId = 0; 57 58 59 public static AgentId getDefault(short serverId) { 60 return new AgentId(serverId, serverId, AgentId.AdminIdStamp); 61 } 62 63 64 public static AgentId getDefault() { 65 return getDefault(AgentServer.getServerId()); 66 } 67 68 80 public static void init(String args, boolean firstTime) throws Exception { 81 logmon = Debug.getLogger("fr.dyade.aaa.agent.Admin"); 83 if (logmon.isLoggable(BasicLevel.DEBUG)) 84 logmon.log(BasicLevel.DEBUG, 85 "AgentAdmin.init(" + args + ", " + firstTime + ")"); 86 87 if (! firstTime) return; 88 89 short maxId; 90 if (args == null) { 91 maxId = -1; 92 } else { 93 try { 94 maxId = Short.parseShort(args); 95 } catch (Exception exc) { 96 if (logmon.isLoggable(BasicLevel.WARN)) 97 logmon.log(BasicLevel.WARN, "", exc); 98 maxId = -1; 99 } 100 } 101 AgentAdmin agentAdmin = new AgentAdmin(maxId); 102 agentAdmin.deploy(); 103 } 104 105 public static void stopService() { 106 } 108 109 protected void agentInitialize(boolean firstTime) throws Exception { 110 super.agentInitialize(firstTime); 111 } 112 113 116 public AgentAdmin(short maxId) { 117 super("AgentAdmin#" + AgentServer.getServerId(), 118 true, AgentId.adminId); 119 this.maxId = maxId; 120 } 121 122 136 public void react(AgentId from, Notification not) throws Exception { 137 if (logmon.isLoggable(BasicLevel.DEBUG)) 138 logmon.log(BasicLevel.DEBUG, 139 "\n\n\nAgentAdmin.react(" + from + "," + not + ")"); 140 if (not instanceof AdminRequestNot) { 141 doReact(from, (AdminRequestNot) not); 142 } else if (not instanceof AdminStartStopNot) { 143 doReact(from, (AdminStartStopNot) not); 144 } else { 145 super.react(from, not); 146 } 147 } 148 149 157 private void doReact(AgentId from, AdminRequestNot not) throws Exception { 158 if (logmon.isLoggable(BasicLevel.DEBUG)) 159 logmon.log(BasicLevel.DEBUG, "AgentAdmin.doReact(AdminRequestNot)"); 160 161 startScript = new StartScript(); 162 stopScript = new StopScript(); 163 rollback = new Script(); 164 silence = not.silence; 165 166 try { 167 doReact(not.script); 169 } catch (Exception exc) { 170 logmon.log(BasicLevel.WARN, "AgentAdmin.react", exc); 171 if (exc instanceof ExceptionCmd) { 172 AdminReplyNot reply = new AdminReplyNot((ExceptionCmd) exc); 174 reply.setContext(not.getContext()); 175 reply.status = NONE; 176 sendTo(from, reply); 177 doReact(rollback); 179 startScript = null; 180 stopScript = null; 181 rollback = null; 182 } else 183 throw exc; 184 } 185 186 if (not.autoStart) { 187 AdminStartStopNot startstop = new AdminStartStopNot(); 189 startstop.setContext(not.getContext()); 190 startstop.startScript = startScript; 191 startstop.stopScript = stopScript; 192 doReact(from, startstop); 193 } else { 194 AdminReplyNot reply = new AdminReplyNot(); 198 reply.setContext(not.getContext()); 199 reply.startScript = startScript; 200 reply.stopScript = stopScript; 201 reply.status = CONFIGURED; 202 sendTo(from, reply); 203 } 204 } 205 206 214 private void doReact(AgentId from, AdminStartStopNot not) throws Exception { 215 if (logmon.isLoggable(BasicLevel.DEBUG)) 216 logmon.log(BasicLevel.DEBUG, 217 "AgentAdmin.doReact(AdminStartStopNot)\n startScript = " + startScript); 218 219 AdminAckStartStopNot ack = new AdminAckStartStopNot(); 220 ack.setContext(not.getContext()); 221 ack.status = CONFIGURED; 222 try { 223 startScript = not.startScript; 224 stopScript = not.stopScript; 225 if (stopScript != null) { 227 stop(); 228 ack.status = STOPED; 229 } 230 if (startScript != null) { 232 start(); 233 ack.status = STARTED; 234 } 235 ack.startScript = startScript; 237 ack.stopScript = stopScript; 240 sendTo(from, ack); 241 } catch (Exception exc) { 242 logmon.log(BasicLevel.WARN, 243 "AgentAdmin.doReact(AdminStartStopNot) Exception : " + exc); 244 if (exc instanceof ExceptionCmd) { 245 ack.exc = (ExceptionCmd) exc; 246 ack.startScript = startScript; 249 ack.stopScript = stopScript; 250 sendTo(from, ack); 251 } else 252 throw exc; 253 } 254 } 255 256 257 private void start() throws Exception { 258 if (logmon.isLoggable(BasicLevel.DEBUG)) 259 logmon.log(BasicLevel.DEBUG, 260 "AgentAdmin.start()\nstartScript = " + startScript); 261 if (startScript == null) return; 262 263 Vector toRemove = new Vector(); 264 try { 265 for (Enumeration e = startScript.serverDesc.elements(); e.hasMoreElements();) 267 AgentServer.addServerDesc((ServerDesc) e.nextElement()); 268 269 for (Enumeration e = startScript.elements(); e.hasMoreElements();) { 271 StartAdminCmd cmd = (StartAdminCmd) e.nextElement(); 272 if (cmd instanceof StartNetworkCmd) { 276 doReact((StartNetworkCmd) cmd); 277 } else if (cmd instanceof StartServiceCmd) { 278 doReact((StartServiceCmd) cmd); 279 } else if (cmd instanceof StartServerCmd) { 280 doReact((StartServerCmd) cmd); 281 } 282 toRemove.addElement(cmd); 284 } 285 286 for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { 288 StartAdminCmd cmd = (StartAdminCmd) e.nextElement(); 289 startScript.remove(cmd); 290 } 291 if (startScript.size() == 0) 292 startScript = null; 293 } catch (Exception exc) { 294 for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { 296 StartAdminCmd cmd = (StartAdminCmd) e.nextElement(); 297 startScript.remove(cmd); 298 } 299 if (startScript.size() == 0) 300 startScript = null; 301 throw exc; 302 } 303 } 304 305 306 private void stop() throws Exception { 307 if (logmon.isLoggable(BasicLevel.DEBUG)) 308 logmon.log(BasicLevel.DEBUG, 309 "AgentAdmin.stop()\nstopScript = " + stopScript); 310 if (stopScript == null) return; 311 312 Vector toRemove = new Vector(); 313 try { 314 for (Enumeration e = stopScript.elements(); e.hasMoreElements();) { 315 StopAdminCmd cmd = (StopAdminCmd) e.nextElement(); 316 if (cmd instanceof StopNetworkCmd) { 317 doReact((StopNetworkCmd) cmd); 318 } else if (cmd instanceof StopServiceCmd) { 319 doReact((StopServiceCmd) cmd); 320 } 321 toRemove.addElement(cmd); 323 } 324 325 for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { 327 StopAdminCmd cmd = (StopAdminCmd) e.nextElement(); 328 stopScript.remove(cmd); 329 } 330 if (stopScript.size() == 0) 331 stopScript = null; 332 } catch (Exception exc) { 333 for (Enumeration e = toRemove.elements(); e.hasMoreElements();) { 335 StartAdminCmd cmd = (StartAdminCmd) e.nextElement(); 336 startScript.remove(cmd); 337 } 338 if (startScript.size() == 0) 339 startScript = null; 340 throw exc; 341 } 342 } 343 344 351 private void doReact(Script script) throws Exception { 352 if (logmon.isLoggable(BasicLevel.DEBUG)) 353 logmon.log(BasicLevel.DEBUG, 354 "AgentAdmin.doReact(" + script + ")"); 355 356 if (script.newConfig) { 357 a3cmlConfig = new A3CMLConfig(); 358 } else { 359 a3cmlConfig = AgentServer.getConfig(); 361 } 362 363 if (a3cmlConfig == null) { 364 throw new ExceptionCmd("a3cmlConfig is null"); 365 } 366 367 for (Enumeration e = script.elements(); e.hasMoreElements();) { 368 AdminCmd cmd = (AdminCmd) e.nextElement(); 369 if (cmd instanceof NewDomainCmd) { 370 doReact((NewDomainCmd) cmd); 371 } else if (cmd instanceof NewServerCmd) { 372 doReact((NewServerCmd) cmd); 373 } else if (cmd instanceof NewServiceCmd) { 374 doReact((NewServiceCmd) cmd); 375 } else if (cmd instanceof NewNetworkCmd) { 376 doReact((NewNetworkCmd) cmd); 377 } else if (cmd instanceof SetJvmArgsCmd) { 378 doReact((SetJvmArgsCmd) cmd); 379 } else if (cmd instanceof SetServerPropertyCmd) { 380 doReact((SetServerPropertyCmd) cmd); 381 } else if (cmd instanceof SetPropertyCmd) { 382 doReact((SetPropertyCmd) cmd); 383 } else if (cmd instanceof SetServerNatCmd) { 384 doReact((SetServerNatCmd) cmd); 385 } else if (cmd instanceof RemoveDomainCmd) { 386 doReact((RemoveDomainCmd) cmd); 387 } else if (cmd instanceof RemoveNetworkCmd) { 388 doReact((RemoveNetworkCmd) cmd); 389 } else if (cmd instanceof RemoveServerCmd) { 390 doReact((RemoveServerCmd) cmd); 391 } else if (cmd instanceof RemoveServiceCmd) { 392 doReact((RemoveServiceCmd) cmd); 393 } else if (cmd instanceof UnsetServerPropertyCmd) { 394 doReact((UnsetServerPropertyCmd) cmd); 395 } else if (cmd instanceof UnsetPropertyCmd) { 396 doReact((UnsetPropertyCmd) cmd); 397 } else if (cmd instanceof UnsetJvmArgsCmd) { 398 doReact((UnsetJvmArgsCmd) cmd); 399 } else if (cmd instanceof UnsetServerNatCmd) { 400 doReact((UnsetServerNatCmd) cmd); 401 } else if (cmd instanceof SetNetworkPortCmd) { 402 doReact((SetNetworkPortCmd) cmd); 403 } 404 } 405 406 A3CMLServer root = a3cmlConfig.getServer(AgentServer.getServerId()); 407 a3cmlConfig.configure(root); 408 409 if (script.newConfig) 410 AgentServer.setConfig(a3cmlConfig); 411 412 a3cmlConfig.save(); 414 } 415 416 423 private void doReact(NewDomainCmd cmd) throws DomainCmdException { 424 if (logmon.isLoggable(BasicLevel.DEBUG)) 425 logmon.log(BasicLevel.DEBUG, 426 "AgentAdmin.doReact(" + cmd + ")"); 427 428 try { 429 if (!a3cmlConfig.containsDomain(cmd.name)) { 430 a3cmlConfig.addDomain( 431 new A3CMLDomain(cmd.name, 432 cmd.networkClass)); 433 rollback.add(new RemoveDomainCmd(cmd.name,cmd.networkClass)); 434 } else { 435 if (silence) return; 436 throw new DomainCmdException("Domain " + cmd.name + " already exist."); 437 } 438 } catch (Exception exc) { 439 if (logmon.isLoggable(BasicLevel.ERROR)) 440 logmon.log(BasicLevel.ERROR, "", exc); 441 throw new DomainCmdException(exc); 442 } 443 } 444 445 452 private void doReact(NewServerCmd cmd) throws ServerCmdException { 453 if (logmon.isLoggable(BasicLevel.DEBUG)) 454 logmon.log(BasicLevel.DEBUG, 455 "AgentAdmin.doReact(" + cmd + ")"); 456 457 try { 458 if (!a3cmlConfig.containsServer(cmd.name)) { 459 Short id = null; 461 if (cmd.sid == null) { 462 id = getSid(); 463 if (id == null) 464 throw new ServerCmdException( 465 "AgentAdmin : NewServerCmd(" + cmd + 466 ")not authorized on " + 467 AgentServer.getServerId()); 468 } else 469 id = cmd.sid; 470 471 a3cmlConfig.addServer( 472 new A3CMLServer(id.shortValue(), 473 cmd.name, 474 cmd.hostname)); 475 rollback.add(new RemoveServerCmd(cmd.name,cmd.hostname)); 476 477 ServerDesc sd = new ServerDesc(id.shortValue(), 479 cmd.name, 480 cmd.hostname, 481 -1); 482 sd.gateway = id.shortValue(); 484 startScript.serverDesc.put(id, sd); 485 startScript.add(new StartServerCmd(id.shortValue(),cmd.name)); 486 if (logmon.isLoggable(BasicLevel.DEBUG)) 487 logmon.log(BasicLevel.DEBUG, 488 "AgentAdmin startScript.serverDesc.put(" + id + "," + sd + ")"); 489 } else { 490 if (silence) { 491 short sid; 492 if (cmd.sid == null) { 493 sid = a3cmlConfig.getServerIdByName(cmd.name); 494 } else { 495 sid = cmd.sid.shortValue(); 496 } 497 if (sid != AgentServer.getServerId()) { 498 startScript.add(new StartServerCmd( 499 sid, 500 cmd.name)); 501 } 502 } else { 503 throw new ServerCmdException( 504 "Server " + cmd.name + " already exists"); 505 } 506 } 507 } catch (Exception exc) { 508 if (logmon.isLoggable(BasicLevel.DEBUG)) 509 logmon.log(BasicLevel.DEBUG, 510 "AgentAdmin.doReact(NewServerCmd)",exc); 511 throw new ServerCmdException(exc); 512 } 513 } 514 515 522 private void doReact(NewServiceCmd cmd) throws ServiceCmdException { 523 if (logmon.isLoggable(BasicLevel.DEBUG)) 524 logmon.log(BasicLevel.DEBUG, 525 "AgentAdmin.doReact(" + cmd + ")"); 526 527 try { 528 A3CMLServer server = a3cmlConfig.getServer(cmd.serverName); 529 A3CMLService newService = new A3CMLService(cmd.className,cmd.args); 530 if (!server.services.contains(newService)) { 531 server.services.addElement(newService); 532 rollback.add(new RemoveServiceCmd(cmd.serverName, 533 cmd.className, 534 cmd.args)); 535 536 A3CMLServer adm = a3cmlConfig.getServer(AgentServer.getServerId()); 537 if (cmd.serverName.equals(adm.name)) { 538 if (logmon.isLoggable(BasicLevel.DEBUG)) 539 logmon.log(BasicLevel.DEBUG, 540 "NewServiceCmd startScript.add(" + cmd.className + ")"); 541 startScript.add(new StartServiceCmd(cmd.className,cmd.args)); 542 } 543 } else { 544 if (silence) { 545 startScript.add(new StartServiceCmd(cmd.className,cmd.args)); 546 return; 547 } 548 throw new ServiceCmdException("Service " + newService + " already exist in server " + server); 549 } 550 } catch (Exception exc) { 551 if (logmon.isLoggable(BasicLevel.ERROR)) 552 logmon.log(BasicLevel.ERROR, "", exc); 553 throw new ServiceCmdException(exc); 554 } 555 } 556 557 564 private void doReact(NewNetworkCmd cmd) throws NetworkCmdException { 565 if (logmon.isLoggable(BasicLevel.DEBUG)) 566 logmon.log(BasicLevel.DEBUG, 567 "AgentAdmin.doReact(" + cmd + ")"); 568 569 try { 570 A3CMLServer server = a3cmlConfig.getServer(cmd.serverName); 571 A3CMLNetwork toadd = new A3CMLNetwork(cmd.domain,cmd.port); 572 if (server.networks.contains(toadd)) { 573 if (silence) { 574 if (server.sid == AgentServer.getServerId()) { 575 startScript.add(new StartNetworkCmd(server.sid, 576 cmd.domain)); 577 } 578 return; 579 } 580 throw new NetworkCmdException(cmd.serverName + " already contains network " + toadd); 581 } 582 server.networks.addElement(toadd); 583 rollback.add(new RemoveNetworkCmd(cmd.serverName, 584 cmd.domain)); 585 586 if (!cmd.domain.equals("transient")) { 587 A3CMLDomain domain = (A3CMLDomain) a3cmlConfig.getDomain(cmd.domain); 589 domain.addServer(server); 590 591 if (server.sid == AgentServer.getServerId()) { 592 if (logmon.isLoggable(BasicLevel.DEBUG)) 593 logmon.log(BasicLevel.DEBUG, 594 "startScript.add(" + server.sid + ")"); 595 startScript.add(new StartNetworkCmd(server.sid, 596 cmd.domain)); 597 } 598 599 ServerDesc serverDesc = 601 (ServerDesc) startScript.serverDesc.get(new Short (server.sid)); 602 if (serverDesc == null) 603 serverDesc = AgentServer.getServerDesc(server.sid); 604 } 608 } catch (Exception exc) { 609 if (logmon.isLoggable(BasicLevel.ERROR)) 610 logmon.log(BasicLevel.ERROR, "", exc); 611 throw new NetworkCmdException(exc); 612 } 613 } 614 615 622 private void doReact(SetNetworkPortCmd cmd) throws NetworkCmdException { 623 if (logmon.isLoggable(BasicLevel.DEBUG)) 624 logmon.log(BasicLevel.DEBUG, 625 "AgentAdmin.doReact(" + cmd + ")"); 626 try { 627 A3CMLServer server = a3cmlConfig.getServer(cmd.serverName); 628 A3CMLNetwork network = null; 629 for (int i = 0; i < server.networks.size(); i++) { 630 A3CMLNetwork nw = 631 (A3CMLNetwork)server.networks.elementAt(i); 632 if (nw.domain.equals(cmd.domain)) { 633 network = nw; 634 } 635 } 636 if (network != null) { 637 network.port = cmd.port; 647 startScript.add( 648 new StartNetworkCmd( 649 server.sid, 650 cmd.domain)); 651 } else { 652 throw new Exception ("Unknown network"); 653 } 654 } catch (Exception exc) { 655 if (logmon.isLoggable(BasicLevel.ERROR)) 656 logmon.log(BasicLevel.ERROR, "", exc); 657 throw new NetworkCmdException(exc); 658 } 659 } 660 661 668 private void doReact(SetJvmArgsCmd cmd) throws JvmArgsCmdException { 669 if (logmon.isLoggable(BasicLevel.DEBUG)) 670 logmon.log(BasicLevel.DEBUG, 671 "AgentAdmin.doReact(" + cmd + ")"); 672 673 try { 674 A3CMLServer server = a3cmlConfig.getServer(cmd.serverName); 675 server.jvmArgs = cmd.args; 676 rollback.add(new UnsetJvmArgsCmd(cmd.serverName,cmd.args)); 677 } catch (Exception exc) { 678 if (logmon.isLoggable(BasicLevel.ERROR)) 679 logmon.log(BasicLevel.ERROR, "", exc); 680 throw new JvmArgsCmdException(exc); 681 } 682 } 683 684 691 private void doReact(SetPropertyCmd cmd) throws PropertyCmdException { 692 if (logmon.isLoggable(BasicLevel.DEBUG)) 693 logmon.log(BasicLevel.DEBUG, 694 "AgentAdmin.doReact(" + cmd + ")"); 695 696 try { 697 if (!a3cmlConfig.containsProperty(cmd.name)) { 698 a3cmlConfig.addProperty( 699 new A3CMLProperty(cmd.name, 700 cmd.value)); 701 rollback.add(new UnsetPropertyCmd(cmd.name,cmd.value)); 702 } else { 703 if (silence) return; 704 throw new PropertyCmdException("Property " + cmd.name + " already exist."); 705 } 706 } catch (Exception exc) { 707 if (logmon.isLoggable(BasicLevel.ERROR)) 708 logmon.log(BasicLevel.ERROR, "", exc); 709 throw new PropertyCmdException(exc); 710 } 711 } 712 713 720 private void doReact(SetServerPropertyCmd cmd) throws PropertyCmdException { 721 if (logmon.isLoggable(BasicLevel.DEBUG)) 722 logmon.log(BasicLevel.DEBUG, 723 "AgentAdmin.doReact(" + cmd + ")"); 724 725 try { 726 A3CMLServer server = a3cmlConfig.getServer(cmd.serverName); 727 if (!server.containsProperty(cmd.name)) { 728 server.addProperty( 729 new A3CMLProperty(cmd.name, 730 cmd.value)); 731 rollback.add(new UnsetServerPropertyCmd(cmd.serverName,cmd.name,cmd.value)); 732 } else { 733 if (silence) return; 734 throw new PropertyCmdException("Property " + cmd.name + 735 " already exist in server " + cmd.serverName); 736 } 737 } catch (Exception exc) { 738 if (logmon.isLoggable(BasicLevel.ERROR)) 739 logmon.log(BasicLevel.ERROR, "", exc); 740 throw new PropertyCmdException(exc); 741 } 742 } 743 744 751 private void doReact(SetServerNatCmd cmd) throws NatCmdException { 752 if (logmon.isLoggable(BasicLevel.DEBUG)) 753 logmon.log(BasicLevel.DEBUG, 754 "AgentAdmin.doReact(" + cmd + ")"); 755 756 try { 757 A3CMLServer server = a3cmlConfig.getServer(cmd.serverName); 758 short sid = a3cmlConfig.getServerIdByName(cmd.translationServerName); 759 if (!server.containsNat(sid)) { 760 server.addNat( 761 new A3CMLNat(sid, 762 cmd.translationHostName, 763 cmd.translationHostPort)); 764 rollback.add(new UnsetServerNatCmd(cmd.serverName, 765 cmd.translationServerName, 766 cmd.translationHostName, 767 cmd.translationHostPort)); 768 } else { 769 if (silence) return; 770 throw new NatCmdException("Nat " + cmd.translationServerName + 771 " already exist in server " + cmd.serverName); 772 } 773 } catch (Exception exc) { 774 if (logmon.isLoggable(BasicLevel.ERROR)) 775 logmon.log(BasicLevel.ERROR, "", exc); 776 throw new NatCmdException(exc); 777 } 778 } 779 780 787 private void doReact(StartServiceCmd cmd) throws ServiceCmdException { 788 if (logmon.isLoggable(BasicLevel.DEBUG)) 789 logmon.log(BasicLevel.DEBUG, 790 "AgentAdmin.doReact(" + cmd + ")"); 791 792 try { 793 A3CMLServer a3cmlServer = 794 a3cmlConfig.getServer(AgentServer.getServerId()); 795 A3CMLService a3cmlService = 796 a3cmlServer.getService(cmd.className); 797 ServiceManager.register( 798 a3cmlService.classname, a3cmlService.args); 799 ServiceDesc desc = 800 (ServiceDesc) ServiceManager.manager.registry.get( 801 a3cmlService.classname); 802 if (desc.running) return; 803 ServiceManager.start(desc); 804 if (stopScript == null) stopScript = new StopScript(); 805 stopScript.add(new StopServiceCmd(a3cmlService.classname, 806 a3cmlService.args)); 807 } catch (Exception exc) { 808 if (logmon.isLoggable(BasicLevel.ERROR)) 809 logmon.log(BasicLevel.ERROR, "", exc); 810 throw new ServiceCmdException(exc); 811 } 812 } 813 814 821 private void doReact(StartNetworkCmd cmd) throws NetworkCmdException { 822 if (logmon.isLoggable(BasicLevel.DEBUG)) 823 logmon.log(BasicLevel.DEBUG, 824 "AgentAdmin.doReact(" + cmd + ")"); 825 try { 826 if (a3cmlConfig == null) 827 a3cmlConfig = AgentServer.getConfig(); 828 A3CMLServer a3cmlServer = a3cmlConfig.getServer(cmd.sid); 829 A3CMLNetwork a3cmlNetwork = null; 830 for (int i = 0; i < a3cmlServer.networks.size(); i++) { 831 A3CMLNetwork nw = 832 (A3CMLNetwork)a3cmlServer.networks.elementAt(i); 833 if (nw.domain.equals(cmd.domainName)) { 834 a3cmlNetwork = nw; 835 } 836 } 837 838 if (a3cmlNetwork == null) 839 throw new NetworkCmdException( 840 "Unknown network: " + 841 cmd.sid + '.' + cmd.domainName); 842 843 if (cmd.sid == AgentServer.getServerId()) { 844 Network network; 845 try { 846 network = 849 (Network) AgentServer.getConsumer(cmd.domainName); 850 } catch (Exception exc) { 851 network = null; 852 } 853 854 if (network != null) { 855 if (network.getPort() != a3cmlNetwork.port) { 856 if (logmon.isLoggable(BasicLevel.DEBUG)) 857 logmon.log(BasicLevel.DEBUG, 858 " -> port change (before = " + 859 network.getPort() + 860 ", after = " + 861 a3cmlNetwork.port + ')'); 862 network.stop(); 865 network.setPort(a3cmlNetwork.port); 866 } 867 network.start(); 868 } else { 869 A3CMLDomain a3cmlDomain = 870 (A3CMLDomain) a3cmlConfig.getDomain(cmd.domainName); 871 network = 872 (Network) Class.forName(a3cmlDomain.network).newInstance(); 873 short[] domainSids = new short[a3cmlDomain.servers.size()]; 874 for (int i = 0; i < domainSids.length; i++) { 875 domainSids[i] = 876 ((A3CMLServer) a3cmlDomain.servers.elementAt(i)).sid; 877 } 878 AgentServer.addConsumer(cmd.domainName, network); 879 network.init(a3cmlDomain.name, 880 a3cmlNetwork.port, 881 domainSids); 882 network.start(); 883 } 884 } else { 885 try { 889 ServerDesc serverDesc = 890 AgentServer.getServerDesc(cmd.sid); 891 if (cmd.domainName.equals(serverDesc.getDomainName())) { 892 serverDesc.updateSockAddr( 894 serverDesc.getHostname(), 895 a3cmlNetwork.port); 896 } 897 } catch (UnknownServerException exc) { 898 } 900 } 901 if (stopScript == null) stopScript = new StopScript(); 902 stopScript.add(new StopNetworkCmd(cmd.sid, 903 cmd.domainName)); 904 } catch (Exception exc) { 905 if (logmon.isLoggable(BasicLevel.ERROR)) 906 logmon.log(BasicLevel.ERROR, "", exc); 907 throw new NetworkCmdException(exc); 908 } 909 } 910 911 930 937 private void doReact(StartServerCmd cmd) throws ServerCmdException { 938 if (logmon.isLoggable(BasicLevel.DEBUG)) 939 logmon.log(BasicLevel.DEBUG, "AgentAdmin.doReact(" + cmd + ")"); 940 941 try { 942 if (a3cmlConfig == null) 946 a3cmlConfig = AgentServer.getConfig(); 947 A3CMLServer current = a3cmlConfig.getServer(AgentServer.getServerId()); 948 A3CMLServer server = a3cmlConfig.getServer(cmd.sid); 949 ServerDesc desc = AgentServer.getServerDesc(server.sid); 950 951 if (logmon.isLoggable(BasicLevel.DEBUG)) 952 logmon.log(BasicLevel.DEBUG, 953 "AgentAdmin.StartServerCmd : desc = " + desc); 954 955 964 AgentServer.initServerDesc(desc, server); 965 if (desc.gateway == desc.sid) 966 ((Network) desc.domain).addServer(server.sid); 967 968 if (logmon.isLoggable(BasicLevel.DEBUG)) 969 logmon.log(BasicLevel.DEBUG, 970 "AgentAdmin.StartServerCmd : " + 971 "desc = " + AgentServer.getServerDesc(server.sid)); 972 } catch (Exception exc) { 973 if (logmon.isLoggable(BasicLevel.ERROR)) 974 logmon.log(BasicLevel.ERROR, "", exc); 975 throw new ServerCmdException(exc); 976 } 977 } 978 979 986 private void doReact(StopNetworkCmd cmd) throws NetworkCmdException { 987 if (logmon.isLoggable(BasicLevel.DEBUG)) 988 logmon.log(BasicLevel.DEBUG, 989 "AgentAdmin.doReact(" + cmd + ")"); 990 991 try { 992 for (Enumeration c=AgentServer.getConsumers(); c.hasMoreElements(); ) { 993 MessageConsumer consumer = (MessageConsumer) c.nextElement(); 994 if (logmon.isLoggable(BasicLevel.DEBUG)) 995 logmon.log(BasicLevel.DEBUG, 996 "AgentAdmin consumer = " + consumer); 997 998 if (consumer.getDomainName().equals(cmd.domainName) 999 && consumer.isRunning()) { 1000 if (logmon.isLoggable(BasicLevel.DEBUG)) 1001 logmon.log(BasicLevel.DEBUG, 1002 "AgentAdmin Stop domain = " + cmd.domainName); 1003 consumer.stop(); 1004 1005 A3CMLNetwork nw = getNetwork( 1006 AgentServer.getServerId(), cmd.domainName); 1007 if (nw == null) { 1008 AgentServer.removeConsumer(cmd.domainName); 1010 } 1011 } 1012 } 1013 } catch (Exception exc) { 1014 if (logmon.isLoggable(BasicLevel.ERROR)) 1015 logmon.log(BasicLevel.ERROR, "", exc); 1016 throw new NetworkCmdException(exc); 1017 } 1018 } 1019 1020 private A3CMLNetwork getNetwork(short sid, String domainName) 1021 throws Exception { 1022 if (a3cmlConfig == null) 1023 a3cmlConfig = AgentServer.getConfig(); 1024 A3CMLServer server = a3cmlConfig.getServer(sid); 1025 if (server == null) return null; 1026 for (int i = 0; i < server.networks.size(); i++) { 1027 A3CMLNetwork network = (A3CMLNetwork) server.networks.elementAt(i); 1028 if (network.domain.equals(domainName)) { 1029 return network; 1030 } 1031 } 1032 return null; 1033 } 1034 1035 1042 private void doReact(StopServiceCmd cmd) throws ServiceCmdException { 1043 if (logmon.isLoggable(BasicLevel.DEBUG)) 1044 logmon.log(BasicLevel.DEBUG, 1045 "AgentAdmin.doReact(" + cmd + ")"); 1046 1047 try { 1048 ServiceManager.stop(cmd.className); 1049 } catch (Exception exc) { 1050 if (logmon.isLoggable(BasicLevel.ERROR)) 1051 logmon.log(BasicLevel.ERROR, "", exc); 1052 throw new ServiceCmdException(exc); 1053 } 1054 } 1055 1056 1063 private void doReact(RemoveDomainCmd cmd) throws DomainCmdException { 1064 if (logmon.isLoggable(BasicLevel.DEBUG)) 1065 logmon.log(BasicLevel.DEBUG, 1066 "AgentAdmin.doReact(" + cmd + ")"); 1067 1068 try { 1069 if (a3cmlConfig.containsDomain(cmd.name)) { 1070 A3CMLDomain domain = a3cmlConfig.getDomain(cmd.name); 1071 1072 if (domain.servers == null) { 1073 removeDomain(cmd.name); 1074 return; 1075 } 1076 1077 Vector toRemove = new Vector(); 1078 for (Enumeration s = domain.servers.elements(); s.hasMoreElements(); ) { 1079 A3CMLServer server = (A3CMLServer) s.nextElement(); 1080 1081 if (server.networks.size() > 2) { 1082 doReact(new RemoveNetworkCmd(server.name, 1083 cmd.name)); 1084 } else if (server.networks.size() == 2) { 1085 if ((((A3CMLNetwork) server.networks.elementAt(0)).domain.equals("transient") && 1086 ((A3CMLNetwork) server.networks.elementAt(1)).domain.equals(cmd.name)) || 1087 (((A3CMLNetwork) server.networks.elementAt(1)).domain.equals("transient") && 1088 ((A3CMLNetwork) server.networks.elementAt(0)).domain.equals(cmd.name))) 1089 toRemove.addElement(new RemoveServerCmd(server.name, 1090 server.hostname)); 1091 else 1092 doReact(new RemoveNetworkCmd(server.name, 1093 cmd.name)); 1094 } else 1095 toRemove.addElement(new RemoveServerCmd(server.name, 1096 server.hostname)); 1097 } 1098 1099 for (int i = 0; i < toRemove.size(); i++) 1100 doReact((RemoveServerCmd)toRemove.elementAt(i)); 1101 1102 removeDomain(cmd.name); 1103 } 1104 } catch (Exception exc) { 1105 if (logmon.isLoggable(BasicLevel.ERROR)) 1106 logmon.log(BasicLevel.ERROR, "", exc); 1107 throw new DomainCmdException(exc); 1108 } 1109 } 1110 1111 1118 private void doReact(RemoveNetworkCmd cmd) throws NetworkCmdException { 1119 if (logmon.isLoggable(BasicLevel.DEBUG)) 1120 logmon.log(BasicLevel.DEBUG, 1121 "AgentAdmin.doReact(" + cmd + ")"); 1122 1123 A3CMLServer server = null; 1124 try { 1125 server = a3cmlConfig.getServer(cmd.serverName); 1126 } catch(fr.dyade.aaa.agent.conf.UnknownServerException e) { return;} 1127 if (server == null) return; 1128 1129 try { 1130 if (server.sid == AgentServer.getServerId()) { 1131 if (logmon.isLoggable(BasicLevel.DEBUG)) 1132 logmon.log(BasicLevel.DEBUG, 1133 "stopScript.add( StopNetworkCmd(" + 1134 cmd.serverName + "))"); 1135 stopScript.add(new StopNetworkCmd(server.sid, 1136 cmd.domain)); 1137 } 1138 removeNetwork(server.sid,cmd.domain); 1139 } catch (Exception exc) { 1140 if (logmon.isLoggable(BasicLevel.ERROR)) 1141 logmon.log(BasicLevel.ERROR, "", exc); 1142 throw new NetworkCmdException(exc); 1143 } 1144 } 1145 1146 1153 private void doReact(RemoveServerCmd cmd) throws ServerCmdException { 1154 if (logmon.isLoggable(BasicLevel.DEBUG)) 1155 logmon.log(BasicLevel.DEBUG, 1156 "AgentAdmin.doReact(" + cmd + ")"); 1157 1158 A3CMLServer svr = null; 1159 try { 1160 svr = a3cmlConfig.getServer(cmd.name); 1161 } catch (fr.dyade.aaa.agent.conf.UnknownServerException exc) { 1162 return; 1163 } 1164 if (svr == null) return; 1165 1166 try { 1167 removeServer(svr.sid); 1168 } catch (Exception exc) { 1169 if (logmon.isLoggable(BasicLevel.ERROR)) 1170 logmon.log(BasicLevel.ERROR, "", exc); 1171 throw new ServerCmdException(exc); 1172 } 1173 } 1174 1175 1182 private void doReact(RemoveServiceCmd cmd) throws ServiceCmdException { 1183 if (logmon.isLoggable(BasicLevel.DEBUG)) 1184 logmon.log(BasicLevel.DEBUG, 1185 "AgentAdmin.doReact(" + cmd + ")"); 1186 1187 A3CMLServer server = null; 1188 try { 1189 server = a3cmlConfig.getServer(cmd.serverName); 1190 } catch(fr.dyade.aaa.agent.conf.UnknownServerException exc) { return;} 1191 1192 if (server == null) return; 1193 try { 1194 if (server.sid == AgentServer.getServerId()) { 1195 if (logmon.isLoggable(BasicLevel.DEBUG)) 1196 logmon.log(BasicLevel.DEBUG, 1197 "stopScript.add( StopServiceCmd(" + 1198 cmd.className + "))"); 1199 stopScript.add(new StopServiceCmd(cmd.className, 1200 cmd.args)); 1201 } 1202 removeService(server.sid,cmd.className); 1203 } catch (Exception exc) { 1204 if (logmon.isLoggable(BasicLevel.ERROR)) 1205 logmon.log(BasicLevel.ERROR, "", exc); 1206 throw new ServiceCmdException(exc); 1207 } 1208 } 1209 1210 1217 private void doReact(UnsetServerPropertyCmd cmd) throws PropertyCmdException { 1218 if (logmon.isLoggable(BasicLevel.DEBUG)) 1219 logmon.log(BasicLevel.DEBUG, 1220 "AgentAdmin.doReact(" + cmd + ")"); 1221 1222 A3CMLServer server = null; 1223 try { 1224 server = a3cmlConfig.getServer(cmd.serverName); 1225 } catch(fr.dyade.aaa.agent.conf.UnknownServerException exc) { return;} 1226 1227 if (server == null) return; 1228 try { 1229 unsetServerProperty(server.sid,cmd.name); 1230 } catch (Exception exc) { 1231 if (logmon.isLoggable(BasicLevel.ERROR)) 1232 logmon.log(BasicLevel.ERROR, "", exc); 1233 throw new PropertyCmdException(exc); 1234 } 1235 } 1236 1237 1244 private void doReact(UnsetPropertyCmd cmd) throws PropertyCmdException { 1245 if (logmon.isLoggable(BasicLevel.DEBUG)) 1246 logmon.log(BasicLevel.DEBUG, 1247 "AgentAdmin.doReact(" + cmd + ")"); 1248 1249 try { 1250 unsetProperty(cmd.name); 1251 } catch (Exception exc) { 1252 if (logmon.isLoggable(BasicLevel.ERROR)) 1253 logmon.log(BasicLevel.ERROR, "", exc); 1254 throw new PropertyCmdException(exc); 1255 } 1256 } 1257 1258 1265 private void doReact(UnsetServerNatCmd cmd) throws NatCmdException { 1266 if (logmon.isLoggable(BasicLevel.DEBUG)) 1267 logmon.log(BasicLevel.DEBUG, 1268 "AgentAdmin.doReact(" + cmd + ")"); 1269 1270 try { 1271 A3CMLServer server = a3cmlConfig.getServer(cmd.serverName); 1272 short sid = a3cmlConfig.getServerIdByName(cmd.translationServerName); 1273 server.removeNat(sid); 1274 } catch (Exception exc) { 1275 if (logmon.isLoggable(BasicLevel.ERROR)) 1276 logmon.log(BasicLevel.ERROR, "", exc); 1277 throw new NatCmdException(exc); 1278 } 1279 } 1280 1287 private void doReact(UnsetJvmArgsCmd cmd) throws JvmArgsCmdException { 1288 if (logmon.isLoggable(BasicLevel.DEBUG)) 1289 logmon.log(BasicLevel.DEBUG, 1290 "AgentAdmin.doReact(" + cmd + ")"); 1291 1292 A3CMLServer server = null; 1293 try { 1294 server = a3cmlConfig.getServer(cmd.serverName); 1295 } catch(fr.dyade.aaa.agent.conf.UnknownServerException exc) { return;} 1296 1297 if (server == null) return; 1298 try { 1299 unsetJvmArgs(server.sid,cmd.args); 1300 } catch (Exception exc) { 1301 if (logmon.isLoggable(BasicLevel.ERROR)) 1302 logmon.log(BasicLevel.ERROR, "", exc); 1303 throw new JvmArgsCmdException(exc); 1304 } 1305 } 1306 1307 private void removeDomain(String domainName) throws Exception { 1308 if (logmon.isLoggable(BasicLevel.DEBUG)) 1309 logmon.log(BasicLevel.DEBUG, 1310 "AgentAdmin.removeDomain(" + domainName + ")"); 1311 1312 a3cmlConfig.removeDomain(domainName); 1313 } 1314 1315 private void removeNetwork(short sid, String domainName) throws Exception { 1316 if (logmon.isLoggable(BasicLevel.DEBUG)) 1317 logmon.log(BasicLevel.DEBUG, 1318 "AgentAdmin.removeNetwork(" + sid + 1319 "," + domainName + ")"); 1320 try { 1321 A3CMLServer server = a3cmlConfig.getServer(sid); 1322 if (server == null) return; 1323 for (int i = 0; i < server.networks.size(); i++) { 1324 A3CMLNetwork network = 1325 (A3CMLNetwork)server.networks.elementAt(i); 1326 if (network.domain.equals(domainName)) { 1327 if (logmon.isLoggable(BasicLevel.DEBUG)) 1328 logmon.log(BasicLevel.DEBUG, 1329 "AgentAdmin.removeNetwork remove : " + network); 1330 server.networks.removeElementAt(i); 1331 } 1332 } 1333 } catch (fr.dyade.aaa.agent.conf.UnknownServerException exc) { 1334 if (logmon.isLoggable(BasicLevel.ERROR)) 1335 logmon.log(BasicLevel.ERROR, "", exc); 1336 return; 1337 } 1338 } 1339 1340 private void removeServer(short sid) throws Exception { 1341 if (logmon.isLoggable(BasicLevel.DEBUG)) 1342 logmon.log(BasicLevel.DEBUG, 1343 "AgentAdmin.removeServer(" + sid + ")"); 1344 1345 try { 1346 ServerDesc servDesc = AgentServer.getServerDesc(sid); 1347 if (servDesc.domain instanceof Network) 1348 ((Network) servDesc.domain).delServer(sid); 1349 } catch (Exception exc) { 1350 if (logmon.isLoggable(BasicLevel.DEBUG)) 1351 logmon.log(BasicLevel.DEBUG, 1352 "AgentAdmin.removeServer remove server #" + sid, exc); 1353 return; 1354 } 1355 1356 try { 1357 AgentServer.removeServerDesc(sid); 1359 for (Enumeration e = AgentServer.elementsServerDesc(); e.hasMoreElements(); ) { 1360 ServerDesc sd = (ServerDesc)e.nextElement(); 1361 if (sd.gateway == sid) { 1362 sd.gateway = -1; 1363 sd.domain = null; 1364 } 1365 } 1366 } catch (Exception exc) { 1367 if (logmon.isLoggable(BasicLevel.DEBUG)) 1368 logmon.log(BasicLevel.DEBUG, 1369 "AgentAdmin.removeServer remove server #" + sid, exc); 1370 } 1371 1372 a3cmlConfig.removeServer(sid); 1374 for (Enumeration d = a3cmlConfig.domains.elements(); d.hasMoreElements(); ) { 1375 A3CMLDomain domain = (A3CMLDomain)d.nextElement(); 1376 for (int i = 0; i < domain.servers.size(); i++) { 1377 A3CMLServer server = (A3CMLServer) domain.servers.elementAt(i); 1378 if (server.sid == sid) { 1379 domain.servers.removeElementAt(i); 1380 if (logmon.isLoggable(BasicLevel.DEBUG)) 1381 logmon.log(BasicLevel.DEBUG, 1382 "AgentAdmin.removeServer remove server #" + sid + 1383 " in domain " + domain); 1384 } 1385 } 1386 } 1387 } 1388 1389 private void removeService(short sid, String className) throws Exception { 1390 if (logmon.isLoggable(BasicLevel.DEBUG)) 1391 logmon.log(BasicLevel.DEBUG, 1392 "AgentAdmin.removeService(" + sid + 1393 "," + className + ")"); 1394 1395 A3CMLServer server = a3cmlConfig.getServer(sid); 1396 if (server == null) return; 1397 for (int i = 0; i < server.services.size(); i++) { 1398 A3CMLService service = 1399 (A3CMLService) server.services.elementAt(i); 1400 if (service.classname.equals(className)) { 1401 server.services.removeElementAt(i); 1402 if (logmon.isLoggable(BasicLevel.DEBUG)) 1403 logmon.log(BasicLevel.DEBUG, 1404 "AgentAdmin.removeService service = " + service); 1405 } 1406 } 1407 } 1408 1409 private void unsetServerProperty(short sid, String name) throws Exception { 1410 if (logmon.isLoggable(BasicLevel.DEBUG)) 1411 logmon.log(BasicLevel.DEBUG, 1412 "AgentAdmin.unsetServerProperty(" + sid + 1413 "," + name + ")"); 1414 1415 A3CMLServer server = a3cmlConfig.getServer(sid); 1416 if (server == null) return; 1417 server.removeProperty(name); 1418 } 1419 1420 private void unsetProperty(String name) throws Exception { 1421 if (logmon.isLoggable(BasicLevel.DEBUG)) 1422 logmon.log(BasicLevel.DEBUG, 1423 "AgentAdmin.unsetProperty(" + name + ")"); 1424 1425 a3cmlConfig.removeProperty(name); 1426 } 1427 1428 private void unsetJvmArgs(short sid, String args) throws Exception { 1429 if (logmon.isLoggable(BasicLevel.DEBUG)) 1430 logmon.log(BasicLevel.DEBUG, 1431 "AgentAdmin.unsetArgs(" + sid + 1432 "," + name + ")"); 1433 1434 A3CMLServer server = a3cmlConfig.getServer(sid); 1435 if (server == null) return; 1436 1438 StringTokenizer st = new StringTokenizer(args); 1439 while (st.hasMoreTokens()) { 1440 String toRemove = st.nextToken(); 1441 int i = server.jvmArgs.indexOf(toRemove); 1442 if (i > -1) { 1443 String begin = server.jvmArgs.substring(0,i); 1444 String end = server.jvmArgs.substring(i + toRemove.length()); 1445 server.jvmArgs = begin.concat(end); 1446 } 1447 } 1448 1449 if (server.jvmArgs == "") 1450 server.jvmArgs = null; 1451 if (logmon.isLoggable(BasicLevel.DEBUG)) 1452 logmon.log(BasicLevel.DEBUG, 1453 "AgentAdmin.unsetArgs jvmArgs = " + server.jvmArgs); 1454 } 1455 1456 public Short getSid() { 1457 if (maxId > -1) { 1458 maxId += 1; 1459 return new Short (maxId); 1460 } else { 1461 return null; 1462 } 1463 } 1464 1465 1471 1475 1485 1488} 1491 | Popular Tags |