001: // ThreadStatFrame.java
002: // $Id: ThreadStatFrame.java,v 1.8 2000/08/16 21:37:48 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.status;
007:
008: import java.util.Hashtable;
009:
010: import org.w3c.tools.resources.Attribute;
011: import org.w3c.tools.resources.AttributeRegistry;
012: import org.w3c.tools.resources.FramedResource;
013: import org.w3c.tools.resources.IntegerAttribute;
014: import org.w3c.tools.resources.Resource;
015: import org.w3c.tools.resources.ResourceFrame;
016:
017: import org.w3c.www.http.HTTP;
018:
019: import org.w3c.jigsaw.http.HTTPException;
020: import org.w3c.jigsaw.http.Reply;
021: import org.w3c.jigsaw.http.Request;
022:
023: import org.w3c.jigsaw.frames.HTTPFrame;
024:
025: import org.w3c.jigsaw.html.HtmlGenerator;
026:
027: /**
028: * The server thread status.
029: * This ought to be the client status, it will shortly (FIXME). It should uses
030: * a two frame display, one listing the clients, and the other one listing per
031: * client informations.
032: * <p>By the Way, this uses the nasty refresh stuff from netscape. It should
033: * perhaps be a servlet.
034: */
035:
036: public class ThreadStatFrame extends HTTPFrame {
037: protected static Integer REFRESH_DEFAULT = new Integer(5);
038:
039: /**
040: * Attribute index - Our refresh interval.
041: */
042: protected static int ATTR_REFRESH = -1;
043:
044: static {
045: Attribute a = null;
046: Class cls = null;
047: try {
048: cls = Class
049: .forName("org.w3c.jigsaw.status.ThreadStatFrame");
050: } catch (Exception ex) {
051: ex.printStackTrace();
052: System.exit(1);
053: }
054: // The refresh interval attribute:
055: a = new IntegerAttribute("refresh", new Integer(5),
056: Attribute.EDITABLE);
057: ATTR_REFRESH = AttributeRegistry.registerAttribute(cls, a);
058: }
059:
060: Runtime runtime = null;
061:
062: public void registerResource(FramedResource resource) {
063: super .registerOtherResource(resource);
064: }
065:
066: /**
067: * Dump the currenr threads into an HTML page.
068: * @param request The request we are to reply to.
069: */
070: public Reply listThreads(Request request) {
071: // enumerate all thread, and return a full thread listing:
072: int tcount = Thread.activeCount();
073: HtmlGenerator g = new HtmlGenerator("Thread status");
074: g.meta("Refresh", getValue(ATTR_REFRESH, REFRESH_DEFAULT)
075: .toString());
076: addStyleSheet(g);
077: // Dump thread informations:
078: Thread tarray[] = new Thread[tcount];
079: Thread.enumerate(tarray);
080: g.append("<h1>Thread dump</h1>");
081: g.append("<ul>");
082: String rname = getResource().getIdentifier();
083: for (int i = 0; i < tcount; i++) {
084: String name = tarray[i].getName();
085: g.append("<li>" + "<a href=\"" + rname + "?" + name + "\">"
086: + name + "</a>"
087: + (tarray[i].isDaemon() ? "D " : " ")
088: + (tarray[i].isAlive() ? "R " : " ") + " Prio: "
089: + (tarray[i].getPriority()));
090: }
091: g.append("</ul>");
092: // Dark features:
093: g
094: .append("<p>To kill a thread, add a <b>?</b><em>thread-name</em> "
095: + "to the current URL.</p>");
096: // Add global java process informations:
097: g.append("<h2>Misc informations</h2>");
098: g.append("<p>Total free memory: " + runtime.freeMemory());
099: g.append("<p>Toal memory : " + runtime.totalMemory());
100: g.close();
101: // Reply back:
102: Reply reply = request.makeReply(HTTP.OK);
103: reply.setStream(g);
104: return reply;
105: }
106:
107: protected Reply dumpThread(Request request) throws HTTPException {
108: // enumerate all thread, and return a full thread listing:
109: int tcount = Thread.activeCount();
110: Thread tarray[] = new Thread[tcount];
111: Thread.enumerate(tarray);
112: // Find ZE thread
113: String tname = request.getQueryString();
114: Thread queried = null;
115: for (int i = 0; i < tcount; i++) {
116: if (tarray[i].getName().equals(tname)) {
117: queried = tarray[i];
118: break;
119: }
120: }
121: if (queried == null) {
122: Reply reply = request.makeReply(HTTP.NOT_FOUND);
123: reply.setContent("Thread " + tname + " not found.");
124: return reply;
125: }
126: // Default to killing the thread
127: queried.destroy();
128: return listThreads(request);
129: }
130:
131: /**
132: * Get the threads.
133: * If a search string is present, kill the indicated thread, otherwise
134: * list the currently running threads.
135: * @param request The request to handle.
136: * @exception HTTPException If processing the request failed.
137: */
138: public Reply get(Request request) throws HTTPException {
139: if (request.hasQueryString()) {
140: return dumpThread(request);
141: } else {
142: return listThreads(request);
143: }
144: }
145:
146: /**
147: * Initialize the thread lister.
148: * Just get a pointer to our runtime object.
149: * @param values The default attribute values.
150: */
151: public void initialize(Object values[]) {
152: super.initialize(values);
153: runtime = Runtime.getRuntime();
154: }
155: }
|