Logo AND Algorithmique Numérique Distribuée

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