Logo AND Algorithmique Numérique Distribuée

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