Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Session

In this example, the client requester uses the server to perform a series of operations for updating contact information in a shared address book. A user can perform four operations:

Add a contact

Add an address (associated with the current contact)

Remove an address (associated with the current contact)

Save the contact and address changes

These operations are defined in the class SessionClient.

Example A.205 SessionClient.java

1.import java.net.MalformedURLException;

2.import java.rmi.Naming;

3.import java.rmi.NotBoundException;

4.import java.rmi.RemoteException;

5.public class SessionClient{

6.private static final String SESSION_SERVER_SERVICE_NAME = "sessionServer";

7.private static final String SESSION_SERVER_MACHINE_NAME = "localhost";

8.private long sessionID;

9.private SessionServer sessionServer;

10.

11.public SessionClient(){

12.try{

13.

String url = "//" + SESSION_SERVER_MACHINE_NAME + "/" +

14.

 

SESSION_SERVER_SERVICE_NAME;

 

sessionServer = (SessionServer)Naming.lookup(url);

15.

}

 

 

 

 

16.

catch (RemoteException exc){}

 

 

17.

catch (NotBoundException exc){}

Y

 

18.

catch (MalformedURLException exc){}

 

19.

catch (ClassCastException exc){}

L

 

20.

}

 

 

F

 

21.

public void addContact(Contact contact)Mthrows SessionException{

22.

23.

try{

 

A

 

0);

24.

sessionID = sessionServer.addContact(contact,E

25.

}

T

 

 

 

26.

catch (RemoteException exc){}

 

 

27.

}

 

 

 

 

28.

 

 

 

 

 

29.public void addAddress(Address address) throws SessionException{

30.try{

31. sessionServer.addAddress(address, sessionID);

32.}

33.catch (RemoteException exc){}

34.}

35.

36.public void removeAddress(Address address) throws SessionException{

37.try{

38.sessionServer.removeAddress(address, sessionID);

39.}

40.catch (RemoteException exc){}

41.}

42.

43.public void commitChanges() throws SessionException{

44.try{

45. sessionID = sessionServer.finalizeContact(sessionID);

46.}

47.catch (RemoteException exc){}

48.}

49.}

Each client method calls a corresponding method on the remote server. SessionServer defines the four methods available to the clients through RMI.

Example A.206 SessionServer.java

TEAM FLY PRESENTS

 

331

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();

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.",

SessionException.CONTACT_BEING_EDITED);

20.

21.

}

22.}

23.else{

24.

contacts.add(contact);

25.

editContacts.put(new Long(sessionID), contact);

26.}

27.return sessionID;

332

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;

333

6. private int errorCode;

7.

8.public SessionException(String cause, int newErrorCode){

9.super(cause);

10.errorCode = newErrorCode;

11.}

12.public SessionException(String cause){ super(cause); }

13.

14.public int getErrorCode(){ return errorCode; }

15.}

The interfaces Address and Contact, and their implementing classes AddressImpl and ContactImpl, represent the business objects used in this example.

Example A.210 Address.java

1.import java.io.Serializable;

2.public interface Address extends Serializable{

3.public static final String EOL_STRING = System.getProperty("line.separator");

4.public static final String SPACE = " ";

5.public static final String COMMA = ",";

6.public String getType();

7.public String getDescription();

8.public String getStreet();

9.public String getCity();

10.public String getState();

11.public String getZipCode();

12.

13.public void setType(String newType);

14.public void setDescription(String newDescription);

15.public void setStreet(String newStreet);

16.public void setCity(String newCity);

17.public void setState(String newState);

18.public void setZipCode(String newZip);

19.}

Example A.211 AddressImpl.java

1.public class AddressImpl implements Address{

2.private String type;

3.private String description;

4.private String street;

5.private String city;

6.private String state;

7.private String zipCode;

8.

9.public AddressImpl(){ }

10.public AddressImpl(String newDescription, String newStreet,

11.String newCity, String newState, String newZipCode){

12.description = newDescription;

13.street = newStreet;

14.city = newCity;

15.state = newState;

16.zipCode = newZipCode;

17.}

18.

19.public String getType(){ return type; }

20.public String getDescription(){ return description; }

21.public String getStreet(){ return street; }

22.public String getCity(){ return city; }

23.public String getState(){ return state; }

24.public String getZipCode(){ return zipCode; }

25.

26.public void setType(String newType){ type = newType; }

27.public void setDescription(String newDescription){ description = newDescription; }

28.public void setStreet(String newStreet){ street = newStreet; }

29.public void setCity(String newCity){ city = newCity; }

30.public void setState(String newState){ state = newState; }

31.public void setZipCode(String newZip){ zipCode = newZip; }

32.

33.public boolean equals(Object o){

34.if (!(o instanceof AddressImpl)){

35.

return false;

36.}

37.else{

38. AddressImpl address = (AddressImpl)o;

39. if (street.equals(address.street) &&

40. city.equals(address.city) &&

334