1 4 package dso; 5 6 import com.tc.config.Directories; 7 8 import java.io.FileInputStream ; 9 import java.io.FileNotFoundException ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import javax.servlet.http.HttpServlet ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 21 public class DSODemoServlet extends HttpServlet { 22 private final static String SEP = System.getProperty("file.separator"); 23 private final static String REPEAT_FIELD = "repeat"; 24 private final static String NUMBER1_FIELD = "number1"; 25 private final static String NUMBER2_FIELD = "number2"; 26 private final static String SIGN_FIELD = "sign"; 27 private String HTML_PATH; 28 private SimpleSharedQueueExample workQueue = new SimpleSharedQueueExample(); 29 30 public void init() { 31 try { 32 HTML_PATH = Directories.getInstallationRoot() + SEP + "demo" + SEP + "demo.html"; 33 } catch (FileNotFoundException e) { 34 throw new RuntimeException (e); 35 } 36 } 37 38 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 39 String pathInContext = request.getRequestURI(); 40 if (pathInContext.endsWith("/add_work_item.asp")) { 41 final float number1 = getFloatForParameter(request, NUMBER1_FIELD); 42 final float number2 = getFloatForParameter(request, NUMBER2_FIELD); 43 final char sign = request.getParameter(SIGN_FIELD).charAt(0); 44 final int timesRepeat = getIntForParameter(request, REPEAT_FIELD); 45 new Thread () { 46 public void run() { 47 workQueue.addAction(number1, number2, sign, timesRepeat); 48 } 49 }.start(); 50 response.sendRedirect(request.getServletPath()); 51 return; 52 } 53 54 if (pathInContext.endsWith("/clear_all.asp")) { 55 workQueue.clearResults(); 56 response.sendRedirect(request.getServletPath()); 57 return; 58 } 59 } 60 61 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 62 63 OutputStream out = response.getOutputStream(); 64 65 InputStream is = new FileInputStream (HTML_PATH); 66 67 byte[] buffer = new byte[4096]; 68 while (is.available() > 0) { 69 int read = is.read(buffer); 70 out.write(buffer, 0, read); 71 } 72 73 HashMap resultsSummary = workQueue.getResultsSummary(); 75 out.write("<hr><h1> L1 Client Summary</h1>\n".getBytes()); 76 out.write("<table>\n<tr><th>SERVER</th><th>EXECUTES</th><th>PUTS\n</th>\n</tr>".getBytes()); 77 78 for (Iterator i = resultsSummary.values().iterator(); i.hasNext();) { 79 out.write(i.next().toString().getBytes()); 80 } 81 82 out.write("</table>\n".getBytes()); 83 out.write("<hr> <h1>Last 15 Executions</h1>\n".getBytes()); 84 85 out.write("<ol>\n".getBytes()); 86 List results = workQueue.getResults(); 87 for (Iterator i = results.iterator(); i.hasNext();) { 88 out.write(("<li>" + i.next() + "</li>\n").getBytes()); 89 } 90 91 out.write("</ol></div></body></html>\n".getBytes()); 92 } 93 94 private float getFloatForParameter(HttpServletRequest request, String name) { 95 String param = request.getParameter(name); 96 try { 97 return param == null ? 0 : Float.parseFloat(param); 98 } catch (NumberFormatException nfe) { 99 return 0; 100 } 101 } 102 103 private int getIntForParameter(HttpServletRequest request, String name) { 104 String param = request.getParameter(name); 105 try { 106 return param == null ? 0 : Integer.parseInt(param); 107 } catch (NumberFormatException nfe) { 108 return 0; 109 } 110 } 111 } | Popular Tags |