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