Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LOGMGR-133] Follow up PR to add the findCallingClasses() method for … #173

Merged
merged 1 commit into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/main/java9/org/jboss/logmanager/JDKSpecific.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,6 +64,15 @@ static Class<?> findCallingClass(Set<ClassLoader> rejectClassLoaders) {
}
}

static Collection<Class<?>> findCallingClasses(Set<ClassLoader> 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));
}
Expand Down Expand Up @@ -133,6 +163,18 @@ public Class<?> run() {
}
}

static final class FindCallingClassesAction implements PrivilegedAction<Collection<Class<?>>> {
private final Set<ClassLoader> rejectClassLoaders;

FindCallingClassesAction(final Set<ClassLoader> rejectClassLoaders) {
this.rejectClassLoaders = rejectClassLoaders;
}

public Collection<Class<?>> run() {
return WALKER.walk(new FindAllWalkFunction(rejectClassLoaders));
}
}

static final class FindFirstWalkFunction implements Function<Stream<StackWalker.StackFrame>, Class<?>> {
private final Set<ClassLoader> rejectClassLoaders;

Expand All @@ -152,4 +194,25 @@ public Class<?> apply(final Stream<StackWalker.StackFrame> stream) {
return null;
}
}

static final class FindAllWalkFunction implements Function<Stream<StackWalker.StackFrame>, Collection<Class<?>>> {
private final Set<ClassLoader> rejectClassLoaders;

FindAllWalkFunction(final Set<ClassLoader> rejectClassLoaders) {
this.rejectClassLoaders = rejectClassLoaders;
}

public Collection<Class<?>> apply(final Stream<StackWalker.StackFrame> stream) {
final Collection<Class<?>> results = new LinkedHashSet<>();
final Iterator<StackWalker.StackFrame> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down