001/* 002 * (C) Copyright 2012-2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 */ 017package org.nuxeo.connect.tools.report.viewer; 018 019import java.io.IOException; 020import java.lang.management.ThreadInfo; 021import java.util.Iterator; 022 023import javax.management.openmbean.CompositeData; 024import javax.management.openmbean.OpenDataException; 025 026import org.jolokia.converter.Converters; 027import org.jolokia.converter.object.StringToOpenTypeConverter; 028import org.nuxeo.runtime.management.jvm.ThreadDeadlocksDetector.JVM16Printer; 029 030import com.fasterxml.jackson.databind.JsonNode; 031import com.fasterxml.jackson.databind.node.ArrayNode; 032 033import sun.management.MappedMXBeanType; 034 035/** 036 * Prints thread dumps included in the report. 037 * 038 * @since 8.4 039 */ 040public class ThreadDumpPrinter { 041 042 final ArrayNode dump; 043 044 ThreadDumpPrinter(ArrayNode dump) { 045 this.dump = dump; 046 } 047 048 public Iterable<ThreadInfo> iterableOf() { 049 return new Iterable<ThreadInfo>() { 050 051 @Override 052 public Iterator<ThreadInfo> iterator() { 053 try { 054 return iteratorOf(); 055 } catch (IOException cause) { 056 throw new AssertionError("Cannot parse thread dump", cause); 057 } 058 } 059 060 }; 061 } 062 063 final StringToOpenTypeConverter converter = new Converters().getToOpenTypeConverter(); 064 065 public Iterator<ThreadInfo> iteratorOf() throws IOException { 066 067 return new Iterator<ThreadInfo>() { 068 069 Iterator<JsonNode> nodes = dump.iterator(); 070 071 @Override 072 public boolean hasNext() { 073 return nodes.hasNext(); 074 } 075 076 @Override 077 public ThreadInfo next() { 078 try { 079 return ThreadInfo.from( 080 (CompositeData) converter.convertToObject(MappedMXBeanType.toOpenType(ThreadInfo.class), nodes.next().toString())); 081 } catch (OpenDataException cause) { 082 throw new AssertionError("Cannot parse thread info attributes", cause); 083 } 084 } 085 086 }; 087 } 088 089 public StringBuilder print(StringBuilder sb) { 090 JVM16Printer printer = new JVM16Printer(); 091 for (ThreadInfo ti : iterableOf()) { 092 printer.print(sb, ti); 093 } 094 return sb; 095 } 096 097}