1 26 27 28 package org.objectweb.mobilitools.smi.api; 29 30 31 import org.omg.CORBA.ORB ; 32 import org.omg.CORBA.Any ; 33 import org.omg.CORBA.SystemException ; 34 import org.omg.CORBA.UnknownUserException ; 35 import org.omg.CORBA.CompletionStatus ; 36 import org.omg.CosNaming.NamingContextPackage.*; 37 import org.omg.CfMAF.*; 38 import org.objectweb.mobilitools.util.corba.NameService; 39 import org.objectweb.mobilitools.util.corba.NameServiceException; 40 import org.objectweb.mobilitools.util.thread.ShutdownLock; 41 import org.objectweb.mobilitools.smi.idl.*; 42 import org.objectweb.mobilitools.smi.*; 43 import java.util.*; 44 import java.io.*; 45 import java.lang.reflect.*; 46 47 48 64 public class Agency 65 extends Observable 66 implements AgentSystem, AgencyPersonality 67 { 68 72 static protected boolean standalone = false; 73 static public final String NOTIFY_TERMINATE = "terminate"; 74 75 84 public static void main(String [] args) 85 { 86 ORB orb = ORB.init(args, null); 87 String region = null; 88 standalone = true; 89 if (args.length >= 2) 90 { 91 try 92 { 93 new Agency(args[0], args[1], orb); 94 } 95 catch (BadOperation e) 96 { 97 System.err.println("Name " + args[0] + " already exists in region " + args[1] + "."); 98 System.exit(-2); 99 } 100 } 101 else 102 { 103 System.err.println("arguments: agency_name region_name"); 104 System.exit(-1); 105 } 106 System.out.println("Agency " + args[0] + " available in region " + args[1] + "."); 107 orb.run(); 108 } 109 110 111 protected String my_region; 113 114 protected String my_name; 116 117 protected _MAFAgentSystemComplementImplBase tie; 119 120 protected NameService my_ns; 122 123 protected String my_ns_name; 125 126 protected Location my_location; 128 129 protected ORB my_orb; 131 132 protected Hashtable agentsByRef, agentsByName; 134 135 protected MAFFinder my_finder; 137 138 protected AgencyPersonality my_personality; 140 141 protected AgentSystem my_shell; 143 144 protected AgentSystemInfo my_info; 146 147 protected ShutdownLock shutdownLock; 149 150 160 public Agency(String name, String region) 161 throws BadOperation 162 { 163 this(name, region, ORB.init(new String [0], System.getProperties())); 164 } 165 166 167 183 public Agency( 184 String region, 185 AgencyPersonality personality, 186 AgentSystem shell, 187 AgentSystemInfo info) 188 throws BadOperation 189 { 190 this(region, 191 ORB.init(new String [0], System.getProperties()), 192 personality, 193 shell, 194 info); 195 } 196 197 198 210 public Agency(String name, String region, ORB orb) 211 throws BadOperation 212 { 213 this( 214 region, 215 orb, 216 null, 217 null, 218 Constants.makeAgentSystemInfo( 219 ((String )System.getProperty("user.name", "-unspecified-")).getBytes(), 220 name.getBytes())); 221 } 222 223 224 243 public Agency( 244 String region, 245 ORB orb, 246 AgencyPersonality personality, 247 AgentSystem shell, 248 AgentSystemInfo info) 249 throws BadOperation 250 { 251 shutdownLock = new ShutdownLock(); 252 agentsByRef = new Hashtable(); 253 agentsByName = new Hashtable(); 254 my_info = info; 255 my_name = new String (info.agent_system_name.identity); 256 my_region = region; 257 my_location = new Location(region, my_name); 258 my_ns_name = my_location.getCosNamingName(); 259 my_orb = orb; 260 if (personality == null) 261 { 262 my_personality = this; 263 } 264 else 265 { 266 my_personality = personality; 267 } 268 if (shell == null) 269 { 270 my_shell = this; 271 } 272 else 273 { 274 my_shell = shell; 275 } 276 277 279 tie = new MAFAgentSystem_impl(this, false); 280 my_orb.connect(tie); 281 String finder_name = 282 System.getProperty(Constants.regionPrefixProp, Constants.regionPrefixDefault) + 283 "/" + 284 region + 285 "/" + 286 System.getProperty(Constants.finderNameProp, Constants.finderNameDefault); 287 try 289 { 290 my_ns = new NameService(my_orb); 291 } 292 catch (NameServiceException e) 293 { 294 System.err.println("Fatal error - could not get CORBA name service: " + e); 295 throw new BadOperation(BadOperation.INFRASTRUCTURE, "Cannot create agency without COS name service", e); 296 } 297 try 299 { 300 my_finder = MAFFinderHelper.narrow(my_ns.resolve(finder_name)); 301 my_finder.register_agent_system( 302 my_info.agent_system_name, 303 my_location.toString(), 304 my_info); 305 } 306 catch (NameServiceException e) 307 { 308 System.err.println("Unable to reach the MAFFinder in " + finder_name + " - no region finder support: " + e); 309 my_finder = null; 310 } 311 catch (SystemException e) 312 { 313 System.err.println("Unable to reach the MAFFinder " + finder_name + " - no region finder support: " + e); 314 my_finder = null; 315 } 316 catch (NameInvalid e) 317 { 318 throw new BadOperation(BadOperation.INVALIDNAME, "Exception in agency creation: name " + new Name(my_info.agent_system_name) + " has been rejected by the MAFFinder", e); 319 } 320 try 322 { 323 if (my_finder == null) 324 { 325 my_ns.bind(my_ns_name, tie); 326 } 327 else { 329 my_ns.rebind(my_ns_name, tie); 330 } 331 } 332 catch (NameServiceException e) 333 { 334 System.err.println("Exception with CORBA naming service - unable to bind name " + my_ns_name + ":" + e); 335 if (my_finder != null) 336 { 337 try 338 { 339 my_finder.unregister_agent_system(my_info.agent_system_name); 340 } 341 catch (Exception ex) 342 { 343 System.err.println("Warning - could not unregister agency " + new Name(my_info.agent_system_name) + " from the MAFFinder while agency creation is aborted: " + e); 344 } 345 } 346 throw new BadOperation(BadOperation.INVALIDNAME, "Could not bind " + my_ns_name + " in CORBA naming service.", e); 347 } 348 } 349 350 351 354 public AgentSystemInfo getAgentSystemInfo() 355 { 356 return my_info; 357 } 358 359 360 365 public AgentProfile makeAgentProfile(Properties properties) 366 { 367 return new AgentProfile( 368 Constants.LANGUAGE_ID, 369 my_info.agent_system_type, 370 my_info.agent_system_description, 371 my_info.major_version, 372 my_info.minor_version, 373 Constants.SERIALIZATION, 374 Misc.property2any(properties, my_orb)); 375 } 376 377 378 394 public void receiveAgent( 395 String class_name, 396 byte[] agent_state, 397 org.omg.CfMAF.Name agent_name, 398 AgentProfile agent_profile, 399 String place, 400 String code_base, 401 MAFAgentSystem class_provider) 402 throws ClassUnknown, DeserializationFailed, MAFExtendedException 403 { 404 try 405 { 406 shutdownLock.in(); 407 } 408 catch (ShutdownLock.ShutdownException e) 409 { 410 throw Constants.agencyIsClosed; 411 } 412 try 413 { 414 if (agent_profile.agent_system_type != my_info.agent_system_type) 416 { 417 System.err.println("Warning: rejected attempt to receive an object of foreign agent system type (" + String.valueOf(agent_name.agent_system_type) + ")."); 418 throw new DeserializationFailed(); 419 } 420 Boolean isRunning; 422 MobileObject agent = null; 423 try 424 { 425 SMIObjectInputStream deserializer = new SMIObjectInputStream( 426 new ByteArrayInputStream(agent_state), 427 code_base, 428 agent_profile, 429 class_provider); 430 isRunning = (Boolean ) deserializer.readObject(); 431 agent = (MobileObject) deserializer.readObject(); 432 } 433 catch (IOException e) 434 { 435 e.printStackTrace(); 436 throw new DeserializationFailed(); 437 } 438 catch (ClassNotFoundException e) 439 { 440 e.printStackTrace(); 441 throw new ClassUnknown(); 442 } 443 if (my_finder != null) 445 { 446 try 447 { 448 my_finder.register_agent(agent_name, my_location.toString(), agent_profile); 449 } 450 catch (SystemException e) 451 { 452 e.printStackTrace(); 453 throw e; 454 } 455 catch (NameInvalid e) 456 { 457 throw new MAFExtendedException(); 458 } 459 } 460 Name agent_name_wrapper = new Name(agent_name); 462 AgentInfo entry = new AgentEntry( 463 agent, 464 new Name(agent_name), 465 place, 466 isRunning.booleanValue(), 467 code_base, 468 Misc.any2property(agent_profile.properties)); 469 synchronized(agent) 470 { 471 registerAgent(agent, agent_name_wrapper, entry); 472 try 473 { 474 my_personality.afterMove(agent, my_shell, my_location, place); 475 } 476 catch (Exception e) 477 { 478 if (!(e instanceof BadOperation)) 479 { 480 System.err.println("Warning: unexpected exception thrown by agent " + agent_name_wrapper + " in afterMove()"); 481 e.printStackTrace(); 482 } 483 unregisterAgent(agent, agent_name_wrapper); 484 throw new MAFExtendedException(); 485 } 486 } 487 setChanged(); 488 notifyObservers(); 489 } 490 finally 491 { 492 shutdownLock.out(); 493 } 494 } 495 496 497 501 public MAFFinder getMAFFinder() 502 { 503 return my_finder; 504 } 505 506 507 510 public String getRegion() 511 { 512 return my_region; 513 } 514 515 516 519 public Name getName() 520 { 521 return new Name(my_info.agent_system_name); 522 } 523 524 525 530 public String getNsName() 531 { 532 return my_ns_name; 533 } 534 535 536 539 public Location getLocation() 540 { 541 return my_location; 542 } 543 544 545 550 public AgentInfo getAgentInfo(Name agent) 551 { 552 AgentInfo result; 553 result = (AgentInfo)agentsByName.get(agent); 554 return result; 555 } 556 557 558 563 public AgentInfo getAgentInfo(MobileObject agent) 564 { 565 synchronized(agent) 566 { 567 AgentInfo result; 568 result = (AgentInfo)agentsByRef.get(agent); 569 return result; 570 } 571 } 572 573 574 590 public AgentInfo createLocalAgent( 591 String agent_class, 592 Name name, 593 String place, 594 String codebase, 595 Properties properties, 596 Object argument) 597 throws BadOperation 598 { 599 return createLocalAgent( 600 agent_class, 601 name, 602 makeAgentProfile(properties), 603 place, 604 codebase, 605 null, 606 argument, 607 null); 608 } 609 610 611 630 public AgentInfo createLocalAgent( 631 String classname, 632 Name name, 633 AgentProfile profile, 634 String place, 635 String codebase, 636 byte[] agent_data, 637 Object argument, 638 MAFAgentSystem class_provider) 639 throws BadOperation 640 { 641 Class agentClass = null; 642 ClassLoader loader; 643 try 644 { 645 loader = Misc.getClassLoader( 646 Thread.currentThread().getContextClassLoader(), 647 codebase, 648 profile, 649 class_provider); 650 agentClass = loader.loadClass(classname); 651 } 652 catch (ClassNotFoundException e) 653 { 654 throw new BadOperation(BadOperation.CLASSFAULT, "Exception in create operation for agent " + name + ": undefined class " + classname + ".", e); 655 } 656 catch (Throwable e) 657 { 658 throw new BadOperation(BadOperation.INFRASTRUCTURE, "Exception in create operation for agent " + name + ": " + e); 659 } 660 if (agentClass == null) 661 { 662 throw new BadOperation(BadOperation.CLASSFAULT, "Exception in create operation for agent " + name + ": undefined class " + classname + "."); 663 } 664 if (agent_data != null && agent_data.length != 0) 665 { 666 try 667 { 668 SMIObjectInputStream deserializer = new SMIObjectInputStream( 669 new ByteArrayInputStream(agent_data), 670 loader); 671 argument = deserializer.readObject(); 672 } 673 catch (IOException e) 674 { 675 throw new BadOperation(BadOperation.SERIALIZATION, "Exception in create operation for agent " + name + ": could not deserialize argument object.", e); 676 } 677 catch (ClassNotFoundException e) 678 { 679 throw new BadOperation(BadOperation.CLASSFAULT, "Exception in create operation for agent " + name + ": undefined class " + classname + ".", e); 680 } 681 } 682 return createLocalAgent( 683 agentClass, 684 name, 685 profile, 686 place, 687 codebase, 688 argument); 689 } 690 691 692 708 AgentInfo createLocalAgent( 709 Class agent_class, 710 Name agent_name, 711 AgentProfile agent_profile, 712 String place, 713 String codebase, 714 Object argument) 715 throws BadOperation 716 { 717 try 718 { 719 shutdownLock.in(); 720 } 721 catch (ShutdownLock. ShutdownException e) 722 { 723 throw Constants.agencyIsClosed; 724 } 725 try 726 { 727 MobileObject agent = null; 728 AgentInfo entry; 729 if (agent_name.type() != my_info.agent_system_type) 731 { 732 throw new BadOperation(BadOperation.AGENTTYPE, "Exception in create operation for agent " + agent_name + ": unsupported agent system type " + String.valueOf(agent_name.type()) + "."); 733 } 734 735 try 737 { 738 if (argument != null 739 && ( ! (argument instanceof Object []) 740 || (argument instanceof Object [] && ((Object [])argument).length != 0))) 741 { 742 try 743 { 744 Constructor constructor = agent_class.getConstructor(new Class []{ Object .class }); 745 agent = (MobileObject) constructor.newInstance(new Object []{ argument }); 746 } 747 catch (NoSuchMethodException ex) 748 { 749 agent = null; 750 } 751 } 752 if (agent == null) 753 { 754 agent = (MobileObject) agent_class.newInstance(); 755 } 756 entry = new AgentEntry( 757 agent, 758 agent_name, 759 place, 760 true, 761 codebase, 762 Misc.any2property(agent_profile.properties)); 763 if (my_finder != null) 764 { 765 my_finder.register_agent( 766 agent_name.getmafname(), 767 my_location.toString(), 768 agent_profile); 769 } 770 } 771 catch (InvocationTargetException e) 772 { 773 throw new BadOperation(BadOperation.REJECTED, "Exception in create operation for agent " + agent_name + ": exception in constructor.", e); 774 } 775 catch (InstantiationException e) 776 { 777 throw new BadOperation(BadOperation.INSTANTIATION, "Exception in create operation for agent " + agent_name, e); 778 } 779 catch (IllegalAccessException e) 780 { 781 throw new BadOperation(BadOperation.INSTANTIATION, "Exception in create operation for agent " + agent_name, e); 782 } 783 catch (ExceptionInInitializerError e) 784 { 785 throw new BadOperation(BadOperation.INSTANTIATION, "Exception in create operation for agent " + agent_name + ": " + e); 786 } 787 catch (SecurityException e) 788 { 789 throw new BadOperation(BadOperation.INSTANTIATION, "Exception in create operation for agent " + agent_name, e); 790 } 791 catch (SystemException e) 792 { 793 throw new BadOperation(BadOperation.INFRASTRUCTURE, "Exception in create operation for agent " + agent_name, e); 794 } 795 catch (NameInvalid e) 796 { 797 throw new BadOperation(BadOperation.OTHER, "Exception in create operation for agent " + agent_name + " (unspecified).", e); 798 } 799 synchronized(agent) 801 { 802 registerAgent(agent, agent_name, entry); 803 try 804 { 805 my_personality.afterBirth(agent, my_shell, entry, argument); 806 } 807 catch (Exception e) 808 { 809 unregisterAgent(agent, agent_name); 810 if (my_finder != null) 811 { 812 try 813 { 814 my_finder.unregister_agent(agent_name.getmafname()); 815 } 816 catch (SystemException ex) 817 { 818 ex.printStackTrace(); 819 } 820 catch (EntryNotFound ex) 821 { 822 ex.printStackTrace(); 823 } 824 } 825 if (e instanceof BadOperation) 826 { 827 throw (BadOperation) e; 828 } 829 else 830 { 831 throw new BadOperation(BadOperation.REJECTED, "unexpected exception in afterBirth: " + e, e); 832 } 833 } 834 } 835 setChanged(); 836 notifyObservers(); 837 return entry; 838 } 839 finally 840 { 841 shutdownLock.out(); 842 } 843 } 844 845 846 858 public void moveLocalAgent(Name agent, Location agency, String place) 859 throws BadOperation 860 { 861 AgentInfo entry = (AgentInfo)agentsByName.get(agent); 862 if (entry == null) 863 { 864 throw new BadOperation( 865 BadOperation.UNKNOWNAGENT, 866 "Exception in move operation: unknown agent " + agent + "."); 867 } 868 else 869 { 870 moveLocalAgent(entry.getAgent(), agency, place); 871 } 872 } 873 874 875 887 public void moveLocalAgent(MobileObject agent, Location agency, String place) 888 throws BadOperation 889 { 890 try 891 { 892 shutdownLock.in(); 893 } 894 catch (ShutdownLock.ShutdownException e) 895 { 896 throw Constants.agencyIsClosed; 897 } 898 try 899 { 900 synchronized(agent) 901 { 902 AgentInfo entry = (AgentInfo) agentsByRef.get(agent); 904 if (entry == null) 905 { 906 throw new BadOperation( 907 BadOperation.UNKNOWNAGENT, 908 "Exception in move operation: unknown agent."); 909 } 910 MAFAgentSystem dest_ref; 912 String to_ns_name = agency.getCosNamingName(); 913 try 914 { 915 dest_ref = MAFAgentSystemHelper.narrow(my_ns.resolve(to_ns_name)); 916 } 917 catch (NameServiceException e) 918 { 919 if (e.exception instanceof NotFound) 920 { 921 throw new BadOperation( 922 BadOperation.DESTINATION, 923 "Exception in move operation for agent " + entry.getName() + ": unknown destination agency " + agency + "."); 924 } 925 else 926 { 927 e.printStackTrace(); 928 throw new BadOperation( 929 BadOperation.INFRASTRUCTURE, 930 "Exception in move operation for agent " + entry.getName() + ": something is going wrong with CORBA naming service."); 931 } 932 } 933 934 AgentProfile profile = makeAgentProfile(entry.getProperties()); 942 BadOperation exception = null; 943 Boolean isRunning; 944 try 945 { 946 my_personality.beforeMove(agent, agency, place); 947 } 948 catch (BadOperation ex) 949 { 950 throw ex; 951 } 952 catch (Exception ex) 953 { 954 throw new BadOperation(BadOperation.REJECTED, "Unexpected exception in beforeMove() for agent " + entry.getName(), ex); 955 } 956 isRunning = new Boolean (localAgentIsRunning(agent)); 957 try 958 { 959 unregisterAgent(agent, entry.getName()); 960 ClassName[] classes = { 961 new ClassName(agent.getClass().toString().substring("class ".length()), new byte[0]) 962 }; 963 ByteArrayOutputStream agent_state = new ByteArrayOutputStream(); 964 ObjectOutputStream objOut = new ObjectOutputStream(agent_state); 965 objOut.writeObject(isRunning); 966 objOut.writeObject(agent); 967 dest_ref.receive_agent( 968 entry.getName().getmafname(), 969 profile, 970 agent_state.toByteArray(), 971 place, 972 classes, 973 entry.getCodebase(), 974 tie); 975 setChanged(); 976 notifyObservers(); 977 } 978 catch (IOException e) 979 { 980 exception = new BadOperation(BadOperation.SERIALIZATION, "Exception in move operation for agent " + entry.getName() + " (serialization)", e); 981 } 982 catch (SystemException e) 983 { 984 exception = new BadOperation(BadOperation.INFRASTRUCTURE, "Exception in move operation for agent " + entry.getName() + " (CORBA)", e); 985 } 986 catch (ClassUnknown e) 987 { 988 exception = new BadOperation(BadOperation.CLASSFAULT, "Exception in move operation for agent " + entry.getName() + " (missing class)", e); 989 } 990 catch (DeserializationFailed e) 991 { 992 exception = new BadOperation(BadOperation.CLASSFAULT, "Exception in move operation for agent " + entry.getName() + " (deserialization)", e); 993 } 994 catch (MAFExtendedException e) 995 { 996 exception = new BadOperation(BadOperation.REJECTED, "Exception in move operation for agent " + entry.getName() + " (agent refused to settle in destination agency)", e); 997 try 998 { 999 my_finder.register_agent(entry.getName().getmafname(), my_location.toString(), profile); 1000 } 1001 catch (SystemException ex) 1002 { 1003 e.printStackTrace(); 1004 } 1005 catch (NameInvalid ex) 1006 { 1007 e.printStackTrace(); 1008 } 1009 } 1010 catch (ArgumentInvalid e) 1011 { 1012 exception = new BadOperation(BadOperation.OTHER, "Exception in move operation for agent " + entry.getName() + " (unspecified problem)", e); 1013 } 1014 if (exception != null) 1015 { 1016 registerAgent(agent, entry.getName(), entry); 1017 try 1018 { 1019 my_personality.afterMoveFailed(agent, agency, place, exception.getReason(), exception.getMessage()); 1020 } 1021 catch (Exception ex) 1022 { 1023 System.err.println("Warning: ignoring unexpected exception thrown by agent " + entry.getName() + " in afterMoveFailed() call-back:\n" + ex); 1024 ex.printStackTrace(); 1025 } 1026 throw exception; 1027 } 1028 } 1029 } 1030 finally 1031 { 1032 shutdownLock.out(); 1033 } 1034 } 1035 1036 1037 1044 public void terminate() 1045 { 1046 try 1047 { 1048 shutdownLock.shutdown(); 1049 } 1050 catch (ShutdownLock.ShutdownException e) 1051 { 1052 throw Constants.agencyIsClosed; 1053 } 1054 if (my_finder != null) 1056 { 1057 try 1058 { 1059 my_finder.unregister_agent_system(my_info.agent_system_name); 1060 } 1061 catch (SystemException e) 1062 { 1063 System.err.println("Warning: unable to unregister agency from MAFFinder:\n" + e.toString()); 1064 } 1065 catch (EntryNotFound e) 1066 { 1067 System.err.println("Warning: unable to unregister agency from MAFFinder:\n" + e.toString()); 1068 } 1069 } 1070 synchronized (agentsByRef) 1071 { 1072 Enumeration agents; 1073 agents = agentsByRef.elements(); 1075 while (agents.hasMoreElements()) 1076 { 1077 AgentInfo entry = (AgentInfo)agents.nextElement(); 1078 try 1079 { 1080 my_personality.beforeShutdown(entry.getAgent()); 1081 } 1082 catch(Throwable ex) 1083 { 1084 System.err.println("Warning: ignored exception thrown in beforeShutdown() by agent " + entry.getName() + ".\n" + ex); 1085 } 1086 } 1087 try 1089 { 1090 my_ns.unbind(my_ns_name); 1091 } 1092 catch (NameServiceException e) 1093 { 1094 System.err.println("Warning: unable to unbind name " + my_ns_name + " from naming service:\n" + e.toString()); 1095 } 1096 my_orb.disconnect(tie); 1097 agents = agentsByRef.elements(); 1099 while (agents.hasMoreElements()) 1100 { 1101 try 1102 { 1103 terminateLocalAgent(((AgentInfo)agents.nextElement()).getName()); 1104 } 1105 catch (BadOperation ex) 1106 { 1107 System.err.println("Exception in terminate operation for agency " + new Name(my_info.agent_system_name) + ":\n" + ex); 1108 } 1109 } 1110 my_personality.agencyShutdown(); 1111 } 1112 setChanged(); 1114 notifyObservers(NOTIFY_TERMINATE); 1115 if (standalone) 1117 { 1118 new ExitDelay(Integer.parseInt(System.getProperty(Constants.agencyExitDelayProp, Constants.agencyExitDelayDefault))); 1119 } 1120 } 1121 1122 1123 1131 public void terminateLocalAgent(Name agent) 1132 throws BadOperation 1133 { 1134 AgentInfo entry = (AgentInfo)agentsByName.get(agent); 1135 if (entry == null) 1136 { 1137 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in terminate operation: unknown agent " + agent + "."); 1138 } 1139 terminateLocalAgent(entry.getAgent()); 1140 } 1141 1142 1143 1151 public void terminateLocalAgent(MobileObject agent) 1152 throws BadOperation 1153 { 1154 try 1155 { 1156 shutdownLock.in(); 1157 } 1158 catch (ShutdownLock.ShutdownException e) 1159 { 1160 throw Constants.agencyIsClosed; 1161 } 1162 try 1163 { 1164 BadOperation exception = null; 1165 synchronized(agent) 1166 { 1167 AgentInfo entry = (AgentInfo)agentsByRef.get(agent); 1169 if (entry == null) 1170 { 1171 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in terminate operation: unknown agent."); 1172 } 1173 try 1177 { 1178 my_personality.beforeDeath(agent); 1179 } 1180 catch (Throwable e) 1181 { 1182 System.err.println("Warning in terminateLocalAgent(" + entry.getName() + "): ignored exception thrown by beforeDeath().\n" + e.toString()); 1183 exception = new BadOperation( 1184 BadOperation.REJECTED, 1185 "Unexpected exception in beforeDeath() call-back in agent " + entry.getName(), 1186 e instanceof Exception ? (Exception ) e : null); 1187 } 1188 unregisterAgent(agent, entry.getName()); 1189 try 1190 { 1191 if (my_finder != null) 1192 { 1193 my_finder.unregister_agent(entry.getName().getmafname()); 1194 } 1195 } 1196 catch (Exception e) 1197 { 1198 System.err.println("Warning in terminateLocalAgent(" + entry.getName() + "): ignored exception dealing with MAFFinder.\n" + e); 1199 if (exception == null) 1200 { 1201 exception = new BadOperation( 1202 BadOperation.INFRASTRUCTURE, 1203 "Exception in terminate operation on agent " + entry.getName(), 1204 e); 1205 } 1206 } 1207 } 1208 setChanged(); 1209 notifyObservers(); 1210 if (exception != null) 1211 { 1212 throw exception; 1213 } 1214 } 1215 finally 1216 { 1217 shutdownLock.out(); 1218 } 1219 } 1220 1221 1222 1227 public Name[] listLocalAgents(AgentProfile profile) 1228 { 1229 synchronized (agentsByRef) 1230 { 1231 Name[] result; 1232 if (profile == null) 1233 { 1234 result = new Name[agentsByName.size()]; 1235 Enumeration keys = agentsByName.keys(); 1236 for (int i=0 ; keys.hasMoreElements() ; ++i) 1237 { 1238 result[i] = (Name)keys.nextElement(); 1239 } 1240 } 1241 else 1242 { 1243 Enumeration keys = agentsByName.keys(); 1244 Vector select = new Vector(); 1245 while (keys.hasMoreElements()) 1246 { 1247 Name name = (Name)keys.nextElement(); 1248 if (Misc.matchProfile( 1249 profile, 1250 makeAgentProfile( 1251 ((AgentInfo)agentsByName.get(name)).getProperties()))) 1252 { 1253 select.add(name); 1254 } 1255 } 1256 result = new Name[select.size()]; 1257 result = (Name[])(select.toArray(result)); 1258 } 1259 return result; 1260 } 1261 } 1262 1263 1264 1270 public org.omg.CfMAF.Name[] listAgents(AgentProfile profile) 1271 { 1272 synchronized (agentsByRef) 1273 { 1274 org.omg.CfMAF.Name[] result; 1275 if (profile == null) 1276 { 1277 result = new org.omg.CfMAF.Name[agentsByName.size()]; 1278 Enumeration keys = agentsByName.keys(); 1279 for (int i=0 ; keys.hasMoreElements() ; ++i) 1280 { 1281 result[i] = ((Name)keys.nextElement()).getmafname(); 1282 } 1283 } 1284 else 1285 { 1286 Enumeration keys = agentsByName.keys(); 1287 Vector select = new Vector(); 1288 while (keys.hasMoreElements()) 1289 { 1290 Name name = (Name)keys.nextElement(); 1291 if (Misc.matchProfile( 1292 profile, 1293 makeAgentProfile( 1294 ((AgentInfo)agentsByName.get(name)).getProperties()))) 1295 { 1296 select.add(name.getmafname()); 1297 } 1298 } 1299 result = new org.omg.CfMAF.Name[select.size()]; 1300 result = (org.omg.CfMAF.Name[])(select.toArray(result)); 1301 } 1302 return result; 1303 } 1304 } 1305 1306 1307 1312 public org.omg.CfMAF.Name[] listAgents(byte[] authority) 1313 { 1314 synchronized (agentsByRef) 1315 { 1316 org.omg.CfMAF.Name[] result; 1317 Enumeration keys = agentsByName.keys(); 1318 Vector select = new Vector(); 1319 while (keys.hasMoreElements()) 1320 { 1321 Name name = (Name)keys.nextElement(); 1322 if ((new String (authority)).equals(new String (name.authority()))) 1323 { 1324 select.add(name.getmafname()); 1325 } 1326 } 1327 result = new org.omg.CfMAF.Name[select.size()]; 1328 result = (org.omg.CfMAF.Name[])(select.toArray(result)); 1329 return result; 1330 } 1331 } 1332 1333 1334 1342 public void resumeLocalAgent(Name agent) 1343 throws BadOperation 1344 { 1345 AgentInfo entry = (AgentInfo)agentsByName.get(agent); 1346 if (entry != null) 1347 { 1348 resumeLocalAgent(entry.getAgent()); 1349 } 1350 else 1351 { 1352 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in resumeLocalAgent(): unknown agent " + agent + "."); 1353 } 1354 } 1355 1356 1357 1365 public void resumeLocalAgent(MobileObject agent) 1366 throws BadOperation 1367 { 1368 try 1369 { 1370 shutdownLock.in(); 1371 } 1372 catch (ShutdownLock.ShutdownException e) 1373 { 1374 throw Constants.agencyIsClosed; 1375 } 1376 try 1377 { 1378 synchronized(agent) 1379 { 1380 AgentInfo entry = (AgentInfo)agentsByRef.get(agent); 1381 if (entry != null) 1382 { 1383 if (entry.isRunning()) 1384 { 1385 throw new BadOperation(BadOperation.RUNNING, "Exception in resume operation: agent " + entry.getName() + " was already running."); 1386 } 1387 else 1388 { 1389 try 1390 { 1391 my_personality.beforeResume(agent); 1392 } 1393 catch (BadOperation ex) 1394 { 1395 throw ex; 1396 } 1397 catch (Exception ex) 1398 { 1399 throw new BadOperation(BadOperation.REJECTED, "Unexpected exception in beforeResume() for agent " + entry.getName(), ex); 1400 } 1401 entry.toggleRunning(); 1402 } 1403 } 1404 else 1405 { 1406 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in resumeLocalAgent: unknown agent " + entry.getName() + "."); 1407 } 1408 } 1409 } 1410 finally 1411 { 1412 shutdownLock.out(); 1413 } 1414 } 1415 1416 1417 1425 public void suspendLocalAgent(Name agent) 1426 throws BadOperation 1427 { 1428 AgentInfo entry = (AgentInfo)agentsByName.get(agent); 1429 if (entry != null) 1430 { 1431 suspendLocalAgent(entry.getAgent()); 1432 } 1433 else 1434 { 1435 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in suspendLocalAgent operation: unknown agent " + agent + "."); 1436 } 1437 } 1438 1439 1440 1448 public void suspendLocalAgent(MobileObject agent) 1449 throws BadOperation 1450 { 1451 try 1452 { 1453 shutdownLock.in(); 1454 } 1455 catch (ShutdownLock.ShutdownException e) 1456 { 1457 throw Constants.agencyIsClosed; 1458 } 1459 try 1460 { 1461 synchronized(agent) 1462 { 1463 AgentInfo entry = (AgentInfo)agentsByRef.get(agent); 1464 if (entry != null) 1465 { 1466 if (! entry.isRunning()) 1467 { 1468 throw new BadOperation(BadOperation.SUSPENDED, "Exception in suspend operation: agent " + entry.getName() + " was already suspended."); 1469 } 1470 else 1471 { 1472 try 1473 { 1474 my_personality.beforeSuspend(agent); 1475 } 1476 catch (BadOperation ex) 1477 { 1478 throw ex; 1479 } 1480 catch (Exception ex) 1481 { 1482 throw new BadOperation(BadOperation.REJECTED, "Unexpected exception in beforeSuspend() for agent " + entry.getName(), ex); 1483 } 1484 entry.toggleRunning(); 1485 } 1486 } 1487 else 1488 { 1489 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in suspendLocalAgent(): unknown agent."); 1490 } 1491 } 1492 } 1493 finally 1494 { 1495 shutdownLock.out(); 1496 } 1497 } 1498 1499 1500 1507 public void setLocalAgentProperty(Name agent, String property, String value) 1508 throws BadOperation 1509 { 1510 AgentInfo entry = (AgentInfo)agentsByName.get(agent); 1511 if (entry != null) 1512 { 1513 setLocalAgentProperty(entry.getAgent(), property, value); 1514 } 1515 else 1516 { 1517 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in setLocalAgentProperty(): unknown agent " + agent + "."); 1518 } 1519 } 1520 1521 1522 1529 public void setLocalAgentProperty(MobileObject agent, String property, String value) 1530 throws BadOperation 1531 { 1532 try 1533 { 1534 shutdownLock.in(); 1535 } 1536 catch (ShutdownLock.ShutdownException e) 1537 { 1538 throw Constants.agencyIsClosed; 1539 } 1540 try 1541 { 1542 synchronized(agent) 1543 { 1544 AgentInfo entry = (AgentInfo)agentsByRef.get(agent); 1545 if (entry == null) 1546 { 1547 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Exception in setLocalAgentProperty(): unknown agent."); 1548 } 1549 Properties props = entry.getProperties(); 1550 String oldValue; 1551 if (value == null) 1552 { 1553 oldValue = (String )props.remove(value); 1554 if (oldValue == null) 1555 { 1556 return; 1557 } 1558 } 1559 else 1560 { 1561 oldValue = (String )props.setProperty(property, value); 1562 } 1563 if (my_finder != null) 1564 { 1565 try 1566 { 1567 my_finder.register_agent( 1568 entry.getName().getmafname(), 1569 my_location.toString(), 1570 makeAgentProfile(props)); 1571 } 1572 catch (Exception ex) 1573 { 1574 BadOperation exception = null; 1575 System.err.println("Exception in setLocalAgentProperty() for agent " + entry.getName() + ": " + ex); 1576 if (ex instanceof SystemException && 1577 ((SystemException )ex).completed.value() != CompletionStatus._COMPLETED_YES) 1578 { 1579 exception = new BadOperation( 1580 BadOperation.INFRASTRUCTURE, 1581 "Exception with MAFFinder in property change operation for agent " + entry.getName(), 1582 ex); 1583 } 1584 else if (ex instanceof NameInvalid) 1585 { 1586 exception = new BadOperation( 1587 BadOperation.INVALIDNAME, 1588 "Exception in property change operation for agent " + entry.getName() + ": invalid name.", 1589 ex); 1590 } 1591 if (exception != null) 1592 { 1593 if (oldValue == null) 1594 { 1595 props.remove(property); 1596 } 1597 else 1598 { 1599 props.setProperty(property, oldValue); 1600 } 1601 throw exception; 1602 } 1603 } 1604 } 1605 } 1606 } 1607 finally 1608 { 1609 shutdownLock.out(); 1610 } 1611 } 1612 1613 1614 1620 public boolean localAgentIsRunning(Name agent) 1621 throws BadOperation 1622 { 1623 AgentInfo entry = (AgentInfo)agentsByName.get(agent); 1624 if (entry != null) 1625 { 1626 return localAgentIsRunning(entry.getAgent()); 1627 } 1628 else 1629 { 1630 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Can't state activity status of unknown agent " + agent + "."); 1631 } 1632 } 1633 1634 1635 1641 public boolean localAgentIsRunning(MobileObject agent) 1642 throws BadOperation 1643 { 1644 try 1645 { 1646 shutdownLock.in(); 1647 } 1648 catch (ShutdownLock.ShutdownException e) 1649 { 1650 throw Constants.agencyIsClosed; 1651 } 1652 try 1653 { 1654 synchronized(agent) 1655 { 1656 AgentInfo entry = (AgentInfo)agentsByRef.get(agent); 1657 if (entry != null) 1658 { 1659 return entry.isRunning(); 1660 } 1661 else 1662 { 1663 throw new BadOperation(BadOperation.UNKNOWNAGENT, "Can't state activity status of unknown agent."); 1664 } 1665 } 1666 } 1667 finally 1668 { 1669 shutdownLock.out(); 1670 } 1671 } 1672 1673 1674 1677 public ORB getORB() 1678 { 1679 return my_orb; 1680 } 1681 1682 1683 1689 private void registerAgent(MobileObject agent, Name name, AgentInfo entry) 1690 { 1691 synchronized (agentsByRef) 1692 { 1693 agentsByName.put(name, entry); 1694 agentsByRef.put(agent, entry); 1695 } 1696 } 1697 1698 1699 1704 private void unregisterAgent(MobileObject agent, Name name) 1705 { 1706 synchronized(agentsByRef) 1707 { 1708 agentsByName.remove(name); 1709 agentsByRef.remove(agent); 1710 } 1711 } 1712 1713 1714 1723 public void exitOnTerminate(boolean mode, long delay) 1724 { 1725 standalone = mode; 1726 if (standalone && delay >= 0) 1727 { 1728 System.setProperty(Constants.agencyExitDelayProp, String.valueOf(delay)); 1729 } 1730 } 1731 1732 1733 1736 class ExitDelay extends Thread 1737 { 1738 long delay; 1739 1740 public ExitDelay(long ms_delay) 1741 { 1742 delay = ms_delay; 1743 start(); 1744 } 1745 1746 public void run() 1747 { 1748 try 1749 { 1750 sleep(delay); 1751 } 1752 catch (InterruptedException ex) 1753 { 1754 System.err.println(ex.toString()); 1755 } 1756 System.exit(0); 1757 } 1758 } 1759 1760 1761 1765 1766 1769 public void afterBirth(MobileObject agent, AgentSystem agency, AgentInfo entry, Object arguments) 1770 throws BadOperation 1771 { 1772 agent.afterBirth(agency, entry, arguments); 1773 } 1774 1775 1776 1779 public void beforeMove(MobileObject agent, Location location, String place) 1780 throws BadOperation 1781 { 1782 agent.beforeMove(location, place); 1783 } 1784 1785 1786 1789 public void afterMove(MobileObject agent, AgentSystem agency, Location location, String place) 1790 throws BadOperation 1791 { 1792 agent.afterMove(agency, location, place); 1793 } 1794 1795 1796 1799 public void afterMoveFailed( 1800 MobileObject agent, 1801 Location agency, 1802 String place, 1803 int reason, 1804 String message) 1805 { 1806 agent.afterMoveFailed(agency, place, reason, message); 1807 } 1808 1809 1810 1813 public void beforeDeath(MobileObject agent) 1814 { 1815 agent.beforeDeath(); 1816 } 1817 1818 1819 1822 public void beforeSuspend(MobileObject agent) 1823 throws BadOperation 1824 { 1825 agent.beforeSuspend(); 1826 } 1827 1828 1829 1832 public void beforeResume(MobileObject agent) 1833 throws BadOperation 1834 { 1835 agent.beforeResume(); 1836 } 1837 1838 1839 1842 public void beforeShutdown(MobileObject agent) 1843 { 1844 agent.beforeShutdown(); 1845 } 1846 1847 1848 1851 public void agencyShutdown() 1852 { 1853 } 1854} 1855 | Popular Tags |