Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[win] I fail to disable winpthread + I think it's harmless, so re-enable it
[simgrid.git] / src / bindings / java / org / simgrid / NativeLib.java
1 /* Copyright (c) 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package org.simgrid;
8
9 import java.io.FileOutputStream;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.io.File;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15
16 public final class NativeLib {
17         /* Statically load the library which contains all native functions used in here */
18         static private boolean isNativeInited = false;
19         public static void nativeInit() {
20                 if (isNativeInited)
21                         return;
22                 
23                 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
24                         NativeLib.nativeInit("winpthread-1");
25
26                 NativeLib.nativeInit("simgrid");
27                 NativeLib.nativeInit("surf-java");
28                 NativeLib.nativeInit("simgrid-java");      
29                 isNativeInited = true;
30         }
31
32         public static void nativeInit(String name) {
33                 try {
34                         /* Prefer the version of the library bundled into the jar file and use it */
35                         loadLib(name);
36                 } catch (SimGridLibNotFoundException e) {
37                         /* If not found, try to see if we can find a version on disk */
38                         try {
39                                 System.loadLibrary(name);
40                         } catch (UnsatisfiedLinkError e2) {
41                                 System.err.println("Cannot load the bindings to the "+name+" library in path "+getPath());
42                                 e.printStackTrace();
43                                 System.err.println("This jar file does not seem to fit your system, and I cannot find an installation of SimGrid.");
44                                 System.exit(1);
45                         }
46                 }
47         }
48
49         public static String getPath() {
50                 String prefix = "NATIVE";
51                 String os = System.getProperty("os.name");
52                 String arch = System.getProperty("os.arch");
53
54                 if (arch.matches("^i[3-6]86$"))
55                         arch = "x86";
56                 else if (arch.equalsIgnoreCase("x86_64"))
57                         arch = "amd64";
58                 else if (arch.equalsIgnoreCase("AMD64"))
59                         arch = "amd64";
60
61                 if (os.toLowerCase().startsWith("win")){
62                         os = "Windows";
63                 } else if (os.contains("OS X"))
64                         os = "Darwin";
65
66                 os = os.replace(' ', '_');
67                 arch = arch.replace(' ', '_');
68
69                 return prefix + "/" + os + "/" + arch + "/";
70         }
71         static Path tempDir = null;
72         private static void loadLib (String name) throws SimGridLibNotFoundException {
73                 String Path = NativeLib.getPath();
74
75                 String filename=name;
76                 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
77
78                 if (in == null) {
79                         filename = "lib"+name+".so";
80                         in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
81                 }
82                 if (in == null) {
83                         filename = name+".dll";
84                         in =  NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
85                 }
86                 if (in == null) {
87                         filename = "lib"+name+".dll";
88                         in =  NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
89                 }
90                 if (in == null) {
91                         filename = "lib"+name+".dylib";
92                         in =  NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
93                 }
94                 if (in == null) {
95                         throw new SimGridLibNotFoundException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
96                 }
97                 try {
98                         // We must write the lib onto the disk before loading it -- stupid operating systems
99                         if (tempDir == null) {
100                                 tempDir = Files.createTempDirectory("simgrid-java");
101                                 // don't leak the files on disk, but remove it on JVM shutdown
102                                 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
103                         }
104                         File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
105
106                         /* copy the library in position */  
107                         OutputStream out = new FileOutputStream(fileOut);
108                         byte[] buffer = new byte[4096]; 
109                         int bytes_read; 
110                         while ((bytes_read = in.read(buffer)) != -1)     // Read until EOF
111                                 out.write(buffer, 0, bytes_read); 
112
113                         /* close all file descriptors, and load that shit */
114                         in.close();
115                         out.close();
116                         System.load(fileOut.getAbsolutePath());
117                 } catch (Exception e) {
118                         System.err.println("Error while extracting the native library from the jar: ");
119                         e.printStackTrace();
120                         throw new SimGridLibNotFoundException("Cannot load the bindings to the "+name+" library in path "+getPath(),   e);
121                 }
122         }
123
124         /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
125         private static class FileCleaner implements Runnable {
126                 private File dir;
127                 public FileCleaner(File dir) {
128                         this.dir = dir;
129                 }
130                 public void run() {
131                         try {
132                             for (File f : dir.listFiles())
133                                 f.delete();
134                                 dir.delete();
135                         } catch(Exception e) {
136                                 System.out.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");
137                                 e.printStackTrace();
138                         }
139                 }    
140         }
141
142
143         public static void main(String[] args) {
144                 System.out.println("This jarfile searches the native code under: " +getPath());
145         }
146 }
147
148 class SimGridLibNotFoundException extends Exception {
149         private static final long serialVersionUID = 1L;
150         public SimGridLibNotFoundException(String msg) {
151                 super(msg);
152         }
153
154         public SimGridLibNotFoundException(String msg, Exception e) {
155                 super(msg,e);
156         }
157 }