📄 statustransformer.java
字号:
if (name.startsWith("//")) {
name = name.substring(2);
}
int slash = name.indexOf("/");
if (slash != -1) {
hostName = name.substring(0, slash);
contextName = name.substring(slash);
} else {
return;
}
ObjectName queryManager = new ObjectName
(objectName.getDomain() + ":type=Manager,path=" + contextName
+ ",host=" + hostName + ",*");
Set managersON = mBeanServer.queryNames(queryManager, null);
ObjectName managerON = null;
Iterator iterator2 = managersON.iterator();
while (iterator2.hasNext()) {
managerON = (ObjectName) iterator2.next();
}
ObjectName queryJspMonitor = new ObjectName
(objectName.getDomain() + ":type=JspMonitor,WebModule=" +
webModuleName + ",*");
Set jspMonitorONs = mBeanServer.queryNames(queryJspMonitor, null);
// Special case for the root context
if (contextName.equals("/")) {
contextName = "";
}
writer.print("<h1>");
writer.print(name);
writer.print("</h1>");
writer.print("</a>");
writer.print("<p>");
Object startTime = mBeanServer.getAttribute(objectName,
"startTime");
writer.print(" Start time: " +
new Date(((Long) startTime).longValue()));
writer.print(" Startup time: ");
writer.print(formatTime(mBeanServer.getAttribute
(objectName, "startupTime"), false));
writer.print(" TLD scan time: ");
writer.print(formatTime(mBeanServer.getAttribute
(objectName, "tldScanTime"), false));
if (managerON != null) {
writeManager(writer, managerON, mBeanServer, mode);
}
if (jspMonitorONs != null) {
writeJspMonitor(writer, jspMonitorONs, mBeanServer, mode);
}
writer.print("</p>");
String onStr = objectName.getDomain()
+ ":j2eeType=Servlet,WebModule=" + webModuleName + ",*";
ObjectName servletObjectName = new ObjectName(onStr);
Set set = mBeanServer.queryMBeans(servletObjectName, null);
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
ObjectInstance oi = (ObjectInstance) iterator.next();
writeWrapper(writer, oi.getObjectName(), mBeanServer, mode);
}
} else if (mode == 1){
// for now we don't write out the context in XML
}
}
/**
* Write detailed information about a manager.
*/
public static void writeManager(PrintWriter writer, ObjectName objectName,
MBeanServer mBeanServer, int mode)
throws Exception {
if (mode == 0) {
writer.print("<br>");
writer.print(" Active sessions: ");
writer.print(mBeanServer.getAttribute
(objectName, "activeSessions"));
writer.print(" Session count: ");
writer.print(mBeanServer.getAttribute
(objectName, "sessionCounter"));
writer.print(" Max active sessions: ");
writer.print(mBeanServer.getAttribute(objectName, "maxActive"));
writer.print(" Rejected session creations: ");
writer.print(mBeanServer.getAttribute
(objectName, "rejectedSessions"));
writer.print(" Expired sessions: ");
writer.print(mBeanServer.getAttribute
(objectName, "expiredSessions"));
writer.print(" Longest session alive time: ");
writer.print(formatSeconds(mBeanServer.getAttribute(
objectName,
"sessionMaxAliveTime")));
writer.print(" Average session alive time: ");
writer.print(formatSeconds(mBeanServer.getAttribute(
objectName,
"sessionAverageAliveTime")));
writer.print(" Processing time: ");
writer.print(formatTime(mBeanServer.getAttribute
(objectName, "processingTime"), false));
} else if (mode == 1) {
// for now we don't write out the wrapper details
}
}
/**
* Write JSP monitoring information.
*/
public static void writeJspMonitor(PrintWriter writer,
Set jspMonitorONs,
MBeanServer mBeanServer,
int mode)
throws Exception {
int jspCount = 0;
int jspReloadCount = 0;
Iterator iter = jspMonitorONs.iterator();
while (iter.hasNext()) {
ObjectName jspMonitorON = (ObjectName) iter.next();
Object obj = mBeanServer.getAttribute(jspMonitorON, "jspCount");
jspCount += ((Integer) obj).intValue();
obj = mBeanServer.getAttribute(jspMonitorON, "jspReloadCount");
jspReloadCount += ((Integer) obj).intValue();
}
if (mode == 0) {
writer.print("<br>");
writer.print(" JSPs loaded: ");
writer.print(jspCount);
writer.print(" JSPs reloaded: ");
writer.print(jspReloadCount);
} else if (mode == 1) {
// for now we don't write out anything
}
}
/**
* Write detailed information about a wrapper.
*/
public static void writeWrapper(PrintWriter writer, ObjectName objectName,
MBeanServer mBeanServer, int mode)
throws Exception {
if (mode == 0) {
String servletName = objectName.getKeyProperty("name");
String[] mappings = (String[])
mBeanServer.invoke(objectName, "findMappings", null, null);
writer.print("<h2>");
writer.print(servletName);
if ((mappings != null) && (mappings.length > 0)) {
writer.print(" [ ");
for (int i = 0; i < mappings.length; i++) {
writer.print(mappings[i]);
if (i < mappings.length - 1) {
writer.print(" , ");
}
}
writer.print(" ] ");
}
writer.print("</h2>");
writer.print("<p>");
writer.print(" Processing time: ");
writer.print(formatTime(mBeanServer.getAttribute
(objectName, "processingTime"), true));
writer.print(" Max time: ");
writer.print(formatTime(mBeanServer.getAttribute
(objectName, "maxTime"), false));
writer.print(" Request count: ");
writer.print(mBeanServer.getAttribute(objectName, "requestCount"));
writer.print(" Error count: ");
writer.print(mBeanServer.getAttribute(objectName, "errorCount"));
writer.print(" Load time: ");
writer.print(formatTime(mBeanServer.getAttribute
(objectName, "loadTime"), false));
writer.print(" Classloading time: ");
writer.print(formatTime(mBeanServer.getAttribute
(objectName, "classLoadTime"), false));
writer.print("</p>");
} else if (mode == 1){
// for now we don't write out the wrapper details
}
}
/**
* Filter the specified message string for characters that are sensitive
* in HTML. This avoids potential attacks caused by including JavaScript
* codes in the request URL that is often reported in error messages.
*
* @param obj The message string to be filtered
*/
public static String filter(Object obj) {
if (obj == null)
return ("?");
String message = obj.toString();
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
/**
* Display the given size in bytes, either as KB or MB.
*
* @param mb true to display megabytes, false for kilobytes
*/
public static String formatSize(Object obj, boolean mb) {
long bytes = -1L;
if (obj instanceof Long) {
bytes = ((Long) obj).longValue();
} else if (obj instanceof Integer) {
bytes = ((Integer) obj).intValue();
}
if (mb) {
long mbytes = bytes / (1024 * 1024);
long rest =
((bytes - (mbytes * (1024 * 1024))) * 100) / (1024 * 1024);
return (mbytes + "." + ((rest < 10) ? "0" : "") + rest + " MB");
} else {
return ((bytes / 1024) + " KB");
}
}
/**
* Display the given time in ms, either as ms or s.
*
* @param seconds true to display seconds, false for milliseconds
*/
public static String formatTime(Object obj, boolean seconds) {
long time = -1L;
if (obj instanceof Long) {
time = ((Long) obj).longValue();
} else if (obj instanceof Integer) {
time = ((Integer) obj).intValue();
}
if (seconds) {
return ((((float) time ) / 1000) + " s");
} else {
return (time + " ms");
}
}
/**
* Formats the given time (given in seconds) as a string.
*
* @param obj Time object to be formatted as string
*
* @return String formatted time
*/
public static String formatSeconds(Object obj) {
long time = -1L;
if (obj instanceof Long) {
time = ((Long) obj).longValue();
} else if (obj instanceof Integer) {
time = ((Integer) obj).intValue();
}
return (time + " s");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -