public static synchronized void err(Throwable e) { String s = prefix() + e.toString(); err.println(s); ui.output.append(s + "\n"); for (StackTraceElement ste : e.getStackTrace()) { String s1 = "\t" + ste.toString(); err.println(s1); ui.output.append(s1 + "\n"); } ui.output.setCaretPosition(ui.output.getDocument().getLength()); err.flush(); }
protected static String printException(Throwable ex) throws IOException { if (ex == null) return null; StackTraceElement[] stack_trace = ex.getStackTrace(); StringBuilder sb = new StringBuilder(); sb.append("\n<" + CDATA + "\n"); sb.append(ex.getClass().getName() + " \n"); for (int i = 0; i < stack_trace.length; i++) { StackTraceElement frame = stack_trace[i]; sb.append("at " + frame.toString() + " \n"); } sb.append("\n]]>"); return sb.toString(); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { response.setHeader("Cache-Control", "no-cache"); response.setCharacterEncoding("UTF-8"); String task = request.getParameter("task"); Element data = null; // process help request if (request.getParameter("help") != null) data = getDescription(task); // redirect to home page if there is no task if (data == null && task == null) { response.setContentType("text/html"); response .getWriter() .append( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><meta http-equiv=\"REFRESH\" content=\"0;url=" + context.getInitParameter("server_path") + "></head><body></body></html>"); return; } // process definition request if (data == null && task.equals("define")) { int id = resolveIntegerArg(request.getParameter("id"), -1); int length = resolveIntegerArg(request.getParameter("length"), definer.getDefaultLength()); int format = resolveIntegerArg(request.getParameter("format"), definer.getDefaultFormat()); int maxImageWidth = resolveIntegerArg( request.getParameter("maxImageWidth"), definer.getDefaultMaxImageWidth()); int maxImageHeight = resolveIntegerArg( request.getParameter("maxImageHeight"), definer.getDefaultMaxImageHeight()); int linkDestination = resolveIntegerArg( request.getParameter("linkDestination"), definer.getDefaultLinkDestination()); boolean getImages = resolveBooleanArg(request.getParameter("getImages"), false); data = definer.getDefinition( id, length, format, linkDestination, getImages, maxImageWidth, maxImageHeight); } // all of the remaining tasks require data to be cached, so lets make sure that is finished // before continuing. if (!cachingThread.isOk()) throw new ServletException("Could not cache wikipedia data"); double progress = cachingThread.getProgress(); if (data == null && (progress < 1 || task.equals("progress"))) { // still caching up data, not ready to return a response yet. data = doc.createElement("loading"); data.setAttribute("progress", df.format(progress)); task = "loading"; } // process search request if (data == null && task.equals("search")) { String term = request.getParameter("term"); String id = request.getParameter("id"); int linkLimit = resolveIntegerArg(request.getParameter("linkLimit"), searcher.getDefaultMaxLinkCount()); int senseLimit = resolveIntegerArg( request.getParameter("senseLimit"), searcher.getDefaultMaxSenseCount()); if (id == null) data = searcher.doSearch(term, linkLimit, senseLimit); else data = searcher.doSearch(Integer.parseInt(id), linkLimit); } // process compare request if (data == null && task.equals("compare")) { String term1 = request.getParameter("term1"); String term2 = request.getParameter("term2"); int linkLimit = resolveIntegerArg(request.getParameter("linkLimit"), comparer.getDefaultMaxLinkCount()); boolean details = resolveBooleanArg(request.getParameter("details"), comparer.getDefaultShowDetails()); data = comparer.getRelatedness(term1, term2, details, linkLimit); } // process wikify request if (data == null && task.equals("wikify")) { if (this.wikifier == null) throw new ServletException( "Wikifier is not available. You must configure the servlet so that it has access to link detection and disambiguation models."); String source = request.getParameter("source"); int sourceMode = resolveIntegerArg(request.getParameter("sourceMode"), Wikifier.SOURCE_AUTODETECT); String linkColor = request.getParameter("linkColor"); String baseColor = request.getParameter("baseColor"); double minProb = resolveDoubleArg( request.getParameter("minProbability"), wikifier.getDefaultMinProbability()); int repeatMode = resolveIntegerArg(request.getParameter("repeatMode"), wikifier.getDefaultRepeatMode()); boolean showTooltips = resolveBooleanArg( request.getParameter("showTooltips"), wikifier.getDefaultShowTooltips()); String bannedTopics = request.getParameter("bannedTopics"); boolean wrapInXml = resolveBooleanArg(request.getParameter("wrapInXml"), true); if (wrapInXml) { data = wikifier.wikifyAndWrapInXML( source, sourceMode, minProb, repeatMode, bannedTopics, baseColor, linkColor, showTooltips); } else { response.setContentType("text/html"); response .getWriter() .append( wikifier.wikify( source, sourceMode, minProb, repeatMode, bannedTopics, baseColor, linkColor, showTooltips)); return; } } if (data == null) throw new Exception("Unknown Task"); // wrap data Element wrapper = doc.createElement("WikipediaMinerResponse"); wrapper.setAttribute("server_path", context.getInitParameter("server_path")); wrapper.setAttribute("service_name", context.getInitParameter("service_name")); wrapper.appendChild(data); data = wrapper; // Transform or serialize xml data as appropriate Transformer tf = null; if (request.getParameter("xml") == null) { // we need to transform the data into html tf = transformersByName.get(task); if (request.getParameter("help") != null) tf = transformersByName.get("help"); } if (tf == null) { // we need to serialize the data as xml tf = transformersByName.get("serializer"); response.setContentType("application/xml"); } else { // output will be transformed to html response.setContentType("text/html"); response .getWriter() .append( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"); } tf.transform(new DOMSource(data), new StreamResult(response.getWriter())); } catch (Exception error) { response.reset(); response.setContentType("application/xml"); response.setHeader("Cache-Control", "no-cache"); response.setCharacterEncoding("UTF8"); Element xmlError = doc.createElement("Error"); if (error.getMessage() != null) xmlError.setAttribute("message", error.getMessage()); Element xmlStackTrace = doc.createElement("StackTrace"); xmlError.appendChild(xmlStackTrace); for (StackTraceElement ste : error.getStackTrace()) { Element xmlSte = doc.createElement("StackTraceElement"); xmlSte.setAttribute("message", ste.toString()); xmlStackTrace.appendChild(xmlSte); } try { transformersByName .get("serializer") .transform(new DOMSource(xmlError), new StreamResult(response.getWriter())); } catch (Exception e) { // TODO: something for when an error is thrown processing an error???? } ; } }
public Writer getErrorReport( Writer to, final HttpServletRequest request, CharTransformer escape) throws IOException { final Writer logMsg = new StringWriter(); final Writer tee = new org.mmbase.util.ChainedWriter(to, logMsg); Writer msg = tee; LinkedList<Throwable> stack = getStack(); String ticket = new Date().toString(); Map<String, String> props; try { props = org.mmbase.util.ApplicationContextReader.getProperties("mmbase_errorpage"); } catch (javax.naming.NamingException ne) { props = Collections.emptyMap(); log.info(ne); } if (request != null) { { msg.append("Headers\n----------\n"); // request properties for (Object name : Collections.list(request.getHeaderNames())) { msg.append( escape.transform( name + ": " + escape.transform(request.getHeader((String) name)) + "\n")); } } { msg.append("\nAttributes\n----------\n"); Pattern p = requestIgnore; if (p == null && props.get("request_ignore") != null) { p = Pattern.compile(props.get("request_ignore")); } for (Object name : Collections.list(request.getAttributeNames())) { if (p == null || !p.matcher((String) name).matches()) { msg.append( escape.transform(name + ": " + request.getAttribute((String) name) + "\n")); } } } if (Boolean.TRUE.equals(showSession) || (showSession == null && !"false".equals(props.get("show_session")))) { HttpSession ses = request.getSession(false); if (ses != null) { msg.append("\nSession\n----------\n"); Pattern p = sessionIgnore; if (p == null && props.get("session_ignore") != null) { p = Pattern.compile(props.get("session_ignore")); } for (Object name : Collections.list(ses.getAttributeNames())) { if (p == null || !p.matcher((String) name).matches()) { msg.append(escape.transform(name + ": " + ses.getAttribute((String) name) + "\n")); } } } } } msg.append("\n"); msg.append("Misc. properties\n----------\n"); if (request != null) { msg.append("method: ").append(escape.transform(request.getMethod())).append("\n"); msg.append("querystring: ").append(escape.transform(request.getQueryString())).append("\n"); msg.append("requesturl: ") .append(escape.transform(request.getRequestURL().toString())) .append("\n"); } if (Boolean.TRUE.equals(showMMBaseVersion) || (showMMBaseVersion == null && !"false".equals(props.get("show_mmbase_version")))) { msg.append("mmbase version: ").append(org.mmbase.Version.get()).append("\n"); } msg.append("status: ").append("").append(String.valueOf(status)).append("\n\n"); if (request != null) { msg.append("Parameters\n----------\n"); // request parameters Enumeration en = request.getParameterNames(); while (en.hasMoreElements()) { String name = (String) en.nextElement(); msg.append(name) .append(": ") .append(escape.transform(request.getParameter(name))) .append("\n"); } } msg.append("\nException ") .append(ticket) .append("\n----------\n\n") .append( exception != null ? (escape.transform(exception.getClass().getName())) : "NO EXCEPTION") .append(": "); int wroteCauses = 0; while (!stack.isEmpty()) { Throwable t = stack.removeFirst(); // add stack stacktraces if (t != null) { if (stack.isEmpty()) { // write last message always msg = tee; } String message = t.getMessage(); if (msg != tee) { to.append("\n=== skipped(see log) : ") .append(escape.transform(t.getClass().getName())) .append(": ") .append(message) .append("\n"); } msg.append("\n\n").append(escape.transform(t.getClass().getName() + ": " + message)); StackTraceElement[] stackTrace = t.getStackTrace(); for (StackTraceElement e : stackTrace) { msg.append("\n at ").append(escape.transform(e.toString())); } if (!stack.isEmpty()) { msg.append("\n-------caused:\n"); } wroteCauses++; if (wroteCauses >= MAX_CAUSES) { msg = logMsg; } } } // write errors to log if (status == 500) { try { if (props.get("to") != null && props.get("to").length() > 0) { javax.naming.Context initCtx = new javax.naming.InitialContext(); javax.naming.Context envCtx = (javax.naming.Context) initCtx.lookup("java:comp/env"); Object mailSession = envCtx.lookup("mail/Session"); Class sessionClass = Class.forName("javax.mail.Session"); Class recipientTypeClass = Class.forName("javax.mail.Message$RecipientType"); Class messageClass = Class.forName("javax.mail.internet.MimeMessage"); Object mail = messageClass.getConstructor(sessionClass).newInstance(mailSession); messageClass .getMethod("addRecipients", recipientTypeClass, String.class) .invoke(mail, recipientTypeClass.getDeclaredField("TO").get(null), props.get("to")); messageClass.getMethod("setSubject", String.class).invoke(mail, ticket); mail.getClass().getMethod("setText", String.class).invoke(mail, logMsg.toString()); Class.forName("javax.mail.Transport") .getMethod("send", Class.forName("javax.mail.Message")) .invoke(null, mail); tee.append("\nmailed to (").append(String.valueOf(props)).append(")"); } } catch (Exception nnfe) { tee.append("\nnot mailed (").append(String.valueOf(nnfe)).append(")"); if (log.isDebugEnabled()) { log.debug(nnfe.getMessage(), nnfe); } } log.error("TICKET " + ticket + ":\n" + logMsg); } return to; }
private void printThread() { long now = System.currentTimeMillis(); long diffLastThreadDump = now - lastThreadDump; logger.info("diffLastThreadDump:" + diffLastThreadDump); if (diffLastThreadDump > 60000) { logger.info("had not sent all threads for a while.. will do so now"); lastThreadDump = now; try { ThreadMXBean t = ManagementFactory.getThreadMXBean(); long threads[] = t.getAllThreadIds(); ThreadInfo[] tinfo = t.getThreadInfo(threads, 40); StringBuilder sb = new StringBuilder("All Threads"); for (int i = 0; i < tinfo.length; i++) { ThreadInfo e = tinfo[i]; try { StackTraceElement[] el = e.getStackTrace(); sb.append( "\n\n" + e.getThreadName() + "\n" + " " + " Thread id = " + e.getThreadId() + " " + e.getThreadState()); if (e.getThreadState().equals(State.BLOCKED)) { sb.append( "\n\nBlocked info: " + e.getBlockedCount() + ":" + e.getBlockedTime() + ":" + e.getLockName() + ":" + e.getLockOwnerId() + ":" + e.getLockOwnerName() + "\n" + " " + " Thread id = " + e.getThreadId() + " " + e.getThreadState()); ThreadInfo eBlockedThread = t.getThreadInfo(e.getLockOwnerId(), 40); StackTraceElement[] elBlockedThread = eBlockedThread.getStackTrace(); sb.append( "\n\n " + e.getThreadName() + "\n" + " " + " Thread id = " + eBlockedThread.getThreadId() + " " + eBlockedThread.getThreadState()); if (elBlockedThread == null || elBlockedThread.length == 0) { sb.append(" no stack trace available"); } else { for (int n = 0; n < elBlockedThread.length; n++) { if (n != 0) sb.append("\n"); StackTraceElement frame = elBlockedThread[n]; if (frame == null) { sb.append(" null stack frame"); continue; } sb.append(" "); sb.append(frame.toString()); } } } if (el == null || el.length == 0) { sb.append(" no stack trace available"); continue; } for (int n = 0; n < el.length; n++) { if (n != 0) sb.append("\n"); StackTraceElement frame = el[n]; if (frame == null) { sb.append(" null stack frame"); continue; } sb.append(" "); sb.append(frame.toString()); } } catch (Exception e2) { } } String warningEmailReceiver = CmsPropertyHandler.getWarningEmailReceiver(); if (warningEmailReceiver != null && !warningEmailReceiver.equals("") && warningEmailReceiver.indexOf("@warningEmailReceiver@") == -1) { try { logger.info("Mailing.."); MailServiceFactory.getService() .sendEmail( CmsPropertyHandler.getMailContentType(), warningEmailReceiver, warningEmailReceiver, null, null, null, null, message, sb.toString().replaceAll("\n", "<br/>"), "utf-8"); } catch (Exception e) { logger.error("Could not send mail:" + e.getMessage(), e); } } } catch (Throwable e) { logger.error("Error generating message:" + e.getMessage(), e); } } // Only sends if the last stack was sent more than 3 seconds ago. if ((now - lastSentTimer) > 10000) { lastSentTimer = System.currentTimeMillis(); StackTraceElement[] el = targetThread.getStackTrace(); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); long stopTimeInNs = threadMXBean.getThreadUserTime(targetThread.getId()); long diff = (stopTimeInNs - startTimeInNs) / 1000000000; StringBuffer stackString = new StringBuffer("\n\n" + message + "\n\n"); stackString.append("ServerName: " + getServerName() + "\n"); stackString.append("Maximum memory (MB): " + (maxMemory / 1024 / 1024) + "\n"); stackString.append("Used memory (MB): " + ((totalMemory - freeMemory) / 1024 / 1024) + "\n"); stackString.append("Free memory (MB): " + (freeMemory / 1024 / 1024) + "\n"); stackString.append("Total memory (MB): " + (totalMemory / 1024 / 1024) + "\n"); stackString.append("Number of current requests: " + numberOfCurrentRequests + "\n"); stackString.append("Number of active requests: " + numberOfActiveRequests + "\n"); stackString.append("Number of long requests: " + longThreadMonitorsSize + "\n"); stackString.append("Current thread time: " + diff + " seconds\n"); stackString.append( "Average time: " + RequestAnalyser.getRequestAnalyser().getAverageElapsedTime() + "\n"); stackString.append( "Longest time: " + RequestAnalyser.getRequestAnalyser().getMaxElapsedTime() + "\n"); stackString.append("Original url: " + getOriginalFullURL() + "\n"); stackString.append("UserInfo: " + getUserInfo() + "\n"); stackString.append("--------------------------------------------\n\n"); stackString.append("Thread with id [" + targetThread.getId() + "] at report time:\n"); if (el != null && el.length != 0) { for (int j = 0; j < el.length; j++) { StackTraceElement frame = el[j]; if (frame == null) stackString.append(" null stack frame" + "\n"); else stackString.append(" ").append(frame.toString()).append("\n"); // if((stackString.indexOf("infoglue") > -1 && j > 20) || j > 35) // break; } } if (targetThread.getState().equals(State.BLOCKED)) { ThreadMXBean t = ManagementFactory.getThreadMXBean(); ThreadInfo e = t.getThreadInfo(targetThread.getId(), 40); stackString.append( "\n\nBlocked info: " + e.getBlockedCount() + ":" + e.getBlockedTime() + ":" + e.getLockName() + ":" + e.getLockOwnerId() + ":" + e.getLockOwnerName() + "\n" + " " + " Thread id = " + e.getThreadId() + " " + e.getThreadState()); ThreadInfo eBlockedThread = t.getThreadInfo(e.getLockOwnerId(), 40); StackTraceElement[] elBlockedThread = eBlockedThread.getStackTrace(); stackString.append( "\n\nBlocked thread: " + e.getThreadName() + "\n" + " " + " Thread id = " + eBlockedThread.getThreadId() + " " + eBlockedThread.getThreadState()); if (elBlockedThread == null || elBlockedThread.length == 0) { stackString.append(" no stack trace available"); } else { for (int n = 0; n < elBlockedThread.length; n++) { if (n != 0) stackString.append("\n"); StackTraceElement frame = elBlockedThread[n]; if (frame == null) { stackString.append(" null stack frame"); continue; } stackString.append(" "); stackString.append(frame.toString()); } } } stackString.append( "\n\n**********************************\nConcurrent long threads (Only an excerpt of all)\n**********************************"); ThreadMXBean t = ManagementFactory.getThreadMXBean(); List threadMonitors = RequestAnalyser.getLongThreadMonitors(); Iterator threadMonitorsIterator = threadMonitors.iterator(); int threadCount = 0; while (threadMonitorsIterator.hasNext() && threadCount < 5) { SimpleThreadMonitor tm = (SimpleThreadMonitor) threadMonitorsIterator.next(); if (targetThread.getId() == tm.getThreadId()) continue; long threads[] = {tm.getThreadId()}; ThreadInfo[] tinfo = t.getThreadInfo(threads, 40); stackString .append("\n\n---------------------------------\nConcurrent long thread [") .append(tm.getThreadId()) .append("]:\n"); stackString .append("Elapsed time:") .append(tm.getElapsedTime()) .append("\n Thread id: ") .append(tm.getThreadId()) .append("\n Original url: ") .append(tm.getOriginalFullURL()) .append(")"); for (int i = 0; i < tinfo.length; i++) { ThreadInfo e = tinfo[i]; el = e.getStackTrace(); if (el != null && el.length != 0) { for (int n = 0; n < el.length; n++) { StackTraceElement frame = el[n]; if (frame == null) stackString.append(" null stack frame\n"); else stackString.append(" null stack frame").append(frame.toString()).append("\n"); } } } threadCount++; } logger.warn(stackString); } else { logger.warn( "A thread took to long but the system seems to be really clogged so we don't send this one."); } }