-
- private static void loadLib (String name) throws LinkageException {
- String path = NativeLib.getPath();
-
- String filename=name;
- InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
- OutputStream out = null;
-
- if (in == null) {
- filename = "lib"+name+".so";
- in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
- }
- if (in == null) {
- filename = name+".dll";
- in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
- }
- if (in == null) {
- filename = "lib"+name+".dll";
- in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
- }
- if (in == null) {
- filename = "lib"+name+".dylib";
- in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
- }
- if (in == null) {
- throw new LinkageException("Cannot find library "+name+" in path "+path+". Sorry, but this jar does not seem to be usable on your machine.");
- }
- try {
- // We must write the lib onto the disk before loading it -- stupid operating systems
- if (tempDir == null) {
- tempDir = Files.createTempDirectory("simgrid-java-");
- // don't leak the files on disk, but remove it on JVM shutdown
- Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
- }
- File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
-
- /* copy the library in position */
- out = new FileOutputStream(fileOut);
- byte[] buffer = new byte[4096];
- int bytesRead;
- while ((bytesRead = in.read(buffer)) != -1) // Read until EOF
- out.write(buffer, 0, bytesRead);
-
- /* load that shit */
- System.load(fileOut.getAbsolutePath());
- } catch (SecurityException|UnsatisfiedLinkError|IOException e) {
- throw new LinkageException("Cannot load the bindings to the "+name+" library in path "+getPath(), e);
- } finally {
- /* Always close all descriptors, no matter success or error */
- try {
- in.close();
- } catch (IOException e) {
- /* Too bad. I dont care. */
- }
-
- if (out != null) {
- try {
- out.close();
- } catch (IOException e) {
- /* Too bad too. I dont care either. */
- }
- }
- }
- }
-
- /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */