All Downloads are FREE. Search and download functionalities are using the official Maven repository.

clime.messadmin.providers.userdata.ThreadsDumper Maven / Gradle / Ivy

Go to download

Notification system and Session administration for J2EE Web Applications

There is a newer version: 4.1.1
Show newest version
/**
 * 
 */
package clime.messadmin.providers.userdata;

import java.lang.reflect.Method;

import clime.messadmin.providers.spi.ServerDataProvider;
import clime.messadmin.utils.StringUtils;

/**
 * Dumps all Threads in a pretty HTML page.
 * @author Cédrik LIME
 */
public class ThreadsDumper implements ServerDataProvider {
	private static Method getStackTrace;
	private static Method getId;
	private static Method getState;
	private static Method getDefaultUncaughtExceptionHandler;
	private static Method getUncaughtExceptionHandler;

	static {
		// @since 1.5
		try {
			getStackTrace = Thread.class.getMethod("getStackTrace", null);
			getId = Thread.class.getMethod("getId", null);
			getState = Thread.class.getMethod("getState", null);
			getDefaultUncaughtExceptionHandler = Thread.class.getMethod("getDefaultUncaughtExceptionHandler", null);
			getUncaughtExceptionHandler = Thread.class.getMethod("getUncaughtExceptionHandler", null);
		} catch (SecurityException e) {
		} catch (NoSuchMethodException e) {
		}

	}
	/**
	 * 
	 */
	public ThreadsDumper() {
		super();
	}

	/**
	 * {@inheritDoc}
	 */
	public int getPriority() {
		return 50;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getServerDataTitle() {
		return "Java Threads";
	}

	/**
	 * {@inheritDoc}
	 */
	public String getXHTMLServerData() {
		ThreadGroup tg = Thread.currentThread().getThreadGroup();
		while (tg.getParent() != null) {
			tg = tg.getParent();
		}
		StringBuffer buffer = new StringBuffer(tg.activeCount()*400 + tg.activeGroupCount()*120);
		Object defaultUncaughtExceptionHandler = invoke(getDefaultUncaughtExceptionHandler, null, null);
		if (defaultUncaughtExceptionHandler != null) {
			buffer.append("Threads Default Uncaught Exception Handler: ");
			buffer.append(StringUtils.escapeXml(defaultUncaughtExceptionHandler.toString()));
			buffer.append("
\n"); } buffer.append("
"); dump(tg, buffer); buffer.append("
"); return buffer.toString(); } private Object invoke(Method m, Object obj, Object[] args) { try { return m.invoke(obj, args); } catch (Exception e) { return null; } } protected void dump(Thread t, StringBuffer out) { ClassLoader cl = t.getContextClassLoader(); Long idL = (Long) invoke(getId, t, null); long id = (idL == null ? -1 : idL.longValue()); String name = StringUtils.escapeXml(t.getName()); int priority = t.getPriority(); // Object[] stackTrace = (Object[]) invoke(getStackTrace, t, null); Object state = invoke(getState, t, null); Object uncaughtExceptionHandler = invoke(getUncaughtExceptionHandler, t, null); boolean alive = t.isAlive(); boolean daemon = t.isDaemon(); boolean interrupted = t.isInterrupted(); String style = ""; if (daemon) { style += "list-style-type: circle;"; } else { style += "list-style-type: disc;"; } if (priority > Thread.NORM_PRIORITY) { String color = Integer.toHexString(5 + priority - Thread.NORM_PRIORITY); style += " color: #" + color + color + "0000;"; } if (priority < Thread.NORM_PRIORITY) { String color = Integer.toHexString(5 + Thread.NORM_PRIORITY - priority); style += " color: #" + color + color + color + color + color + color + ';'; } String infoballoonId = (idL != null ? Long.toString(id) : Long.toString(t.hashCode())+Long.toString(Math.round(100000*Math.random()))); out.append("
  • "); out.append(""); out.append(name); out.append(" ["); if (id >= 0) { // Java 5+ out.append("id=" + id); out.append(", state=" + state); out.append(", "); } out.append("class=" + t.getClass().getName()); out.append(", pri=" + priority); if (alive) out.append(", alive"); if (daemon) out.append(", daemon"); if (interrupted) out.append(", interrupted"); out.append(']'); out.append(""); out.append("
    "); out.append(""); out.append(""); if (uncaughtExceptionHandler != null && uncaughtExceptionHandler != t.getThreadGroup()) { out.append(""); } out.append("
    ClassLoader
    ").append(cl==null?null:StringUtils.escapeXml(cl.toString())).append("
    UncaughtExceptionHandler"); out.append(StringUtils.escapeXml(uncaughtExceptionHandler.toString())); out.append("
    "); out.append("
  • \n"); } protected void dump(ThreadGroup tg, StringBuffer out) { int maxPriority = tg.getMaxPriority(); String name = tg.getName(); boolean daemon = tg.isDaemon(); boolean destroyed = tg.isDestroyed(); out.append("
    "); out.append(name); out.append(" [class=" + tg.getClass().getName()); out.append(", maxpri=" + maxPriority); if (daemon) out.append(", daemon"); if (destroyed) out.append(", destroyed"); out.append(']'); out.append("
    \n
    "); int activeCount = tg.activeCount(); if (activeCount > 0) { out.append("Threads:
      \n"); Thread[] threads = new Thread[activeCount]; tg.enumerate(threads, false); for (int i = 0; i < activeCount; ++i) { if (threads[i] != null) { dump(threads[i], out); } } out.append("
    \n"); } int activeGroupCount = tg.activeGroupCount(); if (activeGroupCount > 0) { ThreadGroup[] threadGroups = new ThreadGroup[activeGroupCount]; tg.enumerate(threadGroups, false); out.append("ThreadGroups:
    "); for (int i = 0; i < activeGroupCount; ++i) { if (threadGroups[i] != null) { dump(threadGroups[i], out); } } out.append("
    \n"); } out.append("
    "); } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy