Example usage for java.lang Thread getAllStackTraces

List of usage examples for java.lang Thread getAllStackTraces

Introduction

In this page you can find the example usage for java.lang Thread getAllStackTraces.

Prototype

public static Map<Thread, StackTraceElement[]> getAllStackTraces() 

Source Link

Document

Returns a map of stack traces for all live threads.

Usage

From source file:br.com.uol.runas.classloader.ClassLoaderGC.java

private void releaseFromThreads(WeakReference<ClassLoader> classLoader) {
    final List<Thread> threads = new ArrayList<Thread>(Thread.getAllStackTraces().keySet());

    for (Thread thread : threads) {
        if (Objects.equals(classLoader.get(), thread.getContextClassLoader())) {
            thread.setContextClassLoader(null);
        }/*w  ww .j  a v  a  2  s . c om*/
    }
}

From source file:org.pepstock.jem.ant.tasks.AntBatchSecurityManager.java

/**
 * Scans the stack trace element to understand if setSecurity manager is allowed.
 * <br>//from   w  ww  . ja  v a  2s.  c o m
 * ONLY ANT engine can set security manager and it does with
 * <code>org.apache.tools.ant.types.Permissions</code> class.
 * <br>
 * if it arrives with <code>org.apache.tools.ant.types.Permissions$MySM</code> means that
 * ANT security manager delegates us to check but we can't do it.
 * <br>
 * If it tries to change it by a ANT task, is not allowed!
 * 
 * @return <code>true</code> if ANT is initializing the task otherwise o=always false.
 */
private boolean isAllowedSetSecurityManager() {
    StackTraceElement[] elements = Thread.getAllStackTraces().get(Thread.currentThread());
    boolean thisFound = false;
    for (StackTraceElement element : elements) {
        // before must be in the stack trace this class
        if (element.getClassName().equalsIgnoreCase(getClass().getName())) {
            thisFound = true;
            // checks if is called by Security manager of ANT
            // if yes, it's delegating... that means that some one try to set the security manager
            // and this is NOT allowed.
        } else if (element.getClassName().equalsIgnoreCase(ANT_SECURITY_MANAGER) && thisFound) {
            return false;
            // if it is called by the permission, that means teh ANT is intsalling own security
            // manager, MySM, and that's ALLOWED!!
        } else if (element.getClassName().equalsIgnoreCase(ANT_PERMISSIONS) && thisFound) {
            return true;
        }
    }
    return false;
}

From source file:com.ilscipio.scipio.common.FileListener.java

public static Thread getThreadByName(String threadName) {
    for (Thread t : Thread.getAllStackTraces().keySet()) {
        if (t.getName().equals(threadName))
            return t;
    }//from   w  w  w  . j a  v a  2s .c  o m
    return null;
}

From source file:org.jajuk.JajukTestCase.java

@Override
protected void tearDown() throws Exception {
    Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
    Iterator<Thread> i = traces.keySet().iterator();
    while (i.hasNext()) {
        Thread thd = i.next();//  w  ww .j  a v a  2s  . c  o  m
        if (thd.getName().contains("MPlayer reader thread")
                || thd.getName().contains("MPlayer writer thread")) {
            TestHelpers.dumpThreads();
            throw new IllegalStateException("Had leftover MPlayer thread: " + thd.getName());
        }
    }
    super.tearDown();
}

From source file:org.apache.hadoop.thriftfs.ThriftHandlerBase.java

/**
 * Return a list of threads that currently exist with their stack traces
 *//* w  w w .java2 s  .  c  om*/
public List<ThreadStackTrace> getThreadDump(RequestContext ctx) {
    List<ThreadStackTrace> dump = new ArrayList<ThreadStackTrace>();

    Map<Thread, java.lang.StackTraceElement[]> traces = Thread.getAllStackTraces();
    for (Map.Entry<Thread, java.lang.StackTraceElement[]> entry : traces.entrySet()) {
        final Thread t = entry.getKey();
        final java.lang.StackTraceElement[] frames = entry.getValue();

        ThreadStackTrace tst = new ThreadStackTrace();
        tst.threadName = t.getName();
        tst.threadStringRepresentation = String.valueOf(t);
        tst.isDaemon = t.isDaemon();
        tst.stackTrace = new ArrayList<StackTraceElement>();
        for (java.lang.StackTraceElement ste : frames) {
            StackTraceElement tFrame = new StackTraceElement();
            tFrame.className = ste.getClassName();
            tFrame.fileName = ste.getFileName();
            tFrame.lineNumber = ste.getLineNumber();
            tFrame.methodName = ste.getMethodName();
            tFrame.isNativeMethod = ste.isNativeMethod();
            tFrame.stringRepresentation = String.valueOf(ste);
            tst.stackTrace.add(tFrame);
        }
        dump.add(tst);
    }
    return dump;
}

From source file:controller.servlet.AllDataDelete.java

/**
 * Mtodo stopThreads, permite parar los hilos en ejecucin.
 *//*from w  w  w  .java  2  s.co m*/
private void stopThreads() {
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    for (Thread thread : threadArray) {
        if (thread.getName().equals(ThreadName.TRACK_THREAD_NAME)
                || thread.getName().equals(ThreadName.AMENITIES_THREAD_NAME)) {
            thread.interrupt();
            thread.stop();
        }
    }
}

From source file:org.xtreemfs.foundation.util.OutputUtils.java

public static String getThreadDump() {
    StringBuilder sb = new StringBuilder();
    sb.append("<HTML><BODY><H1>THREAD STATES</H1><PRE>");
    final Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
    for (Thread t : traces.keySet()) {
        sb.append("<B>thread: ");
        sb.append(t.getName());//from  w  w  w  .  j  av a  2  s.c o m
        sb.append("</B>\n");
        final StackTraceElement[] elems = traces.get(t);
        for (int i = elems.length - 1; i >= 0; i--) {
            sb.append(elems[i].toString());
            sb.append("\n");
        }
        sb.append("\n");
    }
    sb.append("</PRE></BODY></HTML>");
    return sb.toString();
}

From source file:com.moss.greenshell.wizard.catastrophe.PostMortemScreen.java

public static void submitErrorReport(final Throwable cause, final ErrorReportDecorator... decorators)
        throws Exception {

    List<ErrorReportChunk> chunks = new LinkedList<ErrorReportChunk>();

    try {//from w  w w.  ja v a  2  s .  co m
        if (cause instanceof InternalErrorException) {
            InternalErrorException ie = (InternalErrorException) cause;
            ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                    ie.id().getBytes("UTF8"));
            chunks.add(chunk);
        } else if (cause instanceof SOAPFaultException) {
            SOAPFaultException soapFault = (SOAPFaultException) cause;
            String content = soapFault.getFault().getFirstChild().getTextContent();
            String prefix = "Internal Service Error Occurred: ";
            if (content.startsWith(prefix)) {
                String id = content.substring(prefix.length());
                ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                        id.getBytes("UTF8"));
                chunks.add(chunk);
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    // STACK TRACE
    ByteArrayOutputStream stackBytes = new ByteArrayOutputStream();
    PrintStream stackPrintStream = new PrintStream(stackBytes);
    cause.printStackTrace(stackPrintStream);
    stackPrintStream.close();
    stackBytes.close();

    ErrorReportChunk chunk = new ErrorReportChunk("stack trace", "text/plain", stackBytes.toByteArray());
    chunks.add(chunk);

    // THREAD DUMP

    ByteArrayOutputStream dumpBytes = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(dumpBytes);
    Map<Thread, StackTraceElement[]> traceMap = Thread.getAllStackTraces();
    for (Map.Entry<Thread, StackTraceElement[]> next : traceMap.entrySet()) {
        out.println();
        out.println(next.getKey().getName());
        for (StackTraceElement line : next.getValue()) {
            String className = emptyIfNull(line.getClassName());
            String methodName = emptyIfNull(line.getMethodName());
            String fileName = emptyIfNull(line.getFileName());

            out.println("    " + className + "." + methodName + " (" + fileName + " line "
                    + line.getLineNumber() + ")");
        }
    }
    out.flush();
    out.close();
    ErrorReportChunk stackDump = new ErrorReportChunk("thread dump", "text/plain", dumpBytes.toByteArray());
    chunks.add(stackDump);

    // SYSTEM PROPERTIES
    ByteArrayOutputStream propsBytes = new ByteArrayOutputStream();
    PrintStream propsOut = new PrintStream(propsBytes);
    for (Map.Entry<Object, Object> next : System.getProperties().entrySet()) {
        propsOut.println(" " + next.getKey() + "=" + next.getValue());
    }
    propsOut.flush();
    propsOut.close();
    chunks.add(new ErrorReportChunk("system properties", "text/plain", propsBytes.toByteArray()));

    // LOCAL CLOCK
    chunks.add(new ErrorReportChunk("local clock", "text/plain", new DateTime().toString().getBytes()));

    // NETWORKING
    StringBuffer networking = new StringBuffer();
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        networking.append("INTERFACE: " + iface.getName() + " (" + iface.getDisplayName() + ")\n");
        Enumeration<InetAddress> addresses = iface.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            networking.append("  Address:" + address.getHostAddress() + "\n");
            networking.append("      Cannonical Host Name: " + address.getCanonicalHostName() + "\n");
            networking.append("                 Host Name: " + address.getHostName() + "\n");
        }
    }
    chunks.add(new ErrorReportChunk("network configuration", "text/plain", networking.toString().getBytes()));

    // DECORATORS
    if (decorators != null) {
        for (ErrorReportDecorator decorator : decorators) {
            chunks.addAll(decorator.makeChunks(cause));
        }
    }
    ErrorReport report = new ErrorReport(chunks);
    Reporter reporter = new Reporter();
    ReportId id = reporter.submitReport(report);
}

From source file:org.shareok.data.kernel.api.services.ServiceUtil.java

public static Thread getThreadByName(String name) {
    for (Thread th : Thread.getAllStackTraces().keySet()) {
        String threadName = th.getName();
        if (null != threadName && threadName.equals(name)) {
            return th;
        }/*from  w ww  .j a v  a  2 s.  c o  m*/
    }
    return null;
}

From source file:Main.java

private static StackTraceElement[] getStackForThread(long id) {
    for (Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
        if (entry.getKey().getId() == id) {
            return entry.getValue();
        }//w  ww . ja  v  a 2  s .co m
    }
    return new StackTraceElement[0];
}