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.

DemoServer.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package sample.evolve;
  2. import javassist.tools.web.*;
  3. import java.io.*;
  4. /**
  5. * A web server for demonstrating class evolution. It must be
  6. * run with a DemoLoader.
  7. *
  8. * If a html file /java.html is requested, this web server calls
  9. * WebPage.show() for constructing the contents of that html file
  10. * So if a DemoLoader changes the definition of WebPage, then
  11. * the image of /java.html is also changed.
  12. * Note that WebPage is not an applet. It is rather
  13. * similar to a CGI script or a servlet. The web server never
  14. * sends the class file of WebPage to web browsers.
  15. *
  16. * Furthermore, if a html file /update.html is requested, this web
  17. * server overwrites WebPage.class (class file) and calls update()
  18. * in VersionManager so that WebPage.class is loaded into the JVM
  19. * again. The new contents of WebPage.class are copied from
  20. * either sample/evolve/WebPage.class
  21. * or sample/evolve/sample/evolve/WebPage.class.
  22. */
  23. public class DemoServer extends Webserver {
  24. public static void main(String[] args) throws IOException
  25. {
  26. if (args.length == 1) {
  27. DemoServer web = new DemoServer(Integer.parseInt(args[0]));
  28. web.run();
  29. }
  30. else
  31. System.err.println(
  32. "Usage: java sample.evolve.DemoServer <port number>");
  33. }
  34. public DemoServer(int port) throws IOException {
  35. super(port);
  36. htmlfileBase = "sample/evolve/";
  37. }
  38. private static final String ver0 = "sample/evolve/WebPage.class.0";
  39. private static final String ver1 = "sample/evolve/WebPage.class.1";
  40. private String currentVersion = ver0;
  41. public void doReply(InputStream in, OutputStream out, String cmd)
  42. throws IOException, BadHttpRequest
  43. {
  44. if (cmd.startsWith("GET /java.html ")) {
  45. runJava(out);
  46. return;
  47. }
  48. else if (cmd.startsWith("GET /update.html ")) {
  49. try {
  50. if (currentVersion == ver0)
  51. currentVersion = ver1;
  52. else
  53. currentVersion = ver0;
  54. updateClassfile(currentVersion);
  55. VersionManager.update("sample.evolve.WebPage");
  56. }
  57. catch (CannotUpdateException e) {
  58. logging(e.toString());
  59. }
  60. catch (FileNotFoundException e) {
  61. logging(e.toString());
  62. }
  63. }
  64. super.doReply(in, out, cmd);
  65. }
  66. private void runJava(OutputStream outs) throws IOException {
  67. OutputStreamWriter out = new OutputStreamWriter(outs);
  68. out.write("HTTP/1.0 200 OK\r\n\r\n");
  69. WebPage page = new WebPage();
  70. page.show(out);
  71. out.close();
  72. }
  73. /* updateClassfile() copies the specified file to WebPage.class.
  74. */
  75. private void updateClassfile(String filename)
  76. throws IOException, FileNotFoundException
  77. {
  78. byte[] buf = new byte[1024];
  79. FileInputStream fin
  80. = new FileInputStream(filename);
  81. FileOutputStream fout
  82. = new FileOutputStream("sample/evolve/WebPage.class");
  83. for (;;) {
  84. int len = fin.read(buf);
  85. if (len >= 0)
  86. fout.write(buf, 0, len);
  87. else
  88. break;
  89. }
  90. }
  91. }