You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webdemo.html 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <html>
  2. <body>
  3. <h2>Remote Method Invocation</h2>
  4. <P>Javassist enables an applet to access a remote object as if it is a
  5. local object. The applet can communicate through a socket with the
  6. host that executes the web server distributing that applet. However,
  7. the applet cannot directly call a method on an object if the object is
  8. on a remote host. The <code>javassist.tools.rmi</code> package provides
  9. a mechanism for the applet to transparently access the remote object.
  10. The rules that the applet must be subject to are simpler than the
  11. standard Java RMI.
  12. <h3>1. Sample applet</h3>
  13. <P>The applet showing below is a simple number counter.
  14. If you press the button, the number is increased by one.
  15. An interesting feature of this applet is that the object
  16. recording the current number is contained by the web server
  17. written in Java. The applet must access the object through a socket
  18. to obtain the current number.
  19. <p><center>
  20. <applet codebase="http://localhost:5001"
  21. code="sample.rmi.CountApplet" width=200 height=200>
  22. <param name=name value="counter">
  23. <param name=button value="+1">
  24. </applet>
  25. </center>
  26. <p>However, the program of the applet does not need to directly handle
  27. a socket. The <code>ObjectImporter</code> provided by Javassist deals
  28. with all the awkward programming.
  29. Look at the lines shown with red:
  30. <p><b>Figure 1: Applet</b>
  31. <pre>
  32. <font color="red">import javassist.tools.rmi.ObjectImporter;</font>
  33. public class CountApplet extends Applet implements ActionListener {
  34. private Font font;
  35. <font color="red">private ObjectImporter importer;
  36. private Counter counter;</font>
  37. private AlertDialog dialog;
  38. private String message;
  39. public void init() {
  40. font = new Font("SansSerif", Font.ITALIC, 40);
  41. Button b = new Button(getParameter("button"));
  42. b.addActionListener(this);
  43. add(b);
  44. <font color="red">importer = new ObjectImporter(this);</font>
  45. dialog = new AlertDialog();
  46. message = "???";
  47. }
  48. public void start() {
  49. String counterName = getParameter("name");
  50. <font color="red">counter = (Counter)importer.getObject(counterName);</font>
  51. message = Integer.toString(<font color="red">counter.get()</font>);
  52. }
  53. /* The method called when the button is pressed.
  54. */
  55. public void actionPerformed(ActionEvent e) {
  56. message = Integer.toString(<font color="red">counter.increase()</font>);
  57. repaint();
  58. }
  59. public void paint(Graphics g) {
  60. g.setFont(font);
  61. g.drawRect(50, 50, 100, 100);
  62. g.setColor(Color.blue);
  63. g.drawString(message, 60, 120);
  64. }
  65. }
  66. </pre>
  67. <p>A <code>Counter</code> object running on a remote host
  68. maintains the counter number. To access this object, the applet first
  69. calls <code>getObject()</code> on an <code>ObjectImporter</code>
  70. to obtain a reference to the object. The parameter is the name associated
  71. with the object by the web server. Once the reference is obtained, it
  72. is delt with as if it is a reference to a local object.
  73. For example, <code>counter.get()</code> and <code>counter.increase()</code>
  74. call methods on the remote object.
  75. <p>The definition of the <code>Counter</code> class is also
  76. straightforward:
  77. <p><b>Figure 2: Remote object</b>
  78. <pre>
  79. public class Counter {
  80. private int count = 0;
  81. public int get() {
  82. return count;
  83. }
  84. public int increase() {
  85. count += 1;
  86. return count;
  87. }
  88. }
  89. </pre>
  90. <p>Note that the <code>javassist.tools.rmi</code> package does not require
  91. the <code>Counter</code> class to be an interface unlike the Java RMI,
  92. with which <code>Counter</code> must be an interface and it must be
  93. implemented by another class.
  94. <p>To make the <code>Counter</code> object available from the applet,
  95. it must be registered with the web server. A <code>AppletServer</code>
  96. object is a simple webserver that can distribute <code>.html</code> files
  97. and <code>.class</code> files (Java applets).
  98. <p><b>Figure 3: Server-side program</b>
  99. <pre>
  100. public class MyWebServer {
  101. public static void main(String[] args) throws IOException, CannotCompileException
  102. {
  103. AppletServer web = new AppletServer(args[0]);
  104. <font color="red">web.exportObject("counter", new Counter());</font>
  105. web.run();
  106. }
  107. }
  108. </pre>
  109. <p>The <code>exportObject()</code> method registers a remote object
  110. with the <code>AppletServer</code> object. In the example above,
  111. a <code>Counter</code> object is registered. The applet can access
  112. the object with the name "counter". The web server starts the service
  113. if the <code>run()</code> method is called.
  114. <p><br>
  115. <h3>2. Features</h3>
  116. The remote method invocation mechanism provided by Javassist has the
  117. following features:
  118. <ul>
  119. <li><b>Regular Java syntax:</b><br>
  120. The applet can call a method on a remote object with regular
  121. Java syntax.
  122. <p>
  123. <li><b>No special naming convention:</b><br>
  124. The applet can use the same class name as the server-side program.
  125. The reference object to a remote <code>Foo</code> object is
  126. also represented by the class <code>Foo</code>.
  127. Unlike other similar
  128. systems, it is not represented by a different class such as
  129. <code>ProxyFoo</code> or an interface implemented by
  130. <code>Foo</code>.
  131. <p>
  132. <li><b>No extra compiler:</b><br>
  133. All the programs, both the applet and the server-side program,
  134. are compiled by the regular Java compiler. No external compiler
  135. is needed.
  136. </ul>
  137. <p> With the Java RMI or Voyager, the applet programmer must define
  138. an interface for every remote object class and access the remote object
  139. through that interface.
  140. On the other hand, the <code>javassist.tools.rmi</code> package does not
  141. require the programmer to follow that programming convention.
  142. It is suitable for writing simple distributed programs like applets.
  143. <p><br>
  144. <h3>3. Inside of the system</h3>
  145. <p>A key idea of the implementation is that the applet and the server-side
  146. program must use different versions of the class <code>Counter</code>.
  147. The <code>Counter</code> object in the applet must work as a proxy
  148. object, which transfers the method invocations to the <code>Counter</code>
  149. object in the server-side program.
  150. <p>With other systems like the Java RMI, the class of this proxy object is
  151. produced by a special compiler such as <code>rmic</code>.
  152. It must be manually maintained by the programmer.
  153. <center><img src="inside.gif"></center>
  154. <p>However, Javassist automatically generates the proxy class at
  155. runtime so that the programmer does not have to be concerned about the
  156. maintenance of the proxy class.
  157. If the web browser running the applet
  158. requests to load the <code>Counter</code> class, which is the class
  159. of an exported object,
  160. then the web server
  161. transfers the version of <code>Counter</code> that Javassist generates
  162. as a proxy class.
  163. <p><br>
  164. </body>
  165. </html>