5
5
import java .io .IOException ;
6
6
import java .io .InputStream ;
7
7
import java .lang .UnsatisfiedLinkError ;
8
+ import java .security .AccessController ;
9
+ import java .security .PrivilegedAction ;
8
10
9
11
public enum Native {
10
12
;
@@ -61,6 +63,24 @@ public static synchronized boolean isLoaded() {
61
63
return loaded ;
62
64
}
63
65
66
+ private static void loadLibrary (final String libName ) {
67
+ AccessController .doPrivileged (new PrivilegedAction <Void >() {
68
+ public Void run () {
69
+ System .loadLibrary (libName );
70
+ return null ;
71
+ }
72
+ });
73
+ }
74
+
75
+ private static void loadLibraryFile (final String libFileName ) {
76
+ AccessController .doPrivileged (new PrivilegedAction <Void >() {
77
+ public Void run () {
78
+ System .load (libFileName );
79
+ return null ;
80
+ }
81
+ });
82
+ }
83
+
64
84
public static synchronized void load () {
65
85
load (null );
66
86
}
@@ -74,15 +94,15 @@ public static synchronized void load(final File tempFolder) {
74
94
String overridePath = System .getProperty (nativePathOverride );
75
95
if (overridePath != null ) {
76
96
// Do not fall back to auto-discovery - consumers know better
77
- System . load (overridePath );
97
+ loadLibraryFile (overridePath );
78
98
loaded = true ;
79
99
return ;
80
100
}
81
101
82
102
// try to load the shared library directly from the JAR
83
103
try {
84
104
Class .forName ("org.osgi.framework.BundleEvent" ); // Simple OSGI env. check
85
- System . loadLibrary (libname );
105
+ loadLibrary (libname );
86
106
loaded = true ;
87
107
return ;
88
108
} catch (Throwable e ) {
@@ -94,7 +114,7 @@ public static synchronized void load(final File tempFolder) {
94
114
// fallback to loading the zstd-jni from the system library path.
95
115
// It also covers loading on Android.
96
116
try {
97
- System . loadLibrary (libnameShort );
117
+ loadLibrary (libnameShort );
98
118
loaded = true ;
99
119
return ;
100
120
} catch (UnsatisfiedLinkError e ) {
@@ -127,11 +147,11 @@ public static synchronized void load(final File tempFolder) {
127
147
// ignore
128
148
}
129
149
try {
130
- System . load (tempLib .getAbsolutePath ());
150
+ loadLibraryFile (tempLib .getAbsolutePath ());
131
151
} catch (UnsatisfiedLinkError e ) {
132
152
// fall-back to loading the zstd-jni from the system library path
133
153
try {
134
- System . loadLibrary (libnameShort );
154
+ loadLibrary (libnameShort );
135
155
} catch (UnsatisfiedLinkError e1 ) {
136
156
// display error in case problem with loading from temp folder
137
157
// and from system library path - concatenate both messages
0 commit comments