LOG4J2-1926 moved ExtendedThreadInfoFactory out of ThreadDumpMessage class and make it a top-level class in log4j-core
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/75f519f7 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/75f519f7 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/75f519f7 Branch: refs/heads/master Commit: 75f519f75cdd576b264c05af3d4608d6cb8d2f97 Parents: d0023de Author: rpopma <[email protected]> Authored: Sat Jun 24 19:54:29 2017 +0900 Committer: rpopma <[email protected]> Committed: Sat Jun 24 19:54:29 2017 +0900 ---------------------------------------------------------------------- .../core/message/ExtendedThreadInfoFactory.java | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/75f519f7/log4j-core/src/main/java/org/apache/logging/log4j/core/message/ExtendedThreadInfoFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/message/ExtendedThreadInfoFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/message/ExtendedThreadInfoFactory.java new file mode 100644 index 0000000..0079fbc --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/message/ExtendedThreadInfoFactory.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.message; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.apache.logging.log4j.message.ThreadDumpMessage; +import org.apache.logging.log4j.message.ThreadInformation; + +/** + * Factory to create extended thread information. + */ +public class ExtendedThreadInfoFactory implements ThreadDumpMessage.ThreadInfoFactory { + public ExtendedThreadInfoFactory() { + final Method[] methods = ThreadInfo.class.getMethods(); + boolean basic = true; + for (final Method method : methods) { + if (method.getName().equals("getLockInfo")) { + basic = false; + break; + } + } + if (basic) { + throw new IllegalStateException(); + } + } + @Override + public Map<ThreadInformation, StackTraceElement[]> createThreadInfo() { + final ThreadMXBean bean = ManagementFactory.getThreadMXBean(); + final ThreadInfo[] array = bean.dumpAllThreads(true, true); + + final Map<ThreadInformation, StackTraceElement[]> threads = + new HashMap<>(array.length); + for (final ThreadInfo info : array) { + threads.put(new ExtendedThreadInformation(info), info.getStackTrace()); + } + return threads; + } +}
