001: package example;
002:
003: import com.caucho.util.ThreadPool;
004: import com.caucho.util.ThreadTask;
005:
006: import java.io.*;
007:
008: import java.text.DateFormat;
009: import java.text.NumberFormat;
010:
011: import java.util.Date;
012: import java.util.logging.Level;
013: import java.util.logging.Logger;
014:
015: import java.util.concurrent.Executor;
016:
017: import javax.servlet.*;
018: import javax.servlet.http.*;
019: import javax.webbeans.In;
020:
021: /**
022: * A Servlet that provides a user interface for managing a PeriodicTask.
023: */
024: public class PeriodicTaskServlet extends HttpServlet {
025: static protected final Logger log = Logger
026: .getLogger(PeriodicTaskServlet.class.getName());
027:
028: int _refreshRate = 5;
029:
030: @In
031: private Executor _executor;
032:
033: @In
034: private PeriodicTask _periodicTask;
035:
036: private NumberFormat _numberFormat;
037: private DateFormat _dateFormat;
038:
039: public PeriodicTaskServlet() {
040: }
041:
042: /**
043: * The refresh rate in seconds to send to the browser to cause automatic
044: * refresh, default 5, <= 0 disables.
045: */
046: public void setRefreshRate(int refreshRate) {
047: _refreshRate = 5;
048: }
049:
050: public void init() throws ServletException {
051: _numberFormat = NumberFormat.getInstance();
052: _dateFormat = DateFormat.getInstance();
053:
054: String p;
055:
056: p = getInitParameter("refresh-rate");
057: if (p != null)
058: setRefreshRate(Integer.parseInt(p));
059: }
060:
061: protected void doGet(HttpServletRequest request,
062: HttpServletResponse response) throws ServletException,
063: IOException {
064: final PeriodicTask task = _periodicTask;
065:
066: String msg = null;
067:
068: if (request.getParameter("RUN") != null) {
069: if (task.isActive())
070: msg = "Already active.";
071: else {
072: // It's tricky to start another Thread from a Servlet. Here
073: // the Resin ThreadPool class is used. ThreadPool will interrupt a
074: // thread and stop it if it runs for too long.
075: ThreadTask threadTask = new ThreadTask() {
076: public void run() {
077: task.run();
078: }
079: };
080: _executor.execute(threadTask);
081: Thread.yield();
082: response.sendRedirect(request.getRequestURI());
083: }
084: }
085:
086: response.setContentType("text/html");
087: PrintWriter out = response.getWriter();
088:
089: // stop browser from caching the page
090: response.setHeader("Cache-Control",
091: "no-cache,post-check=0,pre-check=0");
092: response.setHeader("Pragma", "no-cache");
093: response.setHeader("Expires", "Thu,01Dec199416:00:00GMT");
094:
095: if (_refreshRate > 0) {
096: response.addHeader("refresh", String.valueOf(_refreshRate)
097: + "; URL=" + request.getRequestURI());
098: }
099:
100: out.println("<html>");
101: out.println("<head><title>PeriodicTask</title></head>");
102: out.println("<body>");
103: out.println("<h1>PeriodicTask</h1>");
104:
105: if (msg != null) {
106: out.print("<p><b>");
107: printSafeHtml(out, msg);
108: out.println("</b></p>");
109: }
110:
111: out.println("<table border='0'>");
112:
113: printHeading(out, "configuration");
114:
115: printPeriod(out, "estimated-average-time:", task
116: .getEstimatedAverageTime());
117:
118: printHeading(out, "statistics");
119:
120: printField(out, "active", task.isActive());
121: printPeriod(out, "estimated-time-remaining", task
122: .getEstimatedTimeRemaining());
123: printDate(out, "last-active-time", task.getLastActiveTime());
124: printField(out, "total-active-count", task
125: .getTotalActiveCount());
126: printPeriod(out, "total-active-time", task.getTotalActiveTime());
127: printPeriod(out, "average-active-time", task
128: .getAverageActiveTime());
129:
130: printHeading(out, "actions");
131: printActions(out, new String[][] { { "RUN", "Run" } });
132:
133: out.println("</table border='0'>");
134:
135: out.println("</body>");
136: out.println("</html>");
137: }
138:
139: protected void printHeading(PrintWriter out, String heading) {
140: out.print("<tr><td colspan='2'><h2>");
141: printSafeHtml(out, heading);
142: out.println("</h2></td></tr>");
143: }
144:
145: protected void printField(PrintWriter out, String name, String value) {
146: out.print("<tr><td>");
147: printSafeHtml(out, name);
148: out.print("</td><td>");
149: printSafeHtml(out, value);
150: out.print("</td></tr>");
151: }
152:
153: protected void printField(PrintWriter out, String name,
154: boolean value) {
155: printField(out, name, value == true ? "true" : "false");
156: }
157:
158: protected void printField(PrintWriter out, String name, long value) {
159: printField(out, name, _numberFormat.format(value));
160: }
161:
162: protected void printDate(PrintWriter out, String name, long date) {
163: printField(out, name, _dateFormat.format(new Date(date)));
164: }
165:
166: protected void printPeriod(PrintWriter out, String name, long millis) {
167: double sec = millis / 1000;
168: printField(out, name, _numberFormat.format(millis) + "sec");
169: }
170:
171: protected void printActions(PrintWriter out, String[][] actions) {
172: out.println("<tr><td colspan='2'><form>");
173: for (int i = 0; i < actions.length; i++) {
174: out.print("<input type='submit' name='");
175: printSafeHtml(out, actions[i][0]);
176: out.print("' value='");
177: printSafeHtml(out, actions[i][1]);
178: out.println("'/>");
179: }
180: out.println("</form></td></tr>");
181: }
182:
183: protected void printSafeHtml(PrintWriter out, String text) {
184: int len = text.length();
185:
186: for (int i = 0; i < len; i++) {
187: char ch = text.charAt(i);
188: switch (ch) {
189: case '<':
190: out.print("<");
191: break;
192: case '>':
193: out.print(">");
194: break;
195: case '&':
196: out.print("&");
197: break;
198: default:
199: out.print(ch);
200: }
201: }
202: }
203: }
|