KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > evolve > DemoServer


1 package sample.evolve;
2
3 import javassist.*;
4 import javassist.web.*;
5 import java.io.*;
6
7 /**
8  * A web server for demonstrating class evolution. It must be
9  * run with a DemoLoader.
10  *
11  * If a html file /java.html is requested, this web server calls
12  * WebPage.show() for constructing the contents of that html file
13  * So if a DemoLoader changes the definition of WebPage, then
14  * the image of /java.html is also changed.
15  * Note that WebPage is not an applet. It is rather
16  * similar to a CGI script or a servlet. The web server never
17  * sends the class file of WebPage to web browsers.
18  *
19  * Furthermore, if a html file /update.html is requested, this web
20  * server overwrites WebPage.class (class file) and calls update()
21  * in VersionManager so that WebPage.class is loaded into the JVM
22  * again. The new contents of WebPage.class are copied from
23  * either WebPage.class.0 or WebPage.class.1.
24  */

25 public class DemoServer extends Webserver {
26
27     public static void main(String JavaDoc[] args) throws IOException
28     {
29     if (args.length == 1) {
30         DemoServer web = new DemoServer(Integer.parseInt(args[0]));
31         web.run();
32     }
33     else
34         System.err.println(
35         "Usage: java sample.evolve.DemoServer <port number>");
36     }
37
38     public DemoServer(int port) throws IOException {
39     super(port);
40     htmlfileBase = "sample/evolve/";
41     }
42
43     private static final String JavaDoc ver0 = "sample/evolve/WebPage.class.0";
44     private static final String JavaDoc ver1 = "sample/evolve/WebPage.class.1";
45     private String JavaDoc currentVersion = ver0;
46
47     public void doReply(InputStream in, OutputStream out, String JavaDoc cmd)
48     throws IOException, BadHttpRequest
49     {
50     if (cmd.startsWith("GET /java.html ")) {
51         runJava(out);
52         return;
53     }
54     else if (cmd.startsWith("GET /update.html ")) {
55         try {
56         if (currentVersion == ver0)
57             currentVersion = ver1;
58         else
59             currentVersion = ver0;
60
61         updateClassfile(currentVersion);
62         VersionManager.update("sample.evolve.WebPage");
63         }
64         catch (CannotUpdateException e) {
65         logging(e.toString());
66         }
67         catch (FileNotFoundException e) {
68         logging(e.toString());
69         }
70     }
71
72     super.doReply(in, out, cmd);
73     }
74
75     private void runJava(OutputStream outs) throws IOException {
76     OutputStreamWriter out = new OutputStreamWriter(outs);
77     out.write("HTTP/1.0 200 OK\r\n\r\n");
78     WebPage page = new WebPage();
79     page.show(out);
80     out.close();
81     }
82
83     /* updateClassfile() copies the specified file to WebPage.class.
84      */

85     private void updateClassfile(String JavaDoc filename)
86     throws IOException, FileNotFoundException
87     {
88     byte[] buf = new byte[1024];
89
90     FileInputStream fin
91         = new FileInputStream(filename);
92     FileOutputStream fout
93         = new FileOutputStream("sample/evolve/WebPage.class");
94     for (;;) {
95         int len = fin.read(buf);
96         if (len >= 0)
97         fout.write(buf, 0, len);
98         else
99         break;
100     }
101     }
102 }
103
Popular Tags