net.testdriven.psiprobe.controllers.threads.ListThreadsController.java Source code

Java tutorial

Introduction

Here is the source code for net.testdriven.psiprobe.controllers.threads.ListThreadsController.java

Source

/*
 * Licensed under the GPL License.  You may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
package net.testdriven.psiprobe.controllers.threads;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Context;
import org.springframework.web.servlet.ModelAndView;
import net.testdriven.psiprobe.controllers.TomcatContainerController;
import net.testdriven.psiprobe.model.java.ThreadModel;
import net.testdriven.psiprobe.tools.Instruments;

/**
 * 
 * @author Vlad Ilyushchenko
 */
public class ListThreadsController extends TomcatContainerController {
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //
        // create a list of webapp classloaders
        // this will help us to associate threads with applications.
        //
        List contexts = getContainerWrapper().getTomcatContainer().findContexts();
        Map<String, String> classLoaderMap = new TreeMap<>();
        for (Object context1 : contexts) {
            Context context = (Context) context1;
            if (context.getLoader() != null && context.getLoader().getClassLoader() != null) {
                classLoaderMap.put(toUID(context.getLoader().getClassLoader()), context.getName());
            }
        }

        return new ModelAndView(getViewName(), "threads", enumerateThreads(classLoaderMap));
    }

    private List enumerateThreads(final Map classLoaderMap) {

        //
        // get top ThreadGroup
        //
        ThreadGroup masterGroup = Thread.currentThread().getThreadGroup();
        while (masterGroup.getParent() != null) {
            masterGroup = masterGroup.getParent();
        }

        //
        // enumerate all Threads starting from top
        //
        List<ThreadModel> threadList = new ArrayList<>();

        Thread[] threads = new Thread[masterGroup.activeCount()];
        int numThreads = masterGroup.enumerate(threads);

        for (int i = 0; i < numThreads; i++) {
            ThreadModel threadModel = new ThreadModel();
            threadModel.setThreadClass(threads[i].getClass().getName());
            threadModel.setName(threads[i].getName());
            threadModel.setPriority(threads[i].getPriority());
            threadModel.setDaemon(threads[i].isDaemon());
            threadModel.setInterrupted(threads[i].isInterrupted());
            if (threads[i].getThreadGroup() != null) {
                threadModel.setGroupName(threads[i].getThreadGroup().getName());
            }
            Object target = Instruments.getField(threads[i], "target");
            if (target != null) {
                threadModel.setRunnableClassName(target.getClass().getName());
            }

            ClassLoader cl = threads[i].getContextClassLoader();
            if (cl != null) {
                if (classLoaderMap != null) {
                    threadModel.setAppName((String) classLoaderMap.get(toUID(cl)));
                }
                threadModel.setClassLoader(toUID(cl));
            }
            threadList.add(threadModel);
        }
        return threadList;
    }

    private static String toUID(Object o) {
        return o.getClass().getName() + "@" + o.hashCode();
    }
}