Java Reference
In-Depth Information
28. }
29.
30. public static long addAddress(Address address, long sessionID) throws SessionException
{
31. if (sessionID <= NO_SESSION_ID){
32. throw new SessionException("A valid session ID is required to add an address",
33. SessionException.SESSION_ID_REQUIRED);
34. }
35. Contact contact = (Contact)editContacts.get(new Long(sessionID));
36. if (contact == null){
37. throw new SessionException("You must select a contact before adding an address",
38. SessionException.CONTACT_SELECT_REQUIRED);
39. }
40. if (addresses.indexOf(address) == -1){
41. addresses.add(address);
42. }
43. contact.addAddress(address);
44. return sessionID;
45. }
46.
47. public static long removeAddress(Address address, long sessionID) throws
SessionException{
48. if (sessionID <= NO_SESSION_ID){
49. throw new SessionException("A valid session ID is required to remove an address",
50. SessionException.SESSION_ID_REQUIRED);
51. }
52. Contact contact = (Contact)editContacts.get(new Long(sessionID));
53. if (contact == null){
54. throw new SessionException("You must select a contact before removing an address",
55. SessionException.CONTACT_SELECT_REQUIRED);
56. }
57. if (addresses.indexOf(address) == -1){
58. throw new SessionException("There is no record of this address",
59. SessionException.ADDRESS_DOES_NOT_EXIST);
60. }
61. contact.removeAddress(address);
62. return sessionID;
63. }
64.
65. public static long finalizeContact(long sessionID) throws SessionException{
66. if (sessionID <= NO_SESSION_ID){
67. throw new SessionException("A valid session ID is required to finalize a contact",
68. SessionException.SESSION_ID_REQUIRED);
69. }
70. Contact contact = (Contact)editContacts.get(new Long(sessionID));
71. if (contact == null){
72. throw new SessionException("You must select and edit a contact before committing
changes",
73. SessionException.CONTACT_SELECT_REQUIRED);
74. }
75. editContacts.remove(new Long(sessionID));
76. return NO_SESSION_ID;
77. }
78.
79. private static long getSessionID(){
80. return nextSessionID++;
81. }
82.
83. public static ArrayList getContacts(){ return contacts; }
84. public static ArrayList getAddresses(){ return addresses; }
85. public static ArrayList getEditContacts(){ return new
ArrayList( editContacts.values()); }
86. }
SessionServerDelegate generates a session ID for clients when they perform their first operation, adding a
Contact . Subsequent operations on the Contact's addresses require the session ID, since the ID is used to
associate the addresses with a specific Contact within the SessionServerDelegate .
Any errors produced in the example are represented by using the SessionException class.
Example A.209 SessionException.java
1. public class SessionException extends Exception{
2. public static final int CONTACT_BEING_EDITED = 1;
3. public static final int SESSION_ID_REQUIRED = 2;
4. public static final int CONTACT_SELECT_REQUIRED = 3;
5. public static final int ADDRESS_DOES_NOT_EXIST = 4;
Search WWH ::




Custom Search