Home | History | Annotate | Download | only in evolve
      1 package sample.evolve;
      2 
      3 import javassist.tools.web.*;
      4 import java.io.*;
      5 
      6 /**
      7  * A web server for demonstrating class evolution.  It must be
      8  * run with a DemoLoader.
      9  *
     10  * If a html file /java.html is requested, this web server calls
     11  * WebPage.show() for constructing the contents of that html file
     12  * So if a DemoLoader changes the definition of WebPage, then
     13  * the image of /java.html is also changed.
     14  * Note that WebPage is not an applet.  It is rather
     15  * similar to a CGI script or a servlet.  The web server never
     16  * sends the class file of WebPage to web browsers.
     17  *
     18  * Furthermore, if a html file /update.html is requested, this web
     19  * server overwrites WebPage.class (class file) and calls update()
     20  * in VersionManager so that WebPage.class is loaded into the JVM
     21  * again.  The new contents of WebPage.class are copied from
     22  * either sample/evolve/WebPage.class
     23  * or sample/evolve/sample/evolve/WebPage.class.
     24  */
     25 public class DemoServer extends Webserver {
     26 
     27     public static void main(String[] 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 ver0 = "sample/evolve/WebPage.class.0";
     44     private static final String ver1 = "sample/evolve/WebPage.class.1";
     45     private String currentVersion = ver0;
     46 
     47     public void doReply(InputStream in, OutputStream out, String 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 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