Java Reference
In-Depth Information
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
3. public interface SessionServer extends Remote{
4. public long addContact(Contact contact, long sessionID) throws RemoteException,
SessionException;
5. public long addAddress(Address address, long sessionID) throws RemoteException,
SessionException;
6. public long removeAddress(Address address, long sessionID) throws RemoteException,
SessionException;
7. public long finalizeContact(long sessionID) throws RemoteException, SessionException;
8. }
SessionServerImpl implements the SessionServer interface, providing an RMI server. It delegates business
behavior to the class SessionServerDelegate .
Example A.207 SessionServerImpl.java
1. import java.rmi.Naming;
2. import java.rmi.server.UnicastRemoteObject;
3. public class SessionServerImpl implements SessionServer{
4. private static final String SESSION_SERVER_SERVICE_NAME = "sessionServer";
5. public SessionServerImpl(){
6. try {
7. UnicastRemoteObject.exportObject(this);
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 A.208 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;
Search WWH ::




Custom Search