To embed an AgentTransporter object within a stand-alone Java application, simply create an instance of COM.meitca.concordia.AgentTransporter. (For Java applets, use the class ConcordiaApplet instead). We can illustrate the use of the AgentTransporter by revisiting the database access example from Chapter 1. A simple application to launch and receive the database access agent using the AgentTransporter is shown below:
1. import COM.meitca.concordia.AgentTransporter; 2. 3. public class AgentTransporterApp { 4. 5. AgentTransporter agentTransporter; 6. 7. public AgentTransporterApp() { 8. 9. System.out.println("Creating Agent Transporter..."); 10. 11. // Create the AgentTransporter, named DBAccessTransporter 12. // and set security level = 0 (Concordia Security Manager disabled). 13. try { 14. agentTransporter = new AgentTransporter("DBAccessTransporter", 0); 15. } catch (Exception e) { 16. System.out.println("Error creating Agent Transporter. " + e); 17. System.exit(0); 18. } 19. ... 20. } 21. 22. /** 23. * This method creates and launches an instance of DBAccessAgent 24. */ 25. public void launch(String destination1) { 26. 27. String region[] = { "North America", "South America", "Europe"}; 28. int num_agents = 3; 29. 30. for (int i=0; i < num_agents; i++) { 31. try { 32. 33. // First we construct the DBAccessAgent 34. DBAccessAgent agent = new DBAccessAgent(region[i]); 35. 36. // Second we set up the Agents itinerary. Notice that 37. // we can specify the method to call by name. 38. Itinerary itinerary = new Itinerary(); 39. itinerary.addDestination(new Destination(destination1, "queryDatabase")); 40. itinerary.addDestination(new Destination (agentTransporter.getName(), "reportResults")); 41. 42. // We attach the Itinerary to the Agent using the setItinerary method 43. agent.setItinerary(itinerary); 44. 45. // Third set up the URL pointing to the Agent's codebase. 46. String codebase = null; 47. agent.setHomeCodebaseURL(codebase); 48. 49. // Fourth, we specify a list of classes which should travel with 50. // the agent 51. String relatedClasses[] = {"QueryResult"}; 52. agent.setRelatedClasses(relatedClasses); 53. 54. // Last, we actually launch the agent by calling 55. // its launch method 56. agent.launch(); 57. ... 58. 59. } catch (LaunchException e) { 60. ... 61. } 62. } 63. }
The AgentTransporter constructor (line 14) takes two arguments. The first argument is a string which associates a name with the new object. The name, in this case "DBAccessTransporter", can be used when specifying the AgentTransporter in an itinerary. This example however, uses the getName() method (line 40) instead of hardcoding the string name in the itinerary.
The second argument to the AgentTransporter constructor is an integer specifying the security level. In a non-Freeware version of Concordia, setting the security level to "1" will enable Concordia's security manager. In the Freeware version of Concordia it should be set to "0", but will be treated as "0" even if set to "1". If you wish to disable Concordia's security manager, or if you want to install your own security manager, set the level to "0". If it is set to "1" in a non-Freeware version of Concordia, Concordia security is enabled, and any agents arriving at the AgentTransporter will be resticted by the permissions set in the Concordia security files.
Once the AgentTransporter is created, the application is ready to receive and execute any incoming agents, or to create and launch new agents.