Java Reference
In-Depth Information
38. ObjectOutputStream writeOut = new ObjectOutputStream(new
FileOutputStream(outputFile));
39. writeOut.writeObject(data);
40. writeOut.close();
41. }
42. catch (IOException exc){
43. exc.printStackTrace();
44. }
45. }
46. }
The RunPattern class demonstrates this pattern by creating CalendarHOPP and CalendarImpl objects. It uses
the CalendarHOPP to perform a local call ( getHost ), then as a way to access the remote resources—the stored
collection of appointments managed by the CalendarImpl object.
Example A.190 RunPattern.java
1. import java.util.Calendar;
2. import java.util.Date;
3. import java.util.ArrayList;
4. import java.io.IOException;
5. import java.rmi.RemoteException;
6. public class RunPattern{
7. private static Calendar dateCreator = Calendar.getInstance();
8. public static void main(String [] arguments) throws RemoteException{
9. System.out.println("Example for the HOPP pattern");
10. System.out.println();
11. System.out.println("This example will use RMI to demonstrate the HOPP pattern.");
12. System.out.println(" In the sample, there will be two objects created,
CalendarImpl");
13. System.out.println(" and CalendarHOPP. The CalendarImpl object provides the true");
14. System.out.println(" server-side implementation, while the CalendarHOPP would be");
15. System.out.println(" a client or middle-tier representative. The CalendarHOPP
will");
16. System.out.println(" provide some functionality, in this case supplying the
hostname");
17. System.out.println(" in response to the getHost method.");
18. System.out.println();
19. System.out.println("Note: This example runs the rmiregistry, CalendarHOPP and
CalendarImpl");
20. System.out.println(" on the same machine.");
21. System.out.println();
22.
23. try{
24. Process p1 = Runtime.getRuntime().exec("rmic CalendarImpl");
25. Process p2 = Runtime.getRuntime().exec("rmic CalendarHOPP");
26. p1.waitFor();
27. p2.waitFor();
28. }
29. catch (IOException exc){
30. System.err.println("Unable to run rmic utility. Exiting application.");
31. System.exit(1);
32. }
33. catch (InterruptedException exc){
34. System.err.println("Threading problems encountered while using the rmic
utility.");
35. }
36.
37. System.out.println("Starting the rmiregistry");
38. System.out.println();
39. Process rmiProcess = null;
40. try{
41. rmiProcess = Runtime.getRuntime().exec("rmiregistry");
42. Thread.sleep(15000);
43. }
44. catch (IOException exc){
45. System.err.println("Unable to start the rmiregistry. Exiting application.");
46. System.exit(1);
47. }
48. catch (InterruptedException exc){
49. System.err.println("Threading problems encountered when starting the
rmiregistry.");
50. }
51.
52. System.out.println("Creating the CalendarImpl object, which provides the
server-side implementation.");
Search WWH ::




Custom Search