Logo AND Algorithmique Numérique Distribuée

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