Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / bindings / java / org / simgrid / NativeLib.java
index 5853aea..7bb2b10 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2014-2021. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -14,10 +14,11 @@ import java.util.stream.Stream;
 
 /** Helper class loading the native functions of SimGrid that we use for downcalls
  *
- * Almost all org.simgrid.msg.* classes contain a static bloc (thus executed when the class is loaded)
+ * Almost all org.simgrid.msg.* classes contain a static block (thus executed when the class is loaded)
  * containing a call to this.
  */
 public final class NativeLib {
+       private static final boolean WINDOWS_OS = System.getProperty("os.name").toLowerCase().startsWith("win");
        private static boolean isNativeInited = false;
        private static Path tempDir = null; // where the embeeded libraries are unpacked before loading them
 
@@ -41,7 +42,7 @@ public final class NativeLib {
                if (isNativeInited)
                        return;
 
-               if (System.getProperty("os.name").toLowerCase().startsWith("win"))
+               if (WINDOWS_OS)
                        NativeLib.nativeInit("winpthread-1");
 
                NativeLib.nativeInit("simgrid");
@@ -89,15 +90,16 @@ public final class NativeLib {
                
                // We must write the lib onto the disk before loading it -- stupid operating systems
                if (tempDir == null) {
+                       final String tempPrefix = "simgrid-java-";
 
-                       if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
+                       if (WINDOWS_OS) {
                                // The cleanup at exit fails on Windows where it is impossible to delete files which are still in
                                // use.  Try to remove stale temporary files from previous executions, and limit disk usage.
                                Path tmpdir = (new File(System.getProperty("java.io.tmpdir"))).toPath();
                                try (Stream<Path> paths = Files.find(tmpdir, 1,
                                        (Path p, java.nio.file.attribute.BasicFileAttributes a) ->
                                                a.isDirectory() && !p.equals(tmpdir) &&
-                                               p.getFileName().toString().startsWith("simgrid-java-"))) {
+                                               p.getFileName().toString().startsWith(tempPrefix))) {
                                        paths.map(Path::toFile)
                                             .map(FileCleaner::new)
                                             .forEach(FileCleaner::run);
@@ -105,7 +107,7 @@ public final class NativeLib {
                                }
                        }
 
-                       tempDir = Files.createTempDirectory("simgrid-java-");
+                       tempDir = Files.createTempDirectory(tempPrefix);
                        // don't leak the files on disk, but remove it on JVM shutdown
                        Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
                }