001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package dso;
005:
006: import com.tc.config.Directories;
007:
008: import java.io.FileInputStream;
009: import java.io.FileNotFoundException;
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.io.OutputStream;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import javax.servlet.http.HttpServlet;
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020:
021: public class DSODemoServlet extends HttpServlet {
022: private final static String SEP = System
023: .getProperty("file.separator");
024: private final static String REPEAT_FIELD = "repeat";
025: private final static String NUMBER1_FIELD = "number1";
026: private final static String NUMBER2_FIELD = "number2";
027: private final static String SIGN_FIELD = "sign";
028: private String HTML_PATH;
029: private SimpleSharedQueueExample workQueue = new SimpleSharedQueueExample();
030:
031: public void init() {
032: try {
033: HTML_PATH = Directories.getInstallationRoot() + SEP
034: + "demo" + SEP + "demo.html";
035: } catch (FileNotFoundException e) {
036: throw new RuntimeException(e);
037: }
038: }
039:
040: public void doPost(HttpServletRequest request,
041: HttpServletResponse response) throws IOException {
042: String pathInContext = request.getRequestURI();
043: if (pathInContext.endsWith("/add_work_item.asp")) {
044: final float number1 = getFloatForParameter(request,
045: NUMBER1_FIELD);
046: final float number2 = getFloatForParameter(request,
047: NUMBER2_FIELD);
048: final char sign = request.getParameter(SIGN_FIELD)
049: .charAt(0);
050: final int timesRepeat = getIntForParameter(request,
051: REPEAT_FIELD);
052: new Thread() {
053: public void run() {
054: workQueue.addAction(number1, number2, sign,
055: timesRepeat);
056: }
057: }.start();
058: response.sendRedirect(request.getServletPath());
059: return;
060: }
061:
062: if (pathInContext.endsWith("/clear_all.asp")) {
063: workQueue.clearResults();
064: response.sendRedirect(request.getServletPath());
065: return;
066: }
067: }
068:
069: public void doGet(HttpServletRequest request,
070: HttpServletResponse response) throws IOException {
071:
072: OutputStream out = response.getOutputStream();
073:
074: InputStream is = new FileInputStream(HTML_PATH);
075:
076: byte[] buffer = new byte[4096];
077: while (is.available() > 0) {
078: int read = is.read(buffer);
079: out.write(buffer, 0, read);
080: }
081:
082: // ARI: Make a summary table as well as a detail table.
083: HashMap resultsSummary = workQueue.getResultsSummary();
084: out.write("<hr><h1> L1 Client Summary</h1>\n".getBytes());
085: out
086: .write("<table>\n<tr><th>SERVER</th><th>EXECUTES</th><th>PUTS\n</th>\n</tr>"
087: .getBytes());
088:
089: for (Iterator i = resultsSummary.values().iterator(); i
090: .hasNext();) {
091: out.write(i.next().toString().getBytes());
092: }
093:
094: out.write("</table>\n".getBytes());
095: out.write("<hr> <h1>Last 15 Executions</h1>\n".getBytes());
096:
097: out.write("<ol>\n".getBytes());
098: List results = workQueue.getResults();
099: for (Iterator i = results.iterator(); i.hasNext();) {
100: out.write(("<li>" + i.next() + "</li>\n").getBytes());
101: }
102:
103: out.write("</ol></div></body></html>\n".getBytes());
104: }
105:
106: private float getFloatForParameter(HttpServletRequest request,
107: String name) {
108: String param = request.getParameter(name);
109: try {
110: return param == null ? 0 : Float.parseFloat(param);
111: } catch (NumberFormatException nfe) {
112: return 0;
113: }
114: }
115:
116: private int getIntForParameter(HttpServletRequest request,
117: String name) {
118: String param = request.getParameter(name);
119: try {
120: return param == null ? 0 : Integer.parseInt(param);
121: } catch (NumberFormatException nfe) {
122: return 0;
123: }
124: }
125: }
|