Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 78 additions & 36 deletions libraries/BridJ/src/main/java/org/bridj/Platform.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@

import org.bridj.util.ProcessUtils;
import java.util.Set;
import java.util.HashSet;
import java.util.regex.Pattern;
import java.io.*;
import java.net.URL;

import java.util.List;
import java.util.Collections;
import java.util.Collection;
import java.util.ArrayList;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -661,47 +658,92 @@ public void run() {
}
static final long DELETE_OLD_BINARIES_AFTER_MILLIS = 24 * 60 * 60 * 1000; // 24 hours

static File extractEmbeddedLibraryResource(String name) throws IOException {
String firstLibraryResource = null;
/**
* Extracts a single embedded file from the a JAR, to the {@link #extractedLibrariesTempDir} directory.
* <p>
* If the embedded library resource cannot be located, null is returned.
* </p>
*
* @param libraryResource Path of the file in the jar to extract.
* @return If successful, the extracted File reference, otherwise null.
*/
static File extractEmbeddedResource(String libraryResource) throws IOException {
InputStream in = getResourceAsStream(libraryResource);
if (in == null) {
File f = new File(libraryResource);
if (!f.exists()) {
f = new File(f.getName());
}
if (f.exists()) {
return f.getCanonicalFile();
} else {
return null;
}
}
int len;
byte[] b = new byte[8196];
String fileName = new File(libraryResource).getName();
File libFile = new File(extractedLibrariesTempDir, fileName);
OutputStream out = new BufferedOutputStream(new FileOutputStream(libFile));
while ((len = in.read(b)) > 0) {
out.write(b, 0, len);
}
out.close();
in.close();

addTemporaryExtractedLibraryFileToDeleteOnExit(libFile);
addTemporaryExtractedLibraryFileToDeleteOnExit(libFile.getParentFile());

List<String> libraryResources = getEmbeddedLibraryResource(name);
if (BridJ.veryVerbose) {
BridJ.info("Library resources for " + name + ": " + libraryResources);
return libFile;
}

/**
* Extracts a single library from the classpath JARs to {@link #extractedLibrariesTempDir}.
* <p>
* Tries to extract the library based on the possible paths generated by {@link #getEmbeddedLibraryResource}.
* Additionally, if available, accompanying debug symbol files are extracted into the same folder.
* </p>
* <p>
* If no matching library was found and extracted, null is returned.
* </p>
*
* @param name Name of the library to extract (without extension).
* @return If found, a File reference to the extracted library, otherwise null.
*/
static File extractEmbeddedLibraryResource(String name) throws IOException {
List<String> libraryResources = getEmbeddedLibraryResource(name);
if (BridJ.veryVerbose) {
BridJ.info("Library resources for " + name + ": " + libraryResources);
}
for (String libraryResource : libraryResources) {
if (firstLibraryResource == null) {
firstLibraryResource = libraryResource;
}
int i = libraryResource.lastIndexOf('.');
int len;
byte[] b = new byte[8196];
InputStream in = getResourceAsStream(libraryResource);
if (in == null) {
File f = new File(libraryResource);
if (!f.exists()) {
f = new File(f.getName());
}
if (f.exists()) {
return f.getCanonicalFile();
// Try to extract the library resource
File libFile = extractEmbeddedResource(libraryResource);
// If successful, try to extract accompanying debug symbol files
if (libFile != null) {
// The library file without its shared library extension
String basePath = libraryResource.substring(0, libraryResource.lastIndexOf('.'));

if ( isWindows() ) {
// MSVC debug symbol file, eg: foo.pdb
extractEmbeddedResource(basePath + ".pdb");
} else if ( isMacOSX() ) {
// Apple debug symbol file, eg: foo.dylib.dSYM
extractEmbeddedResource(libraryResource + ".dSYM");
// Apple debug symbol file, eg: foo.dSYM
extractEmbeddedResource(basePath + ".dSYM");
} else {
// GCC debug symbol file, eg: foo.so.debug
extractEmbeddedResource(libraryResource + ".debug");
// GCC debug symbol file, eg: foo.debug
extractEmbeddedResource(basePath + ".debug");
}
continue;
}
String fileName = new File(libraryResource).getName();
File libFile = new File(extractedLibrariesTempDir, fileName);
OutputStream out = new BufferedOutputStream(new FileOutputStream(libFile));
while ((len = in.read(b)) > 0) {
out.write(b, 0, len);
}
out.close();
in.close();

addTemporaryExtractedLibraryFileToDeleteOnExit(libFile);
addTemporaryExtractedLibraryFileToDeleteOnExit(libFile.getParentFile());

return libFile;
return libFile;
}
}
return null;
}

static final int maxTempFileAttempts = 20;

static File createTempDir(String prefix) throws IOException {
Expand Down