<%@ page import="java.io.*, java.util.*"%>
<%@ page language="java" contentType="text/html;charset=utf-8"%>

<%!
    private ThreadGroup getRootThreadGroup() {
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        ThreadGroup parent = null;
        while(true) {
            parent = group.getParent();
            if(parent==null) return group;
            
            group = parent;
        }
    }   
    
    private ThreadGroup findThreadGroup(ThreadGroup parent, String path) {
        if(path.startsWith("/")) {
            path = path.substring(1);   
        }   
        
        return null;
    }
    
    
%>

<%
    String threadGroupName = request.getParameter("ThreadGroupName");
    
    ThreadGroup root = getRootThreadGroup();
    if(threadGroupName==null) threadGroupName=root.getName();
    
    ThreadGroup[] children = new ThreadGroup[root.activeGroupCount()];
    root.enumerate(children, true);
    
    ThreadGroup current = null;
    for(int i=0;i<children.length;i++) {
        if(threadGroupName.equals(children[i].getName())) {
            current = children[i];
            break;   
        }
    }
    if(current==null) current = root;
    
    Thread[] threads = new Thread[current.activeCount()];
    current.enumerate(threads, false);
%>
<html>
    <head>
        <title>
            CurrentThread List
        </title>
    </head>
    <body>
        <p>
            쓰레드 전체 리스트 보기
        </p>
        <table border='0' width='800'>
        <tr>
            <td>
            Thread Group List (총: <%=children.length%>개)
            <table border=0 cellspacing="1" cellpadding="1" bgcolor="black" width='800'>
            <tr bgcolor='yellow'>
                <td align='center' width='50'>No</td><td align='center'>ThreadGroup Name</td>
            </tr>
            <tr bgcolor='white'>
                <td align='center'>root</td>
                <td>&nbsp;<a href="viewThreadList.jsp"><%=root%></a></td>
            </tr>
<%
    for(int i=0;i<children.length;i++) {
%>
            <tr bgcolor='white'>
                <td align='center'><%=(i+1)%></td>
                <td>&nbsp;<a href="viewThreadList.jsp?ThreadGroupName=<%=children[i].getName()%>"><%=children[i].getName()%></a></td>
            </tr>
<%
    }
%>
            </table>
            </td>
        </tr>
        <tr>
            <td>   
            <br>
            Thread List [<%=current.getName()%>] (총: <%=threads.length%>개)
            <table border=0 cellspacing="1" cellpadding="1" bgcolor="black" width='800'>
            <tr bgcolor='yellow'>
                <td align='center' width='50'>No</td><td align='center'>Thread Name</td>
                <td align='center'>isAlive</td>
                <td align='center'>isDaemon</td>
                <td align='center'>isInterrupted</td>
                <td align='center'>activeCount</td>
            </tr>
<%
    for(int i=0;i<threads.length;i++) {
        if(threads[i]==null) continue;
%>
            <tr bgcolor='white'>
                <td align='center'><%=(i+1)%></td>
                <td>&nbsp;<%=threads[i].getName()%></td>
                <td>&nbsp;<%=threads[i].isAlive()%></td>
                <td>&nbsp;<%=threads[i].isDaemon()%></td>
                <td>&nbsp;<%=threads[i].isInterrupted()%></td>
                <td>&nbsp;<%=threads[i].activeCount()%></td>
            </tr>
<%
    }
%>
            </table>
            </td>
        </tr>
        </table>
    </body>
</html>