KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dso > SharedQueueExampleHttpServer


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 JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 /*
25  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
26  */

27
28 public class SharedQueueExampleHttpServer {
29   private SimpleSharedQueueExample workQueue;
30
31   private final static String JavaDoc SEP = System.getProperty("file.separator");
32   private final static String JavaDoc ACTION = "/action/";
33   private final String JavaDoc HTML_PATH;
34
35   public SharedQueueExampleHttpServer() {
36     try {
37       HTML_PATH = Directories.getInstallationRoot() + SEP + "demo" + SEP + "demo.html";
38     } catch (FileNotFoundException JavaDoc e) {
39       throw new RuntimeException JavaDoc(e);
40     }
41   }
42
43   public static class DemoHttpHandler implements HttpHandler {
44
45     private final static String JavaDoc REPEAT_FIELD = "repeat";
46     private final static String JavaDoc NUMBER1_FIELD = "number1";
47     private final static String JavaDoc NUMBER2_FIELD = "number2";
48     private final static String JavaDoc SIGN_FIELD = "sign";
49
50     private HttpContext context;
51     private SimpleSharedQueueExample workQueue;
52     private boolean started = false;
53     private final String JavaDoc htmlPath;
54
55     public DemoHttpHandler(SimpleSharedQueueExample workQueue, String JavaDoc htmlPath) {
56       Assert.eval(workQueue != null);
57       this.workQueue = workQueue;
58       this.htmlPath = htmlPath;
59     }
60
61     public String JavaDoc 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 JavaDoc pathInContext, String JavaDoc pathParams, HttpRequest request, HttpResponse response)
74         throws HttpException, IOException JavaDoc {
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 JavaDoc() {
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 JavaDoc is = new FileInputStream JavaDoc(htmlPath);
96       OutputStream JavaDoc 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       // ARI: Make a summary table as well as a detail table.
104
HashMap JavaDoc 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 JavaDoc 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 JavaDoc results = workQueue.getResults();
117       for (Iterator JavaDoc 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 JavaDoc name) {
128       String JavaDoc param = request.getParameter(name);
129       try {
130         return param == null ? 0 : Float.parseFloat(param);
131       } catch (NumberFormatException JavaDoc nfe) {
132         return 0;
133       }
134     }
135
136     private int getIntForParameter(HttpRequest request, String JavaDoc name) {
137       String JavaDoc param = request.getParameter(name);
138       try {
139         return param == null ? 0 : Integer.parseInt(param);
140       } catch (NumberFormatException JavaDoc nfe) {
141         return 0;
142       }
143     }
144
145     public void start() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc[] args) throws Exception JavaDoc {
176     new SharedQueueExampleHttpServer().start(Integer.parseInt(new String JavaDoc(args[0])));
177   }
178 }
Popular Tags