|
29 | 29 | import io.questdb.std.ex.KerberosException; |
30 | 30 | import io.questdb.std.str.Path; |
31 | 31 | import org.jetbrains.annotations.NotNull; |
| 32 | +import org.jetbrains.annotations.Nullable; |
32 | 33 |
|
33 | 34 | import java.io.File; |
34 | 35 | import java.io.FileOutputStream; |
35 | 36 | import java.io.IOException; |
36 | 37 | import java.io.InputStream; |
37 | 38 | import java.lang.management.ManagementFactory; |
| 39 | +import java.net.URL; |
38 | 40 | import java.nio.file.Paths; |
| 41 | +import java.security.CodeSource; |
39 | 42 | import java.util.concurrent.locks.LockSupport; |
40 | 43 |
|
41 | 44 | public final class Os { |
@@ -260,9 +263,52 @@ private static void loadLib(String lib) { |
260 | 263 |
|
261 | 264 | private static native int setCurrentThreadAffinity0(int cpu); |
262 | 265 |
|
263 | | - private static boolean tryLoadFromDistribution(String cxxLibName, String rustLibName) { |
264 | | - // the property name must be sync-ed with questdb.sh |
| 266 | + private static boolean isJlinkRuntime() { |
| 267 | + // Detect jlink-ed runtime by checking if CodeSource uses jrt: protocol |
| 268 | + CodeSource codeSource = Os.class.getProtectionDomain().getCodeSource(); |
| 269 | + if (codeSource == null) { |
| 270 | + return false; |
| 271 | + } |
| 272 | + URL location = codeSource.getLocation(); |
| 273 | + return location != null && "jrt".equals(location.getProtocol()); |
| 274 | + } |
| 275 | + |
| 276 | + @Nullable |
| 277 | + public static String getNativeLibsDir(String libName) { |
| 278 | + // the property name must be synced with questdb.sh and docker-entrypoint.sh |
265 | 279 | String libsDir = System.getProperty("questdb.libs.dir"); |
| 280 | + if (libsDir != null) { |
| 281 | + // hooray, we are running from a distribution and the lib dir was set explicitly! |
| 282 | + return libsDir; |
| 283 | + } |
| 284 | + |
| 285 | + // let's try to detect the lib location |
| 286 | + if (!isJlinkRuntime()) { |
| 287 | + // we are not in a jlink-ed runtime image -> we have to extract the native libs from the jar |
| 288 | + return null; |
| 289 | + } |
| 290 | + |
| 291 | + // In jlink-ed runtime images, java.home points to the runtime image root, |
| 292 | + // modules and native libs are in $JAVA_HOME/lib/ |
| 293 | + String javaHome = System.getProperty("java.home"); |
| 294 | + if (javaHome == null) { |
| 295 | + return null; |
| 296 | + } |
| 297 | + |
| 298 | + java.nio.file.Path libDir = Paths.get(javaHome, "lib"); |
| 299 | + if (!libDir.toFile().isDirectory()) { |
| 300 | + return null; |
| 301 | + } |
| 302 | + |
| 303 | + java.nio.file.Path libPath = libDir.resolve(libName); |
| 304 | + if (!libPath.toFile().exists()) { |
| 305 | + return null; |
| 306 | + } |
| 307 | + return libDir.toString(); |
| 308 | + } |
| 309 | + |
| 310 | + private static boolean tryLoadFromDistribution(String cxxLibName, String rustLibName) { |
| 311 | + String libsDir = getNativeLibsDir(cxxLibName); |
266 | 312 | if (libsDir == null) { |
267 | 313 | return false; |
268 | 314 | } |
|
0 commit comments