mirror of
https://github.com/apache/nifi.git
synced 2025-02-09 11:35:05 +00:00
NIFI-12072 moved to StackWalker API elimnating usage of SecurityManager (#7742)
This commit is contained in:
parent
cb03d6de74
commit
cbf5bb98f9
@ -54,15 +54,16 @@ import java.util.Collections;
|
|||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* THREAD SAFE
|
* THREAD SAFE
|
||||||
*/
|
*/
|
||||||
public class NarThreadContextClassLoader extends URLClassLoader {
|
public class NarThreadContextClassLoader extends URLClassLoader {
|
||||||
|
|
||||||
static final ContextSecurityManager contextSecurityManager = new ContextSecurityManager();
|
|
||||||
private final ClassLoader forward = ClassLoader.getSystemClassLoader();
|
private final ClassLoader forward = ClassLoader.getSystemClassLoader();
|
||||||
private static final List<Class<?>> narSpecificClasses = new ArrayList<>();
|
private static final List<Class<?>> narSpecificClasses = new ArrayList<>();
|
||||||
|
|
||||||
@ -135,13 +136,6 @@ public class NarThreadContextClassLoader extends URLClassLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ClassLoader lookupClassLoader() {
|
private ClassLoader lookupClassLoader() {
|
||||||
final Class<?>[] classStack = contextSecurityManager.getExecutionStack();
|
|
||||||
|
|
||||||
for (Class<?> currentClass : classStack) {
|
|
||||||
final Class<?> narClass = findNarClass(currentClass);
|
|
||||||
if (narClass != null) {
|
|
||||||
final ClassLoader desiredClassLoader = narClass.getClassLoader();
|
|
||||||
|
|
||||||
// When new Threads are created, the new Thread inherits the ClassLoaderContext of
|
// When new Threads are created, the new Thread inherits the ClassLoaderContext of
|
||||||
// the caller. However, the call stack of that new Thread may not trace back to any NiFi-specific
|
// the caller. However, the call stack of that new Thread may not trace back to any NiFi-specific
|
||||||
// code. Therefore, the NarThreadContextClassLoader will be unable to find the appropriate NAR
|
// code. Therefore, the NarThreadContextClassLoader will be unable to find the appropriate NAR
|
||||||
@ -151,13 +145,18 @@ public class NarThreadContextClassLoader extends URLClassLoader {
|
|||||||
// the ContextClassLoader back to the NarThreadContextClassLoader as appropriate via the
|
// the ContextClassLoader back to the NarThreadContextClassLoader as appropriate via the
|
||||||
// {@link FlowEngine.beforeExecute(Thread, Runnable)} and
|
// {@link FlowEngine.beforeExecute(Thread, Runnable)} and
|
||||||
// {@link FlowEngine.afterExecute(Thread, Runnable)} methods.
|
// {@link FlowEngine.afterExecute(Thread, Runnable)} methods.
|
||||||
if (desiredClassLoader instanceof NarClassLoader) {
|
|
||||||
Thread.currentThread().setContextClassLoader(desiredClassLoader);
|
final StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
|
||||||
}
|
final Optional<ClassLoader> callerClassLoader = walker.walk(s ->
|
||||||
return desiredClassLoader;
|
s.map(StackWalker.StackFrame::getDeclaringClass)
|
||||||
}
|
.map(this::findNarClass)
|
||||||
}
|
.filter(Objects::nonNull)
|
||||||
return forward;
|
.map(Class::getClassLoader)
|
||||||
|
.map(cl->cl instanceof NarClassLoader ? cl : null)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.findFirst());
|
||||||
|
callerClassLoader.ifPresent(Thread.currentThread()::setContextClassLoader);
|
||||||
|
return callerClassLoader.orElse(forward);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Class<?> findNarClass(final Class<?> cls) {
|
private Class<?> findNarClass(final Class<?> cls) {
|
||||||
@ -181,14 +180,6 @@ public class NarThreadContextClassLoader extends URLClassLoader {
|
|||||||
return SingletonHolder.instance;
|
return SingletonHolder.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ContextSecurityManager extends SecurityManager {
|
|
||||||
|
|
||||||
Class<?>[] getExecutionStack() {
|
|
||||||
return getClassContext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an instance of the given type using either default no args
|
* Constructs an instance of the given type using either default no args
|
||||||
* constructor or a constructor which takes a NiFiProperties object
|
* constructor or a constructor which takes a NiFiProperties object
|
||||||
|
Loading…
x
Reference in New Issue
Block a user