/Users/lyon/j4p/src/javassist/sample/evolve/DemoServer.java

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