001: /* Copyright 2003, 2003 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal;
007:
008: import java.io.IOException;
009: import java.io.PrintWriter;
010: import java.io.StringWriter;
011: import java.text.DateFormat;
012: import java.util.Iterator;
013: import java.util.TreeMap;
014:
015: import javax.servlet.ServletException;
016: import javax.servlet.http.HttpServlet;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: /**
024: * A servlet that reports portal problems.
025: * This servlet attempts to accomodate both the ErrorID approach and the
026: * display recent PortalExceptions approach.
027: * @author Howard Gilbert
028: * @author andrew.petro@yale.edu
029: * @version $Revision: 35312 $
030: */
031: public class Problems extends HttpServlet {
032:
033: private static final Log log = LogFactory.getLog(Problems.class);
034:
035: /**
036: * The simple formatter used to format the times of the PortalExceptions.
037: */
038: private DateFormat simpleTimeFormat = DateFormat
039: .getDateTimeInstance();
040:
041: protected void doGet(HttpServletRequest request,
042: HttpServletResponse response) throws ServletException,
043: IOException {
044:
045: response.setContentType("text/html");
046: PrintWriter out = response.getWriter();
047:
048: out
049: .println("<head><title>uPortal Problem Status</title></head>");
050: out.println("<body>");
051: out.println("<h1>uPortal Problem Status</h1>");
052:
053: String majorname = request.getParameter("major");
054: String minorname = request.getParameter("minor");
055: String peDetails = request.getParameter("peDetails");
056: boolean showPeDetails = ("true".equals(peDetails));
057:
058: if (majorname == null) {
059: out.println(listRecent());
060: out.println(listRecentPortalExceptions(showPeDetails));
061: out.println(listRegistered());
062: } else {
063: out.println(listdetail(majorname, minorname));
064: }
065:
066: out.println("</body>");
067:
068: }
069:
070: /**
071: * Generate HTML for stack trace and detail on one ID.
072: * @param majorname first key of ErrorID
073: * @param minorname second key of ErrorID
074: * @return HTML in String
075: */
076: private String listdetail(String majorname, String minorname) {
077: StringBuffer sb = new StringBuffer(1000);
078: TreeMap submap = (TreeMap) ProblemsTable.registeredIds
079: .get(majorname);
080: CountID countid = null;
081: if (submap != null) {
082: countid = (CountID) submap.get(minorname);
083: }
084: if (countid == null) {
085: return "No registered error with these parameters.";
086: }
087: sb.append("<pre>");
088: PortalException pe = countid.lastPortalException;
089: Throwable ex = pe.getCause();
090: if (ex == null)
091: ex = pe;
092: sb.append("at " + pe.getTimestamp().toString() + "\n");
093: sb.append(ExceptionHelper.errorInfo(pe.getErrorID(), pe
094: .getParameter(), ex));
095: sb.append("</pre>");
096: return sb.toString();
097: }
098:
099: /**
100: * List all registered ErrorIDs
101: * @return HTML String with list
102: */
103: private String listRegistered() {
104: StringBuffer sb = new StringBuffer(1000);
105: sb.append("<h2>Registered Error IDs</h2>");
106: Iterator minors = ProblemsTable.registeredIds.values()
107: .iterator();
108: sb.append("<table>");
109: while (minors.hasNext()) {
110: TreeMap minor = (TreeMap) minors.next();
111: Iterator ids = minor.values().iterator();
112: while (ids.hasNext()) {
113: CountID nextid = (CountID) ids.next();
114: sb.append("<tr><td>");
115: sb.append(nextid.errorID.category);
116: sb.append("</td><td>");
117: sb.append(nextid.errorID.specific);
118: sb.append("</td><td>");
119: if (nextid.count > 0) {
120: sb.append("<a href='problems?major="
121: + nextid.errorID.category + "&minor="
122: + nextid.errorID.specific + "'>");
123: }
124: sb.append(nextid.errorID.message);
125: if (nextid.count > 0)
126: sb.append("</a>");
127: sb.append("</td><td>");
128: sb.append(nextid.count);
129: sb.append("</td></tr>");
130: }
131: }
132: sb.append("</table>");
133: return sb.toString();
134: }
135:
136: private String listRecent() {
137: StringBuffer sb = new StringBuffer(1000);
138: sb.append("<h2>Recent Error IDs</h2>");
139: Iterator list = ProblemsTable.recentIds.iterator();
140: sb.append("<table>");
141: while (list.hasNext()) {
142: CountID nextid = (CountID) list.next();
143: sb.append("<tr><td>");
144: sb.append(nextid.errorID.category);
145: sb.append("</td><td>");
146: sb.append(nextid.errorID.specific);
147: sb.append("</td><td>");
148: if (nextid.count > 0) {
149: sb.append("<a href='problems?major="
150: + nextid.errorID.category + "&minor="
151: + nextid.errorID.specific + "'>");
152: }
153: sb.append(nextid.errorID.message);
154: if (nextid.count > 0)
155: sb.append("</a>");
156: sb.append("</td><td>");
157: sb.append(nextid.count);
158: sb.append("</td></tr>");
159: }
160: sb.append("</table>");
161: return sb.toString();
162: }
163:
164: private String listRecentPortalExceptions(boolean showStackTraces) {
165: StringBuffer sb = new StringBuffer();
166: sb.append("<h2>Recent PortalExceptions</h2>");
167:
168: Iterator peIter = ProblemsTable.getRecentPortalExceptions()
169: .iterator();
170: if (peIter.hasNext()) {
171: if (showStackTraces) {
172: sb
173: .append("<p><a href='problems?peDetails=false'>Click to view without stack traces</a></p>");
174: } else {
175: sb
176: .append("<p><a href='problems?peDetails=true'>Click to view with stack traces</a></p>");
177: }
178: sb.append("<table>");
179: while (peIter.hasNext()) {
180: PortalException portalException = (PortalException) peIter
181: .next();
182: sb.append("<tr>");
183: sb.append("<td>");
184: sb.append(this .simpleTimeFormat.format(portalException
185: .getTimestamp()));
186: sb.append("</td>");
187: sb.append("<td>");
188: sb.append(portalException.getClass().getName());
189: sb.append("</td>");
190: sb.append("<td>");
191: sb.append(portalException.getLocalizedMessage());
192: sb.append("</td>");
193: sb.append("</tr>");
194: if (showStackTraces) {
195: sb.append("<tr >");
196: sb.append("<td colspan='3'>");
197: sb.append("<pre>");
198:
199: // I so wish we were in JDK 1.4...
200: // this is boilerplate acrobatics to get our hands on the StackTrace
201: StringWriter stringWriter = new StringWriter();
202: PrintWriter printWriter = new PrintWriter(
203: stringWriter, false);
204: portalException.printStackTrace(printWriter);
205: try {
206: stringWriter.close();
207: } catch (IOException e) {
208: log
209: .error(
210: "Problem closing our StringWriter used to obtain the stack trace.",
211: e);
212: }
213: printWriter.close();
214: sb.append(stringWriter.toString());
215: sb.append("</pre>");
216: sb.append("</td>");
217: sb.append("</tr>");
218: }
219: }
220: sb.append("</table>");
221: }
222: return sb.toString();
223: }
224:
225: }
|