Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[java] unpack dll/so to a tempDir instead of messing with their name
authorMartin Quinson <martin.quinson@loria.fr>
Fri, 2 Oct 2015 20:57:56 +0000 (22:57 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Fri, 2 Oct 2015 20:57:56 +0000 (22:57 +0200)
12 files changed:
src/bindings/java/org/simgrid/NativeLib.java
src/bindings/java/org/simgrid/msg/Comm.java
src/bindings/java/org/simgrid/msg/File.java
src/bindings/java/org/simgrid/msg/Msg.java
src/bindings/java/org/simgrid/msg/Mutex.java
src/bindings/java/org/simgrid/msg/Process.java
src/bindings/java/org/simgrid/msg/RngStream.java
src/bindings/java/org/simgrid/msg/Semaphore.java
src/bindings/java/org/simgrid/msg/Task.java
src/bindings/java/org/simgrid/surf/SurfJNI.java
src/bindings/java/org/simgrid/trace/Trace.java
src/bindings/java/surf.i

index 4e61de9..2f583a9 100644 (file)
@@ -10,8 +10,46 @@ import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.File;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 public final class NativeLib {
 
 public final class NativeLib {
+       /* Statically load the library which contains all native functions used in here */
+       static private boolean isNativeInited = false;
+       public static void nativeInit() {
+               if (isNativeInited)
+                       return;
+               
+               if (System.getProperty("os.name").toLowerCase().startsWith("win"))
+                       NativeLib.nativeInit("winpthread-1");
+
+               NativeLib.nativeInit("simgrid");
+               NativeLib.nativeInit("surf-java");
+               NativeLib.nativeInit("simgrid-java");      
+               isNativeInited = true;
+       }
+
+       static {
+               nativeInit();
+       }
+
+
+       public static void nativeInit(String name) {
+               try {
+                       /* Prefer the version of the library bundled into the jar file and use it */
+                       loadLib(name);
+               } catch (SimGridLibNotFoundException e) {
+                       /* If not found, try to see if we can find a version on disk */
+                       try {
+                               System.loadLibrary(name);
+                       } catch (UnsatisfiedLinkError e2) {
+                               System.err.println("Cannot load the bindings to the "+name+" library in path "+getPath());
+                               e.printStackTrace();
+                               System.err.println("This jar file does not seem to fit your system, and I cannot find an installation of SimGrid.");
+                               System.exit(1);
+                       }
+               }
+       }
 
        public static String getPath() {
                String prefix = "NATIVE";
 
        public static String getPath() {
                String prefix = "NATIVE";
@@ -35,23 +73,7 @@ public final class NativeLib {
 
                return prefix + "/" + os + "/" + arch + "/";
        }
 
                return prefix + "/" + os + "/" + arch + "/";
        }
-       public static void nativeInit(String name) {
-               try {
-                       /* Prefer the version of the library bundled into the jar file and use it */
-                       loadLib(name);
-               } catch (SimGridLibNotFoundException e) {
-                       /* If not found, try to see if we can find a version on disk */
-                       try {
-                               System.loadLibrary(name);
-                       } catch (UnsatisfiedLinkError e2) {
-                               System.err.println("Cannot load the bindings to the "+name+" library in path "+getPath());
-                               e.printStackTrace();
-                               System.err.println("This jar file does not seem to fit your system, and I cannot find an installation of SimGrid.");
-                               System.exit(1);
-                       }
-               }
-       }
-
+       static Path tempDir = null;
        private static void loadLib (String name) throws SimGridLibNotFoundException {
                String Path = NativeLib.getPath();
 
        private static void loadLib (String name) throws SimGridLibNotFoundException {
                String Path = NativeLib.getPath();
 
@@ -79,13 +101,15 @@ public final class NativeLib {
                }
                try {
                        // We must write the lib onto the disk before loading it -- stupid operating systems
                }
                try {
                        // We must write the lib onto the disk before loading it -- stupid operating systems
-                       File fileOut = new File(filename);
-                       fileOut = File.createTempFile(name+"-", ".tmp");
-                       // don't leak the file on disk, but remove it on JVM shutdown
-                       Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(fileOut.getAbsolutePath())));
-                       OutputStream out = new FileOutputStream(fileOut);
+                       if (tempDir == null) {
+                               tempDir = Files.createTempDirectory("simgrid-java");
+                               // don't leak the files on disk, but remove it on JVM shutdown
+                               Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
+                       }
+                       File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
 
                        /* copy the library in position */  
 
                        /* copy the library in position */  
+                       OutputStream out = new FileOutputStream(fileOut);
                        byte[] buffer = new byte[4096]; 
                        int bytes_read; 
                        while ((bytes_read = in.read(buffer)) != -1)     // Read until EOF
                        byte[] buffer = new byte[4096]; 
                        int bytes_read; 
                        while ((bytes_read = in.read(buffer)) != -1)     // Read until EOF
@@ -104,15 +128,17 @@ public final class NativeLib {
 
        /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
        private static class FileCleaner implements Runnable {
 
        /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
        private static class FileCleaner implements Runnable {
-               private String target;
-               public FileCleaner(String name) {
-                       target = name;
+               private File dir;
+               public FileCleaner(File dir) {
+                       this.dir = dir;
                }
                public void run() {
                        try {
                }
                public void run() {
                        try {
-                               new File(target).delete();
+                           for (File f : dir.listFiles())
+                               f.delete();
+                               dir.delete();
                        } catch(Exception e) {
                        } catch(Exception e) {
-                               System.out.println("Unable to clean temporary file "+target+" during shutdown.");
+                               System.out.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");
                                e.printStackTrace();
                        }
                }    
                                e.printStackTrace();
                        }
                }    
index b4d2341..86c33cb 100644 (file)
@@ -80,7 +80,7 @@ public class Comm {
         */
        public static native void nativeInit();
        static {
         */
        public static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }       
 }
                nativeInit();
        }       
 }
index bd19252..015d080 100644 (file)
@@ -45,12 +45,10 @@ public class File {
         */
        public native void close();
 
         */
        public native void close();
 
-       /**
-        * Class initializer, to initialize various JNI stuff
-        */
+       /** Class initializer, to initialize various JNI stuff */
        public static native void nativeInit();
        static {
        public static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }       
 }
\ No newline at end of file
                nativeInit();
        }       
 }
\ No newline at end of file
index d46b4d8..b324db8 100644 (file)
@@ -11,23 +11,6 @@ import org.simgrid.NativeLib;
 
 
 public final class Msg {
 
 
 public final class Msg {
-       /* Statically load the library which contains all native functions used in here */
-       static private boolean isNativeInited = false;
-       public static void nativeInit() {
-               if (isNativeInited)
-                       return;
-               
-               if (System.getProperty("os.name").toLowerCase().startsWith("win"))
-                       NativeLib.nativeInit("winpthread-1");
-
-               NativeLib.nativeInit("simgrid");
-               NativeLib.nativeInit("simgrid-java");      
-               isNativeInited = true;
-       }
-
-       static {
-               nativeInit();
-       }
 
        /** Retrieve the simulation time
         * @return The simulation time.
 
        /** Retrieve the simulation time
         * @return The simulation time.
@@ -126,4 +109,9 @@ public final class Msg {
                /* Execute the simulation */
                Msg.run();
        }
                /* Execute the simulation */
                Msg.run();
        }
+       
+       /* Class initializer, to initialize various JNI stuff */
+       static {
+               org.simgrid.NativeLib.nativeInit();
+       }
 }
 }
index 503588b..ed7340b 100644 (file)
@@ -34,7 +34,7 @@ public class Mutex {
         */
        public static native void nativeInit();
        static {
         */
        public static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }       
 }
                nativeInit();
        }       
 }
index 5c55f10..5e53e74 100644 (file)
@@ -363,7 +363,7 @@ public abstract class Process implements Runnable {
         */
        private static native void nativeInit();
        static {
         */
        private static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }
        /**
                nativeInit();
        }
        /**
index 9e0ec6f..23e95cb 100644 (file)
@@ -113,7 +113,7 @@ public class RngStream {
         */
        public static native void nativeInit();
        static {
         */
        public static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }
 }
                nativeInit();
        }
 }
index 9be79f6..0172aa3 100644 (file)
@@ -80,7 +80,7 @@ public class Semaphore {
         */
        public static native void nativeInit();
        static {
         */
        public static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }
 }
\ No newline at end of file
                nativeInit();
        }
 }
\ No newline at end of file
index 46f568a..2b9481f 100644 (file)
@@ -363,7 +363,7 @@ public class Task {
         */
        public static native void nativeInit();
        static {
         */
        public static native void nativeInit();
        static {
-               Msg.nativeInit();
+               org.simgrid.NativeLib.nativeInit();
                nativeInit();
        }
 
                nativeInit();
        }
 
index 277db31..af1eff3 100644 (file)
@@ -13,10 +13,7 @@ import org.simgrid.NativeLib;
 public class SurfJNI {
 
   static {
 public class SurfJNI {
 
   static {
-    if (System.getProperty("os.name").toLowerCase().startsWith("win"))
-        NativeLib.nativeInit("winpthread-1");
-    NativeLib.nativeInit("simgrid");
-    NativeLib.nativeInit("surf-java");
+    org.simgrid.NativeLib.nativeInit();    
     Runtime.getRuntime().addShutdownHook(
       new Thread() {
         public void run() {
     Runtime.getRuntime().addShutdownHook(
       new Thread() {
         public void run() {
index 4892ac3..70cb9e0 100644 (file)
@@ -8,12 +8,12 @@
 
 package org.simgrid.trace;
 
 
 package org.simgrid.trace;
 
-import org.simgrid.msg.Msg;
+import org.simgrid.NativeLib;
 
 public final class Trace {
        /* Statically load the library which contains all native functions used in here */
        static {
 
 public final class Trace {
        /* Statically load the library which contains all native functions used in here */
        static {
-               Msg.nativeInit();
+               NativeLib.nativeInit();
        }
 
        // TODO complete the binding of the tracing API 
        }
 
        // TODO complete the binding of the tracing API 
index 2332cbc..95c1130 100644 (file)
@@ -16,10 +16,7 @@ import org.simgrid.NativeLib;
 %}
 %pragma(java) jniclasscode=%{
   static {
 %}
 %pragma(java) jniclasscode=%{
   static {
-    if (System.getProperty("os.name").toLowerCase().startsWith("win"))
-        NativeLib.nativeInit("winpthread-1");
-    NativeLib.nativeInit("simgrid");
-    NativeLib.nativeInit("surf-java");
+    org.simgrid.NativeLib.nativeInit();    
     Runtime.getRuntime().addShutdownHook(
       new Thread() {
         public void run() {
     Runtime.getRuntime().addShutdownHook(
       new Thread() {
         public void run() {