From aa76f8078984bc34e22e9b2399ed6d923161f092 Mon Sep 17 00:00:00 2001 From: James Perkins Date: Mon, 5 Mar 2018 15:14:37 -0800 Subject: [PATCH] [LOGMGR-133] Follow up PR to add the findCallingClasses() method for JDK 9 specific implementation. Also added a copyright and removed an unused import. --- .../org/jboss/logmanager/JDKSpecific.java | 63 +++++++++++++++++++ .../formatters/StackTraceFormatter.java | 1 - 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/java9/org/jboss/logmanager/JDKSpecific.java b/src/main/java9/org/jboss/logmanager/JDKSpecific.java index 843f09ca..f3ce6f36 100644 --- a/src/main/java9/org/jboss/logmanager/JDKSpecific.java +++ b/src/main/java9/org/jboss/logmanager/JDKSpecific.java @@ -1,11 +1,32 @@ +/* + * JBoss, Home of Professional Open Source. + * + * Copyright 2018 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * Licensed 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.jboss.logmanager; import static java.security.AccessController.doPrivileged; import java.lang.module.ModuleDescriptor; import java.security.PrivilegedAction; +import java.util.Collection; import java.util.EnumSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Optional; import java.util.Set; import java.util.function.Function; @@ -43,6 +64,15 @@ static Class findCallingClass(Set rejectClassLoaders) { } } + static Collection> findCallingClasses(Set rejectClassLoaders) { + final SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + return doPrivileged(new FindCallingClassesAction(rejectClassLoaders)); + } else { + return WALKER.walk(new FindAllWalkFunction(rejectClassLoaders)); + } + } + static void calculateCaller(ExtLogRecord logRecord) { WALKER.walk(new CallerCalcFunction(logRecord)); } @@ -133,6 +163,18 @@ public Class run() { } } + static final class FindCallingClassesAction implements PrivilegedAction>> { + private final Set rejectClassLoaders; + + FindCallingClassesAction(final Set rejectClassLoaders) { + this.rejectClassLoaders = rejectClassLoaders; + } + + public Collection> run() { + return WALKER.walk(new FindAllWalkFunction(rejectClassLoaders)); + } + } + static final class FindFirstWalkFunction implements Function, Class> { private final Set rejectClassLoaders; @@ -152,4 +194,25 @@ public Class apply(final Stream stream) { return null; } } + + static final class FindAllWalkFunction implements Function, Collection>> { + private final Set rejectClassLoaders; + + FindAllWalkFunction(final Set rejectClassLoaders) { + this.rejectClassLoaders = rejectClassLoaders; + } + + public Collection> apply(final Stream stream) { + final Collection> results = new LinkedHashSet<>(); + final Iterator iterator = stream.iterator(); + while (iterator.hasNext()) { + final Class clazz = iterator.next().getDeclaringClass(); + final ClassLoader classLoader = clazz.getClassLoader(); + if (classLoader != null && ! rejectClassLoaders.contains(classLoader)) { + results.add(clazz); + } + } + return results; + } + } } diff --git a/src/main/java9/org/jboss/logmanager/formatters/StackTraceFormatter.java b/src/main/java9/org/jboss/logmanager/formatters/StackTraceFormatter.java index 69f615c4..78d63c63 100644 --- a/src/main/java9/org/jboss/logmanager/formatters/StackTraceFormatter.java +++ b/src/main/java9/org/jboss/logmanager/formatters/StackTraceFormatter.java @@ -18,7 +18,6 @@ package org.jboss.logmanager.formatters; -import java.net.URL; import java.util.Collections; import java.util.IdentityHashMap; import java.util.Set;