Skip to content

Commit e68e4ad

Browse files
authored
RANGER-5174: fix for script engine instantiation failure (apache#546)
1 parent e93c017 commit e68e4ad

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

agents-common/src/main/java/org/apache/ranger/plugin/util/GraalScriptEngineCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ScriptEngine getScriptEngine(ClassLoader clsLoader) {
7272
ScriptEngine ret = null;
7373

7474
if (clsLoader == null) {
75-
clsLoader = Thread.currentThread().getContextClassLoader();
75+
clsLoader = getDefaultClassLoader();
7676
}
7777

7878
try {

agents-common/src/main/java/org/apache/ranger/plugin/util/JavaScriptEngineCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ScriptEngine getScriptEngine(ClassLoader clsLoader) {
3333
ScriptEngine ret = null;
3434

3535
if (clsLoader == null) {
36-
clsLoader = Thread.currentThread().getContextClassLoader();
36+
clsLoader = getDefaultClassLoader();
3737
}
3838

3939
try {

agents-common/src/main/java/org/apache/ranger/plugin/util/NashornScriptEngineCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ScriptEngine getScriptEngine(ClassLoader clsLoader) {
3737
ScriptEngine ret = null;
3838

3939
if (clsLoader == null) {
40-
clsLoader = Thread.currentThread().getContextClassLoader();
40+
clsLoader = getDefaultClassLoader();
4141
}
4242

4343
try {

agents-common/src/main/java/org/apache/ranger/plugin/util/ScriptEngineCreator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@
1919

2020
package org.apache.ranger.plugin.util;
2121

22+
import org.apache.ranger.plugin.classloader.RangerPluginClassLoader;
23+
2224
import javax.script.ScriptEngine;
2325

2426
public interface ScriptEngineCreator {
2527
ScriptEngine getScriptEngine(ClassLoader clsLoader);
28+
29+
default ClassLoader getDefaultClassLoader() {
30+
ClassLoader ret = Thread.currentThread().getContextClassLoader();
31+
32+
// Most Ranger plugins use a shim layer and RangerPluginClassLoader for isolation of libraries
33+
// loaded by the plugin implementation. The shim ensures that all calls to the plugin would have
34+
// RangerPluginClassLoader as current thread classloader.
35+
// Some plugins, like Trino, use their own isolation mechanism. In these plugins, current thread's
36+
// classloader may not load libraries in the plugin directory, which can result in failure in
37+
// creation of the script engine. Using the classloader of current class to resolve this issue.
38+
return ret instanceof RangerPluginClassLoader ? ret : this.getClass().getClassLoader();
39+
}
2640
}

0 commit comments

Comments
 (0)