Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<%@ page import="java.lang.management.LockInfo" %>
<%@ page import="java.lang.management.ManagementFactory" %>
<%@ page import="java.lang.management.MonitorInfo" %>
<%@ page import="java.lang.management.ThreadInfo" %>
<%@ page import="java.lang.management.ThreadMXBean" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="java.net.Socket" %>
<%@ page import="java.util.Collection" %>
<%@ page import="com.documentum.fc.client.impl.connection.docbase.IDocbaseConnection" %>
<%@ page import="com.documentum.fc.client.impl.connection.docbase.IDocbaseRpcClient" %>
<%@ page import="com.documentum.fc.client.impl.session.ISession" %>
<%@ page import="com.documentum.fc.common.DfRuntimeException" %>
<%@ page import="com.documentum.fc.impl.RuntimeContext" %>
<%@ page import="com.documentum.fc.impl.util.io.MessageChannel" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Dfc Sessions</title>
</head>
<%!
public static <T> T getValue(Object object, String fieldName) {
try {
Class cls = null;
if (object instanceof Class) {
cls = (Class) object;
} else {
cls = object.getClass();
}
Field field = getField(cls, fieldName);
if (object instanceof Class) {
return (T) field.get(null);
}
return (T) field.get(object);
} catch (IllegalAccessException ex) {
throw DfRuntimeException.convertToRuntimeException(ex);
}
}
public static Field getField(Class<?> clazz, String fieldName) {
Class<?> cls = clazz;
while (true) {
try {
Field field = cls.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException ex) {
if (cls == Object.class) {
throw DfRuntimeException.convertToRuntimeException(ex);
}
cls = cls.getSuperclass();
}
}
}
public static String toString(Thread thread) {
StringBuilder sb = new StringBuilder("\"" + thread.getName() + "\"" +
" Id=" + thread.getId() + " " +
thread.getState());
StackTraceElement[] stackTrace = thread.getStackTrace();
for (int i = 0, n = stackTrace.length; i < n; i++) {
StackTraceElement ste = stackTrace[i];
sb.append("\tat ").append(ste.toString());
sb.append('\n');
}
sb.append("\n");
return sb.toString();
}
public static String toString(ThreadInfo threadInfo) {
StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" +
" Id=" + threadInfo.getThreadId() + " " +
threadInfo.getThreadState());
if (threadInfo.getLockName() != null) {
sb.append(" on ").append(threadInfo.getLockName());
}
if (threadInfo.getLockOwnerName() != null) {
sb.append(" owned by \"").append(threadInfo.getLockOwnerName()).append("\" Id=").append(threadInfo.getLockOwnerId());
}
if (threadInfo.isSuspended()) {
sb.append(" (suspended)");
}
if (threadInfo.isInNative()) {
sb.append(" (in native)");
}
sb.append('\n');
StackTraceElement[] stackTrace = threadInfo.getStackTrace();
for (int i = 0, n = stackTrace.length; i < n; i++) {
StackTraceElement ste = stackTrace[i];
sb.append("\tat ").append(ste.toString());
sb.append('\n');
if (i == 0 && threadInfo.getLockInfo() != null) {
Thread.State ts = threadInfo.getThreadState();
switch (ts) {
case BLOCKED:
sb.append("\t- blocked on ").append(threadInfo.getLockInfo());
sb.append('\n');
break;
case WAITING:
sb.append("\t- waiting on ").append(threadInfo.getLockInfo());
sb.append('\n');
break;
case TIMED_WAITING:
sb.append("\t- waiting on ").append(threadInfo.getLockInfo());
sb.append('\n');
break;
default:
}
}
for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
if (mi.getLockedStackDepth() == i) {
sb.append("\t- locked ").append(mi);
sb.append('\n');
}
}
}
LockInfo[] locks = threadInfo.getLockedSynchronizers();
if (locks.length > 0) {
sb.append("\n\tNumber of locked synchronizers = ").append(locks.length);
sb.append('\n');
for (LockInfo li : locks) {
sb.append("\t- ").append(li);
sb.append('\n');
}
}
sb.append('\n');
return sb.toString();
}
%>
<body>
<pre>
<%
Collection<ISession> sessions = RuntimeContext.getInstance().getSessionRegistry().getAllSessions();
ThreadMXBean tmb = ManagementFactory.getThreadMXBean();
for (ISession dfcSession : sessions) {
if (dfcSession == null) {
continue;
}
ISession realSession = getValue(dfcSession, "m_session");
if (realSession == null) {
continue;
}
IDocbaseConnection connection = getValue(realSession, "m_connection");
if (connection == null) {
continue;
}
IDocbaseRpcClient rpcClient = getValue(connection, "m_rpcClient");
if (rpcClient == null) {
continue;
}
MessageChannel messageChannel = getValue(rpcClient, "m_messageChannel");
if (messageChannel == null) {
continue;
}
Socket socket = getValue(messageChannel, "m_socket");
if (socket == null) {
continue;
}
Thread transactionThread = getValue(connection, "m_transactionThread");
out.write(String.format("Session: %s, user: %s, connected to: %s:%d", getValue(connection, "m_externalId"), realSession.getRawLoginInfo().getUser(), socket.getInetAddress(), socket.getPort()));
if (transactionThread != null) {
out.write(" has active transaction with following stack:\n");
ThreadInfo[] threadInfos = tmb.getThreadInfo(new long[]{transactionThread.getId()}, true, true);
ThreadInfo threadInfo = null;
if (threadInfos != null && threadInfos.length > 0) {
threadInfo = threadInfos[0];
}
if (threadInfo != null) {
out.write(toString(threadInfo));
} else {
out.write(toString(transactionThread));
}
out.write("\n");
}
out.write("\n");
}
%>
</pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.