Java Reference
In-Depth Information
8. Naming.rebind(SESSION_SERVER_SERVICE_NAME, this);
9. }
10. catch (Exception exc){
11. System.err.println("Error using RMI to register the SessionServerImpl " + exc);
12. }
13. }
14.
15. public long addContact(Contact contact, long sessionID) throws SessionException{
16. return SessionServerDelegate.addContact(contact, sessionID);
17. }
18.
19. public long addAddress(Address address, long sessionID) throws SessionException{
20. return SessionServerDelegate.addAddress(address, sessionID);
21. }
22.
23. public long removeAddress(Address address, long sessionID) throws SessionException{
24. return SessionServerDelegate.removeAddress(address, sessionID);
25. }
26.
27. public long finalizeContact(long sessionID) throws SessionException{
28. return SessionServerDelegate.finalizeContact(sessionID);
29. }
30. }
Example 4.9 SessionServerDelegate.java
1. import java.util.ArrayList;
2. import java.util.HashMap;
3. public class SessionServerDelegate{
4. private static final long NO_SESSION_ID = 0;
5. private static long nextSessionID = 1;
6. private static ArrayList contacts = new ArrayList();
7. private static ArrayList addresses = new ArrayList();
8. private static HashMap editContacts = new HashMap();
9.
10. public static long addContact(Contact contact, long sessionID) throws SessionException{
11. if (sessionID <= NO_SESSION_ID){
12. sessionID = getSessionID();
13. }
14. if (contacts.indexOf(contact) != -1){
15. if (!editContacts.containsValue(contact)){
16. editContacts.put(new Long(sessionID), contact);
17. }
18. else{
19. throw new SessionException("This contact is currently being edited by another
user.",
20. SessionException.CONTACT_BEING_EDITED);
21. }
22. }
23. else{
24. contacts.add(contact);
25. editContacts.put(new Long(sessionID), contact);
26. }
27. return sessionID;
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. }
Search WWH ::




Custom Search