Skip to content

Commit

Permalink
Revert the library loader
Browse files Browse the repository at this point in the history
The library is currently not being loaded during the tests.
Trying to debug why.

Change-Id: Idd9e4f2b3d2d5c1986159d540657e0afea1f98e6
  • Loading branch information
irbull committed Dec 15, 2017
1 parent ad547b4 commit cd57118
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 212 deletions.
152 changes: 89 additions & 63 deletions src/main/java/com/eclipsesource/v8/LibraryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*
* Contributors:
* EclipseSource - initial API and implementation
* Wolfgang Steiner - code separation PlatformDetector/LibraryLoader
******************************************************************************/
package com.eclipsesource.v8;

Expand All @@ -28,86 +27,53 @@ class LibraryLoader {
SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$
}

/**
* Returns the base-name for the native J2V8 library file.
* @param withLinuxVendor include/exclude the {vendor} part from the returned filename
* <p>NOTE: Vendors are only included for linux systems</p>
* @return The filename string has the following structure:
* <pre><code>{arch}-[vendor]-{operating_system}</pre></code>
*/
public static String computeLibraryShortName(boolean withLinuxVendor) {
String prefix = "j2v8";
String vendor = withLinuxVendor && PlatformDetector.OS.isLinux() ? PlatformDetector.Vendor.getName() : null;
String os = PlatformDetector.OS.getName();
String arch = PlatformDetector.Arch.getName();

final String separator = "-";

return
prefix +
(vendor != null ? separator + vendor : "") +
separator + os +
separator + arch;
private static String computeLibraryShortName() {
String base = "j2v8";
String osSuffix = getOS();
String archSuffix = getArchSuffix();
return base + "_" + osSuffix + "_" + archSuffix;
}

public static String computeLibraryFullName(boolean withLinuxVendor) {
return "lib" + computeLibraryShortName(withLinuxVendor) + "." + PlatformDetector.OS.getLibFileExtension();
private static String computeLibraryFullName() {
return "lib" + computeLibraryShortName() + "." + getOSFileExtension();
}

static boolean tryLoad(boolean withLinuxVendor, StringBuffer message) {
String libShortName = computeLibraryShortName(withLinuxVendor);
String libFullName = computeLibraryFullName(withLinuxVendor);
String ideLocation = System.getProperty("user.dir") + SEPARATOR + "jni" + SEPARATOR + libFullName;
static void loadLibrary(final String tempDirectory) {
if ( isAndroid() ) {
System.loadLibrary("j2v8");
return;
}
StringBuffer message = new StringBuffer();
String libShortName = computeLibraryShortName();
String libFullName = computeLibraryFullName();
String ideLocation = System.getProperty("user.dir") + SEPARATOR + "jni" + SEPARATOR + computeLibraryFullName();

String path = null;

/* Try loading library from java library path */
if (load(libFullName, message)) {
return true;
return;
}
if (load(libShortName, message)) {
return true;
return;
}

/* Try loading library from the IDE location */
if (new File(ideLocation).exists()) {
if (load(ideLocation, message)) {
return true;
return;
}
}

return false;
}

static void loadLibrary(final String tempDirectory) {
if (PlatformDetector.OS.isAndroid()) {
System.loadLibrary("j2v8");
return;
}

StringBuffer message = new StringBuffer();

// try loading a vendor-specific library first
if (tryLoad(true, message))
return;

// if there is no vendor-specific library, just try to load the default OS library
if (tryLoad(false, message))
return;

String path = null;

if (tempDirectory != null) {
path = tempDirectory;
} else {
path = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
}

// try extracting a vendor-specific library first
if (extract(path, true, message))
return;

// if there is no vendor-specific library, just try to extract the default OS library
if (extract(path, false, message))
if (extract(path + SEPARATOR + libFullName, libFullName, message)) {
return;
}

/* Failed to find the library */
throw new UnsatisfiedLinkError("Could not load J2V8 library. Reasons: " + message.toString()); //$NON-NLS-1$
Expand All @@ -132,11 +98,6 @@ static boolean load(final String libName, final StringBuffer message) {
return false;
}

static boolean extract(String libPath, boolean withLinuxVendor, StringBuffer message) {
String libFullName = computeLibraryFullName(withLinuxVendor);
return extract(libPath + SEPARATOR + libFullName, libFullName, message);
}

static boolean extract(final String fileName, final String mappedName, final StringBuffer message) {
FileOutputStream os = null;
InputStream is = null;
Expand Down Expand Up @@ -183,12 +144,77 @@ static boolean extract(final String fileName, final String mappedName, final Str
}

static void chmod(final String permision, final String path) {
if (PlatformDetector.OS.isWindows()) {
if (isWindows()) {
return;
}
try {
Runtime.getRuntime().exec(new String[] { "chmod", permision, path }).waitFor(); //$NON-NLS-1$
} catch (Throwable e) {
}
}

static String getOsName() {
return System.getProperty("os.name") + System.getProperty("java.specification.vendor");
}

static boolean isWindows() {
return getOsName().startsWith("Windows");
}

static boolean isMac() {
return getOsName().startsWith("Mac");
}

static boolean isLinux() {
return getOsName().startsWith("Linux");
}

static boolean isNativeClient() {
return getOsName().startsWith("nacl");
}

static boolean isAndroid() {
return getOsName().contains("Android");
}

static String getArchSuffix() {
String arch = System.getProperty("os.arch");
if (arch.equals("i686")) {
return "x86";
} else if (arch.equals("amd64")) {
return "x86_64";
} else if (arch.equals("nacl")) {
return "armv7l";
} else if (arch.equals("aarch64")) {
return "armv7l";
}
return arch;
}

static String getOSFileExtension() {
if (isWindows()) {
return "dll";
} else if (isMac()) {
return "dylib";
} else if (isLinux()) {
return "so";
} else if (isNativeClient()) {
return "so";
}
throw new UnsatisfiedLinkError("Unsupported platform: " + getOsName());
}

static String getOS() {
if (isWindows()) {
return "win32";
} else if (isMac()) {
return "macosx";
} else if (isLinux() && !isAndroid()) {
return "linux";
} else if (isAndroid()) {
return "android";
}
throw new UnsatisfiedLinkError("Unsupported platform: " + getOsName());
}

}
23 changes: 3 additions & 20 deletions src/main/java/com/eclipsesource/v8/V8.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,12 @@ private void notifyReferenceDisposed(final V8Value object) {

private static void checkNativeLibraryLoaded() {
if (!nativeLibraryLoaded) {
String vendorName = LibraryLoader.computeLibraryShortName(true);
String baseName = LibraryLoader.computeLibraryShortName(false);
String message = "J2V8 native library not loaded (" + baseName + "/" + vendorName + ")";

if (nativeLoadError != null) {
throw new IllegalStateException(message, nativeLoadError);
throw new IllegalStateException("J2V8 native library not loaded", nativeLoadError);
} else if (nativeLoadException != null) {
throw new IllegalStateException(message, nativeLoadException);
throw new IllegalStateException("J2V8 native library not loaded", nativeLoadException);
} else {
throw new IllegalStateException(message);
throw new IllegalStateException("J2V8 native library not loaded");
}
}
}
Expand Down Expand Up @@ -1567,19 +1563,6 @@ protected void releaseMethodDescriptor(final long v8RuntimePtr, final long metho

private native static boolean _isRunning(final long v8RuntimePtr);

private native static boolean _isNodeCompatible();

public static boolean isNodeCompatible() {
if (!nativeLibraryLoaded) {
synchronized (lock) {
if (!nativeLibraryLoaded) {
load(null);
}
}
}
return _isNodeCompatible();
}

void addObjRef(final V8Value reference) {
objectReferences++;
if (!referenceHandlers.isEmpty()) {
Expand Down
Loading

0 comments on commit cd57118

Please sign in to comment.