Java Reference
In-Depth Information
42. else{
43. ContactImpl contact = (ContactImpl)o;
44. if (firstName.equals(contact.firstName) &&
45. lastName.equals(contact.lastName) &&
46. organization.equals(contact.organization) &&
47. title.equals(contact.title)){
48. return true;
49. }
50. return false;
51. }
52. }
53.
54. public String toString(){
55. return firstName + SPACE + lastName + EOL_STRING + addresses;
56. }
57. }
RunPattern demonstrates how sessions can be used for communication between clients and servers. The main
method creates a server and two clients, and subsequently uses both clients to make edits on Contact objects,
adding and removing Address objects.
Example A.214 RunPattern.java
1. import java.io.IOException;
2. public class RunPattern{
3. public static void main(String [] arguments){
4. System.out.println("Example for the Session pattern");
5. System.out.println("This demonstration will show how a Session can be used");
6. System.out.println(" to organize a series of actions between a client and");
7. System.out.println(" server.");
8. System.out.println("In this case, clients will use sessions to coordinate");
9. System.out.println(" edits of Contact addresses.");
10. System.out.println();
11.
12. System.out.println("Running the RMI compiler (rmic)");
13. System.out.println();
14. try{
15. Process p1 = Runtime.getRuntime().exec("rmic SessionServerImpl");
16. p1.waitFor();
17. }
18. catch (IOException exc){
19. System.err.println("Unable to run rmic utility. Exiting application.");
20. System.exit(1);
21. }
22. catch (InterruptedException exc){
23. System.err.println("Threading problems encountered while using the rmic utility.");
24. }
25.
26. System.out.println("Starting the rmiregistry");
27. System.out.println();
28. Process rmiProcess = null;
29. try{
30. rmiProcess = Runtime.getRuntime().exec("rmiregistry");
31. Thread.sleep(15000);
32. }
33. catch (IOException exc){
34. System.err.println("Unable to start the rmiregistry. Exiting application.");
35. System.exit(1);
36. }
37. catch (InterruptedException exc){
38. System.err.println("Threading problems encountered when starting the rmiregistry.");
39. }
40.
41. System.out.println("Creating the SessionServer and two SessionClient objects");
42. System.out.println();
43. SessionServer serverObject = new SessionServerImpl();
44. SessionClient clientOne = new SessionClient();
45. SessionClient clientTwo = new SessionClient();
46.
47. System.out.println("Creating sample Contacts and Addresses");
48. System.out.println();
49. Contact firstContact = new ContactImpl("First", "Contact", "primo", "OOI", null);
50. Contact secondContact = new ContactImpl("Second", "Contact", "secondo", "OOI", null);
51. Address workAddress = new AddressImpl("Work address", "5440 Division", "Fargo", "ND",
"54321");
52. Address homeAddress = new AddressImpl("Home address", "40 Planar Way", "Paris", "TX",
"84301");
Search WWH ::




Custom Search