Logo AND Algorithmique Numérique Distribuée

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