1 package sample.evolve; 2 3 import javassist.*; 4 import javassist.web.*; 5 import java.io.*; 6 7 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 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 | Popular Tags |