Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
always close file descriptors, even on exceptions
[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.IOException;
12 import java.io.OutputStream;
13 import java.io.File;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16
17 public final class NativeLib {
18         /* Statically load the library which contains all native functions used in here */
19         private static boolean isNativeInited = false;
20         static Path tempDir = null;
21
22         private NativeLib() {
23                 throw new IllegalAccessError("Utility class");
24         }
25
26         public static void nativeInit() {
27                 if (isNativeInited)
28                         return;
29
30                 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
31                         NativeLib.nativeInit("winpthread-1");
32
33                 NativeLib.nativeInit("simgrid");
34                 NativeLib.nativeInit("simgrid-java");
35                 isNativeInited = true;
36         }
37
38         public static void nativeInit(String name) {
39                 try {
40                         /* Prefer the version of the library bundled into the jar file and use it */
41                         loadLib(name);
42                 } catch (LinkageException embeededException) {
43                         /* If not found, try to see if we can find a version on disk */
44                         try {
45                                 System.loadLibrary(name);
46                         } catch (UnsatisfiedLinkError systemException) {
47                                 if (! "boost_context".equals(name)) { // Ignore when we cannot load boost_context
48                                         
49                                         System.err.println("\nCannot load the bindings to the "+name+" library in path "+getPath());
50                                         Throwable cause = embeededException.getCause();
51                                         if (cause instanceof java.lang.UnsatisfiedLinkError) {
52                                                 if (cause.getMessage().matches(".*libcgraph.so.*"))
53                                                         System.err.println("HINT: Try to install the libcgraph package (sudo apt-get install libcgraph).");
54                                                 else if (cause.getMessage().matches(".*libboost_context.so.*"))
55                                                         System.err.println("HINT: Try to install the boost-context package (sudo apt-get install libboost-context-dev).");
56                                                 else
57                                                         System.err.println("Try to install the missing dependencies, which name should appear above.");
58                                         } else {
59                                                 System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk.");
60                                         }
61                                         System.err.println();
62                                         cause.printStackTrace();
63                                         System.exit(1);
64                                 }
65                         }
66                 }
67         }
68
69         public static String getPath() {
70                 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
71                 String prefix = "NATIVE";
72                 String os = System.getProperty("os.name");
73                 String arch = System.getProperty("os.arch");
74
75                 if (arch.matches("^i[3-6]86$"))
76                         arch = "x86";
77                 else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
78                         arch = "amd64";
79
80                 if (os.toLowerCase().startsWith("win")){
81                         os = "Windows";
82                 } else if (os.contains("OS X"))
83                         os = "Darwin";
84
85                 os = os.replace(' ', '_');
86                 arch = arch.replace(' ', '_');
87
88                 return prefix + "/" + os + "/" + arch + "/";
89         }
90
91         private static void loadLib (String name) throws LinkageException {
92                 String path = NativeLib.getPath();
93
94                 String filename=name;
95                 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
96                 OutputStream out = null;
97                 
98                 if (in == null) {
99                         filename = "lib"+name+".so";
100                         in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
101                 }
102                 if (in == null) {
103                         filename = name+".dll";
104                         in =  NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
105                 }
106                 if (in == null) {
107                         filename = "lib"+name+".dll";
108                         in =  NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
109                 }
110                 if (in == null) {
111                         filename = "lib"+name+".dylib";
112                         in =  NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
113                 }
114                 if (in == null) {
115                         throw new LinkageException("Cannot find library "+name+" in path "+path+". Sorry, but this jar does not seem to be usable on your machine.");
116                 }
117                 try {
118                         // We must write the lib onto the disk before loading it -- stupid operating systems
119                         if (tempDir == null) {
120                                 tempDir = Files.createTempDirectory("simgrid-java-");
121                                 // don't leak the files on disk, but remove it on JVM shutdown
122                                 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
123                         }
124                         File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
125
126                         /* copy the library in position */  
127                         out = new FileOutputStream(fileOut);
128                         byte[] buffer = new byte[4096]; 
129                         int bytesRead; 
130                         while ((bytesRead = in.read(buffer)) != -1)     // Read until EOF
131                                 out.write(buffer, 0, bytesRead); 
132
133                         /* load that shit */
134                         System.load(fileOut.getAbsolutePath());
135                 } catch (SecurityException|UnsatisfiedLinkError|IOException e) {
136                         throw new LinkageException("Cannot load the bindings to the "+name+" library in path "+getPath(), e);
137                 } finally {
138                         /* Always close all descriptors, no matter success or error */
139                         try { 
140                                 in.close();
141                         } catch (IOException e) {
142                                 /* Too bad. I dont care. */
143                         }
144                         
145                         if (out != null) {
146                                 try { 
147                                 out.close();
148                                 } catch (IOException e) {
149                                         /* Too bad too. I dont care either. */
150                                 }
151                         }
152                 }
153         }
154
155         /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
156         private static class FileCleaner implements Runnable {
157                 private File dir;
158                 public FileCleaner(File dir) {
159                         this.dir = dir;
160                 }
161                 @Override
162                 public void run() {
163                         try {
164                                 for (File f : dir.listFiles())
165                                         if (! f.delete() )
166                                                 System.err.println("Unable to clean temporary file "+f.getAbsolutePath()+" during shutdown.");
167                                 if (! dir.delete() )
168                                         System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");                                
169                         } catch(Exception e) {
170                                 System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown: "+e.getCause());
171                                 e.printStackTrace();
172                         }
173                 }
174         }
175
176
177         public static void main(String[] args) {
178                 System.out.println("This jarfile searches the native code under: " +getPath());
179         }
180 }
181
182 class LinkageException extends Exception {
183         private static final long serialVersionUID = 1L;
184         public LinkageException(String msg) {
185                 super(msg);
186         }
187
188         public LinkageException(String msg, Throwable e) {
189                 super(msg,e);
190         }
191 }