Logo AND Algorithmique Numérique Distribuée

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