001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.message; 018 019import java.lang.management.ManagementFactory; 020import java.lang.management.ThreadInfo; 021import java.lang.management.ThreadMXBean; 022import java.lang.reflect.Method; 023import java.util.HashMap; 024import java.util.Map; 025 026import org.apache.logging.log4j.message.ThreadDumpMessage; 027import org.apache.logging.log4j.message.ThreadInformation; 028 029/** 030 * Factory to create extended thread information. 031 */ 032public class ExtendedThreadInfoFactory implements ThreadDumpMessage.ThreadInfoFactory { 033 public ExtendedThreadInfoFactory() { 034 final Method[] methods = ThreadInfo.class.getMethods(); 035 boolean basic = true; 036 for (final Method method : methods) { 037 if (method.getName().equals("getLockInfo")) { 038 basic = false; 039 break; 040 } 041 } 042 if (basic) { 043 throw new IllegalStateException(); 044 } 045 } 046 @Override 047 public Map<ThreadInformation, StackTraceElement[]> createThreadInfo() { 048 final ThreadMXBean bean = ManagementFactory.getThreadMXBean(); 049 final ThreadInfo[] array = bean.dumpAllThreads(true, true); 050 051 final Map<ThreadInformation, StackTraceElement[]> threads = 052 new HashMap<>(array.length); 053 for (final ThreadInfo info : array) { 054 threads.put(new ExtendedThreadInformation(info), info.getStackTrace()); 055 } 056 return threads; 057 } 058}