001: package dso;
002:
003: import org.mortbay.http.HttpContext;
004: import org.mortbay.http.HttpException;
005: import org.mortbay.http.HttpHandler;
006: import org.mortbay.http.HttpRequest;
007: import org.mortbay.http.HttpResponse;
008: import org.mortbay.http.HttpServer;
009: import org.mortbay.http.SocketListener;
010: import org.mortbay.http.handler.ResourceHandler;
011:
012: import com.tc.config.Directories;
013: import com.tc.util.Assert;
014:
015: import java.io.FileInputStream;
016: import java.io.FileNotFoundException;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.io.OutputStream;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: /*
025: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
026: */
027:
028: public class SharedQueueExampleHttpServer {
029: private SimpleSharedQueueExample workQueue;
030:
031: private final static String SEP = System
032: .getProperty("file.separator");
033: private final static String ACTION = "/action/";
034: private final String HTML_PATH;
035:
036: public SharedQueueExampleHttpServer() {
037: try {
038: HTML_PATH = Directories.getInstallationRoot() + SEP
039: + "demo" + SEP + "demo.html";
040: } catch (FileNotFoundException e) {
041: throw new RuntimeException(e);
042: }
043: }
044:
045: public static class DemoHttpHandler implements HttpHandler {
046:
047: private final static String REPEAT_FIELD = "repeat";
048: private final static String NUMBER1_FIELD = "number1";
049: private final static String NUMBER2_FIELD = "number2";
050: private final static String SIGN_FIELD = "sign";
051:
052: private HttpContext context;
053: private SimpleSharedQueueExample workQueue;
054: private boolean started = false;
055: private final String htmlPath;
056:
057: public DemoHttpHandler(SimpleSharedQueueExample workQueue,
058: String htmlPath) {
059: Assert.eval(workQueue != null);
060: this .workQueue = workQueue;
061: this .htmlPath = htmlPath;
062: }
063:
064: public String getName() {
065: return "Queue Http Handler";
066: }
067:
068: public HttpContext getHttpContext() {
069: return context;
070: }
071:
072: public void initialize(HttpContext initContext) {
073: this .context = initContext;
074: }
075:
076: public void handle(String pathInContext, String pathParams,
077: HttpRequest request, HttpResponse response)
078: throws HttpException, IOException {
079: if (pathInContext.equals("/add_work_item.asp")
080: && request.getParameterNames().size() == 4) {
081: final float number1 = getFloatForParameter(request,
082: NUMBER1_FIELD);
083: final float number2 = getFloatForParameter(request,
084: NUMBER2_FIELD);
085: final char sign = request.getParameter(SIGN_FIELD)
086: .charAt(0);
087: final int timesRepeat = getIntForParameter(request,
088: REPEAT_FIELD);
089: new Thread() {
090: public void run() {
091: workQueue.addAction(number1, number2, sign,
092: timesRepeat);
093: }
094: }.start();
095: response.sendRedirect(ACTION);
096: return;
097: }
098:
099: if (pathInContext.equals("/clear_all.asp")) {
100: workQueue.clearResults();
101: response.sendRedirect(ACTION);
102: return;
103: }
104:
105: InputStream is = new FileInputStream(htmlPath);
106: OutputStream os = response.getOutputStream();
107: byte[] buffer = new byte[4096];
108: while (is.available() > 0) {
109: int read = is.read(buffer);
110: os.write(buffer, 0, read);
111: }
112:
113: // ARI: Make a summary table as well as a detail table.
114: HashMap resultsSummary = workQueue.getResultsSummary();
115: os.write("<hr><h1> L1 Client Summary</h1>\n".getBytes());
116: os
117: .write("<table>\n<tr><th>SERVER</th><th>EXECUTES</th><th>PUTS\n</th>\n</tr>"
118: .getBytes());
119:
120: for (Iterator i = resultsSummary.values().iterator(); i
121: .hasNext();) {
122: os.write(i.next().toString().getBytes());
123: }
124:
125: os.write("</table>\n".getBytes());
126: os.write("<hr> <h1>Last 15 Executions</h1>\n".getBytes());
127:
128: os.write("<ol>\n".getBytes());
129: List results = workQueue.getResults();
130: for (Iterator i = results.iterator(); i.hasNext();) {
131: os.write(("<li>" + i.next() + "</li>\n").getBytes());
132: }
133:
134: os.write("</ol></div></body></html>\n".getBytes());
135:
136: response.commit();
137: request.setHandled(true);
138: }
139:
140: private float getFloatForParameter(HttpRequest request,
141: String name) {
142: String param = request.getParameter(name);
143: try {
144: return param == null ? 0 : Float.parseFloat(param);
145: } catch (NumberFormatException nfe) {
146: return 0;
147: }
148: }
149:
150: private int getIntForParameter(HttpRequest request, String name) {
151: String param = request.getParameter(name);
152: try {
153: return param == null ? 0 : Integer.parseInt(param);
154: } catch (NumberFormatException nfe) {
155: return 0;
156: }
157: }
158:
159: public void start() throws Exception {
160: this .started = true;
161: }
162:
163: public void stop() {
164: started = false;
165: }
166:
167: public boolean isStarted() {
168: return started;
169: }
170:
171: }
172:
173: public void start(int port) throws Exception {
174: HttpServer server = new HttpServer();
175: SocketListener listener = new SocketListener();
176: listener.setPort(port);
177: server.addListener(listener);
178: HttpContext context = server.addContext("/");
179: HttpContext actionContext = server.addContext(ACTION);
180: System.out.println("Setting resource base: " + HTML_PATH);
181: context.setResourceBase(HTML_PATH);
182: context.addHandler(new ResourceHandler());
183: this .workQueue = new SimpleSharedQueueExample();
184: HttpHandler handler = new DemoHttpHandler(workQueue, HTML_PATH);
185: actionContext.addHandler(handler);
186: server.start();
187: }
188:
189: public static void main(String[] args) throws Exception {
190: new SharedQueueExampleHttpServer().start(Integer
191: .parseInt(new String(args[0])));
192: }
193: }
|