Java Reference
In-Depth Information
Half-Object Plus Protocol (HOPP)
A Personal Information Manager should be available everywhere, but its data should only be stored in one place.
This example uses RMI and the HOPP pattern to hold a personal calendar on a server, while making its
information available to remote callers.
The Calendar interface defines all methods that will be available remotely. This interface extends
java.rmi.Remote and all its methods throw java.rmi.RemoteException . In this case, Calendar defines three
methods: getHost , getAppointments , and addAppointment .
Example A.181 Calendar.java
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
3. import java.util.Date;
4. import java.util.ArrayList;
5. public interface Calendar extends Remote{
6. public String getHost() throws RemoteException;
7. public ArrayList getAppointments(Date date) throws RemoteException;
8. public void addAppointment(Appointment appointment, Date date) throws RemoteException;
9. }
Calendar is implemented by two classes—the RMI remote object and its stub, or proxy. (See “ Proxy ” on page
492.) The remote object class, CalendarImpl , provides method implementations, while the stub manages
communication to the remote object. The Java RMI compiler ( rmic ) needs to be run on the CalendarImpl to
generate a stub and a skeleton class. The skeleton class is provided for backward compatibility, but, as of Java 1.2,
is no longer necessary.
Example A.182 CalendarImpl.java
1. import java.rmi.Naming;
2. import java.rmi.server.UnicastRemoteObject;
3. import java.io.File;
4. import java.util.Date;
5. import java.util.ArrayList;
6. import java.util.HashMap;
7. public class CalendarImpl implements Calendar{
8. private static final String REMOTE_SERVICE = "calendarimpl";
9. private static final String DEFAULT_FILE_NAME = "calendar.ser";
10. private HashMap appointmentCalendar = new HashMap();
11.
12. public CalendarImpl(){
13. this(DEFAULT_FILE_NAME);
14. }
15. public CalendarImpl(String filename){
16. File inputFile = new File(filename);
17. appointmentCalendar = (HashMap)FileLoader.loadData(inputFile);
18. if (appointmentCalendar == null){
19. appointmentCalendar = new HashMap();
20. }
21. try {
22. UnicastRemoteObject.exportObject(this);
23. Naming.rebind(REMOTE_SERVICE, this);
24. }
25. catch (Exception exc){
26. System.err.println("Error using RMI to register the CalendarImpl " + exc);
27. }
28. }
29.
30. public String getHost(){ return ""; }
31. public ArrayList getAppointments(Date date){
32. ArrayList returnValue = null;
33. Long appointmentKey = new Long(date.getTime());
34. if (appointmentCalendar.containsKey(appointmentKey)){
35. returnValue = (ArrayList)appointmentCalendar.get(appointmentKey);
36. }
37. return returnValue;
38. }
39.
40. public void addAppointment(Appointment appointment, Date date){
41. Long appointmentKey = new Long(date.getTime());
42. if (appointmentCalendar.containsKey(appointmentKey)){
43. ArrayList appointments = (ArrayList)appointmentCalendar.get(appointmentKey);
 
 
Search WWH ::




Custom Search