1 package dso; 2 3 import org.mortbay.http.HttpContext; 4 import org.mortbay.http.HttpException; 5 import org.mortbay.http.HttpHandler; 6 import org.mortbay.http.HttpRequest; 7 import org.mortbay.http.HttpResponse; 8 import org.mortbay.http.HttpServer; 9 import org.mortbay.http.SocketListener; 10 import org.mortbay.http.handler.ResourceHandler; 11 12 import com.tc.config.Directories; 13 import com.tc.util.Assert; 14 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 27 28 public class SharedQueueExampleHttpServer { 29 private SimpleSharedQueueExample workQueue; 30 31 private final static String SEP = System.getProperty("file.separator"); 32 private final static String ACTION = "/action/"; 33 private final String HTML_PATH; 34 35 public SharedQueueExampleHttpServer() { 36 try { 37 HTML_PATH = Directories.getInstallationRoot() + SEP + "demo" + SEP + "demo.html"; 38 } catch (FileNotFoundException e) { 39 throw new RuntimeException (e); 40 } 41 } 42 43 public static class DemoHttpHandler implements HttpHandler { 44 45 private final static String REPEAT_FIELD = "repeat"; 46 private final static String NUMBER1_FIELD = "number1"; 47 private final static String NUMBER2_FIELD = "number2"; 48 private final static String SIGN_FIELD = "sign"; 49 50 private HttpContext context; 51 private SimpleSharedQueueExample workQueue; 52 private boolean started = false; 53 private final String htmlPath; 54 55 public DemoHttpHandler(SimpleSharedQueueExample workQueue, String htmlPath) { 56 Assert.eval(workQueue != null); 57 this.workQueue = workQueue; 58 this.htmlPath = htmlPath; 59 } 60 61 public String getName() { 62 return "Queue Http Handler"; 63 } 64 65 public HttpContext getHttpContext() { 66 return context; 67 } 68 69 public void initialize(HttpContext initContext) { 70 this.context = initContext; 71 } 72 73 public void handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) 74 throws HttpException, IOException { 75 if (pathInContext.equals("/add_work_item.asp") && request.getParameterNames().size() == 4) { 76 final float number1 = getFloatForParameter(request, NUMBER1_FIELD); 77 final float number2 = getFloatForParameter(request, NUMBER2_FIELD); 78 final char sign = request.getParameter(SIGN_FIELD).charAt(0); 79 final int timesRepeat = getIntForParameter(request, REPEAT_FIELD); 80 new Thread () { 81 public void run() { 82 workQueue.addAction(number1, number2, sign, timesRepeat); 83 } 84 }.start(); 85 response.sendRedirect(ACTION); 86 return; 87 } 88 89 if (pathInContext.equals("/clear_all.asp")) { 90 workQueue.clearResults(); 91 response.sendRedirect(ACTION); 92 return; 93 } 94 95 InputStream is = new FileInputStream (htmlPath); 96 OutputStream os = response.getOutputStream(); 97 byte[] buffer = new byte[4096]; 98 while (is.available() > 0) { 99 int read = is.read(buffer); 100 os.write(buffer, 0, read); 101 } 102 103 HashMap resultsSummary = workQueue.getResultsSummary(); 105 os.write("<hr><h1> L1 Client Summary</h1>\n".getBytes()); 106 os.write("<table>\n<tr><th>SERVER</th><th>EXECUTES</th><th>PUTS\n</th>\n</tr>".getBytes()); 107 108 for (Iterator i = resultsSummary.values().iterator(); i.hasNext();) { 109 os.write(i.next().toString().getBytes()); 110 } 111 112 os.write("</table>\n".getBytes()); 113 os.write("<hr> <h1>Last 15 Executions</h1>\n".getBytes()); 114 115 os.write("<ol>\n".getBytes()); 116 List results = workQueue.getResults(); 117 for (Iterator i = results.iterator(); i.hasNext();) { 118 os.write(("<li>" + i.next() + "</li>\n").getBytes()); 119 } 120 121 os.write("</ol></div></body></html>\n".getBytes()); 122 123 response.commit(); 124 request.setHandled(true); 125 } 126 127 private float getFloatForParameter(HttpRequest request, String name) { 128 String param = request.getParameter(name); 129 try { 130 return param == null ? 0 : Float.parseFloat(param); 131 } catch (NumberFormatException nfe) { 132 return 0; 133 } 134 } 135 136 private int getIntForParameter(HttpRequest request, String name) { 137 String param = request.getParameter(name); 138 try { 139 return param == null ? 0 : Integer.parseInt(param); 140 } catch (NumberFormatException nfe) { 141 return 0; 142 } 143 } 144 145 public void start() throws Exception { 146 this.started = true; 147 } 148 149 public void stop() { 150 started = false; 151 } 152 153 public boolean isStarted() { 154 return started; 155 } 156 157 } 158 159 public void start(int port) throws Exception { 160 HttpServer server = new HttpServer(); 161 SocketListener listener = new SocketListener(); 162 listener.setPort(port); 163 server.addListener(listener); 164 HttpContext context = server.addContext("/"); 165 HttpContext actionContext = server.addContext(ACTION); 166 System.out.println("Setting resource base: " + HTML_PATH); 167 context.setResourceBase(HTML_PATH); 168 context.addHandler(new ResourceHandler()); 169 this.workQueue = new SimpleSharedQueueExample(); 170 HttpHandler handler = new DemoHttpHandler(workQueue, HTML_PATH); 171 actionContext.addHandler(handler); 172 server.start(); 173 } 174 175 public static void main(String [] args) throws Exception { 176 new SharedQueueExampleHttpServer().start(Integer.parseInt(new String (args[0]))); 177 } 178 } | Popular Tags |