Java Reference
In-Depth Information
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);
44. appointments.add(appointment);
45. }
46. else {
47. ArrayList appointments = new ArrayList();
48. appointments.add(appointment);
49. appointmentCalendar.put(appointmentKey, appointments);
50. }
51. }
52. }
The CalendarImpl object must use the RMI support class UnicastRemoteObject so that it can handle incoming
communication requests. In this case, the CalendarImpl constructor exports itself using the static method
UnicastRemoteObject.exportObject .
CalendarImpl also needs to have some way of publishing itself to the outside world. In RMI, the naming service
is called the rmiregistry . It must be running before the CalendarImpl object is created. The rmiregistry is
like a telephone book, providing a connection between a name and an object. When the CalendarImpl object
registers itself with the rmiregistry through the rebind method it binds the name “calendarimp” to the stub of
this remote object.
For a client to use the remote object it has to do a lookup in the rmiregistry of the host machine and receive the
stub to the remote object. You can compare the stub to a telephone number. You can use that number from
anywhere, on any phone, and you get connected to someone answering the number you're calling. In this example,
the CalendarHOPP class acts as the client for the CalendarImpl object.
Example 3.31 CalendarHOPP.java
1. import java.rmi.Naming;
2. import java.rmi.RemoteException;
3. import java.util.Date;
4. import java.util.ArrayList;
5. public class CalendarHOPP implements Calendar, java.io.Serializable {
6. private static final String PROTOCOL = "rmi://";
7. private static final String REMOTE_SERVICE = "/calendarimpl";
8. private static final String HOPP_SERVICE = "calendar";
Search WWH ::




Custom Search