Logo AND Algorithmique Numérique Distribuée

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