Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d2538473982caee39a8313178d77329d3d8217d7
[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
14 public final class NativeLib {
15
16         public static String getPath() {
17                 String prefix = "NATIVE";
18                 String os = System.getProperty("os.name");
19                 String arch = System.getProperty("os.arch");
20
21                 if (arch.matches("^i[3-6]86$"))
22                         arch = "x86";
23                 else if (arch.equalsIgnoreCase("amd64"))
24                         arch = "x86_64";
25
26                 if (os.toLowerCase().startsWith("win")){
27                         os = "Windows";
28                         arch = "x86";
29                 }else if (os.contains("OS X"))
30                         os = "Darwin";
31
32                 os = os.replace(' ', '_');
33                 arch = arch.replace(' ', '_');
34
35                 return prefix + "/" + os + "/" + arch + "/";
36         }
37         public static void nativeInit(String name) {
38                 try {
39                         /* prefer the version on disk, if existing */
40                         System.loadLibrary(name);
41                 } catch (UnsatisfiedLinkError e) {
42                         /* If not found, unpack the one bundled into the jar file and use it */
43                         loadLib(name);
44                 }
45         }
46
47         private static void loadLib (String name) {
48                 String Path = NativeLib.getPath();
49
50                 String filename=name;
51                 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
52
53                 if (in == null) {
54                         filename = "lib"+name+".so";
55                         in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
56                 } 
57                 if (in == null) {
58                         filename = name+".dll";
59                         in =  NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
60                 }  
61                 if (in == null) {
62                         filename = "lib"+name+".dll";
63                         in =  NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
64                 }  
65                 if (in == null) {
66                         filename = "lib"+name+".dylib";
67                         in =  NativeLib.class.getClassLoader().getResourceAsStream(Path+filename);
68                 }  
69                 if (in == null) {
70                         throw new RuntimeException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
71                 }
72                 try {
73                         // We must write the lib onto the disk before loading it -- stupid operating systems
74                         File fileOut = new File(filename);
75                         fileOut = File.createTempFile(name+"-", ".tmp");
76                         // don't leak the file on disk, but remove it on JVM shutdown
77                         Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(fileOut.getAbsolutePath())));
78                         OutputStream out = new FileOutputStream(fileOut);
79
80                         /* copy the library in position */  
81                         byte[] buffer = new byte[4096]; 
82                         int bytes_read; 
83                         while ((bytes_read = in.read(buffer)) != -1)     // Read until EOF
84                                 out.write(buffer, 0, bytes_read); 
85
86                         /* close all file descriptors, and load that shit */
87                         in.close();
88                         out.close();
89                         System.load(fileOut.getAbsolutePath());
90
91                 } catch (Exception e) {
92                         System.err.println("Cannot load the bindings to the "+name+" library in path "+getPath());
93                         e.printStackTrace();
94                         System.err.println("This jar file does not seem to fit your system, sorry");
95                         System.exit(1);
96                 }
97         }
98
99         /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
100         private static class FileCleaner implements Runnable {
101                 private String target;
102                 public FileCleaner(String name) {
103                         target = name;
104                 }
105                 public void run() {
106                         try {
107                                 new File(target).delete();
108                         } catch(Exception e) {
109                                 System.out.println("Unable to clean temporary file "+target+" during shutdown.");
110                                 e.printStackTrace();
111                         }
112                 }    
113         }
114
115
116         public static void main(String[] args) {
117                 if (args.length >= 1 && args[0].equals("--quiet"))
118                         /* be careful, this execution path is used in buildtools/Cmake/Scripts/java_bundle.sh to determine where to put the libs */
119                         System.out.println(getPath());
120                 else 
121                         System.out.println("This java library will try to load the native code under the following name:\n" +getPath());
122         }
123 }