Skip to content

Commit cd57118

Browse files
committed
Revert the library loader
The library is currently not being loaded during the tests. Trying to debug why. Change-Id: Idd9e4f2b3d2d5c1986159d540657e0afea1f98e6
1 parent ad547b4 commit cd57118

File tree

4 files changed

+155
-212
lines changed

4 files changed

+155
-212
lines changed

src/main/java/com/eclipsesource/v8/LibraryLoader.java

Lines changed: 89 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*
88
* Contributors:
99
* EclipseSource - initial API and implementation
10-
* Wolfgang Steiner - code separation PlatformDetector/LibraryLoader
1110
******************************************************************************/
1211
package com.eclipsesource.v8;
1312

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

31-
/**
32-
* Returns the base-name for the native J2V8 library file.
33-
* @param withLinuxVendor include/exclude the {vendor} part from the returned filename
34-
* <p>NOTE: Vendors are only included for linux systems</p>
35-
* @return The filename string has the following structure:
36-
* <pre><code>{arch}-[vendor]-{operating_system}</pre></code>
37-
*/
38-
public static String computeLibraryShortName(boolean withLinuxVendor) {
39-
String prefix = "j2v8";
40-
String vendor = withLinuxVendor && PlatformDetector.OS.isLinux() ? PlatformDetector.Vendor.getName() : null;
41-
String os = PlatformDetector.OS.getName();
42-
String arch = PlatformDetector.Arch.getName();
43-
44-
final String separator = "-";
45-
46-
return
47-
prefix +
48-
(vendor != null ? separator + vendor : "") +
49-
separator + os +
50-
separator + arch;
30+
private static String computeLibraryShortName() {
31+
String base = "j2v8";
32+
String osSuffix = getOS();
33+
String archSuffix = getArchSuffix();
34+
return base + "_" + osSuffix + "_" + archSuffix;
5135
}
5236

53-
public static String computeLibraryFullName(boolean withLinuxVendor) {
54-
return "lib" + computeLibraryShortName(withLinuxVendor) + "." + PlatformDetector.OS.getLibFileExtension();
37+
private static String computeLibraryFullName() {
38+
return "lib" + computeLibraryShortName() + "." + getOSFileExtension();
5539
}
5640

57-
static boolean tryLoad(boolean withLinuxVendor, StringBuffer message) {
58-
String libShortName = computeLibraryShortName(withLinuxVendor);
59-
String libFullName = computeLibraryFullName(withLinuxVendor);
60-
String ideLocation = System.getProperty("user.dir") + SEPARATOR + "jni" + SEPARATOR + libFullName;
41+
static void loadLibrary(final String tempDirectory) {
42+
if ( isAndroid() ) {
43+
System.loadLibrary("j2v8");
44+
return;
45+
}
46+
StringBuffer message = new StringBuffer();
47+
String libShortName = computeLibraryShortName();
48+
String libFullName = computeLibraryFullName();
49+
String ideLocation = System.getProperty("user.dir") + SEPARATOR + "jni" + SEPARATOR + computeLibraryFullName();
50+
51+
String path = null;
6152

6253
/* Try loading library from java library path */
6354
if (load(libFullName, message)) {
64-
return true;
55+
return;
6556
}
6657
if (load(libShortName, message)) {
67-
return true;
58+
return;
6859
}
6960

7061
/* Try loading library from the IDE location */
7162
if (new File(ideLocation).exists()) {
7263
if (load(ideLocation, message)) {
73-
return true;
64+
return;
7465
}
7566
}
7667

77-
return false;
78-
}
79-
80-
static void loadLibrary(final String tempDirectory) {
81-
if (PlatformDetector.OS.isAndroid()) {
82-
System.loadLibrary("j2v8");
83-
return;
84-
}
85-
86-
StringBuffer message = new StringBuffer();
87-
88-
// try loading a vendor-specific library first
89-
if (tryLoad(true, message))
90-
return;
91-
92-
// if there is no vendor-specific library, just try to load the default OS library
93-
if (tryLoad(false, message))
94-
return;
95-
96-
String path = null;
97-
9868
if (tempDirectory != null) {
9969
path = tempDirectory;
10070
} else {
10171
path = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
10272
}
10373

104-
// try extracting a vendor-specific library first
105-
if (extract(path, true, message))
106-
return;
107-
108-
// if there is no vendor-specific library, just try to extract the default OS library
109-
if (extract(path, false, message))
74+
if (extract(path + SEPARATOR + libFullName, libFullName, message)) {
11075
return;
76+
}
11177

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

135-
static boolean extract(String libPath, boolean withLinuxVendor, StringBuffer message) {
136-
String libFullName = computeLibraryFullName(withLinuxVendor);
137-
return extract(libPath + SEPARATOR + libFullName, libFullName, message);
138-
}
139-
140101
static boolean extract(final String fileName, final String mappedName, final StringBuffer message) {
141102
FileOutputStream os = null;
142103
InputStream is = null;
@@ -183,12 +144,77 @@ static boolean extract(final String fileName, final String mappedName, final Str
183144
}
184145

185146
static void chmod(final String permision, final String path) {
186-
if (PlatformDetector.OS.isWindows()) {
147+
if (isWindows()) {
187148
return;
188149
}
189150
try {
190151
Runtime.getRuntime().exec(new String[] { "chmod", permision, path }).waitFor(); //$NON-NLS-1$
191152
} catch (Throwable e) {
192153
}
193154
}
155+
156+
static String getOsName() {
157+
return System.getProperty("os.name") + System.getProperty("java.specification.vendor");
158+
}
159+
160+
static boolean isWindows() {
161+
return getOsName().startsWith("Windows");
162+
}
163+
164+
static boolean isMac() {
165+
return getOsName().startsWith("Mac");
166+
}
167+
168+
static boolean isLinux() {
169+
return getOsName().startsWith("Linux");
170+
}
171+
172+
static boolean isNativeClient() {
173+
return getOsName().startsWith("nacl");
174+
}
175+
176+
static boolean isAndroid() {
177+
return getOsName().contains("Android");
178+
}
179+
180+
static String getArchSuffix() {
181+
String arch = System.getProperty("os.arch");
182+
if (arch.equals("i686")) {
183+
return "x86";
184+
} else if (arch.equals("amd64")) {
185+
return "x86_64";
186+
} else if (arch.equals("nacl")) {
187+
return "armv7l";
188+
} else if (arch.equals("aarch64")) {
189+
return "armv7l";
190+
}
191+
return arch;
192+
}
193+
194+
static String getOSFileExtension() {
195+
if (isWindows()) {
196+
return "dll";
197+
} else if (isMac()) {
198+
return "dylib";
199+
} else if (isLinux()) {
200+
return "so";
201+
} else if (isNativeClient()) {
202+
return "so";
203+
}
204+
throw new UnsatisfiedLinkError("Unsupported platform: " + getOsName());
205+
}
206+
207+
static String getOS() {
208+
if (isWindows()) {
209+
return "win32";
210+
} else if (isMac()) {
211+
return "macosx";
212+
} else if (isLinux() && !isAndroid()) {
213+
return "linux";
214+
} else if (isAndroid()) {
215+
return "android";
216+
}
217+
throw new UnsatisfiedLinkError("Unsupported platform: " + getOsName());
218+
}
219+
194220
}

src/main/java/com/eclipsesource/v8/V8.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,12 @@ private void notifyReferenceDisposed(final V8Value object) {
249249

250250
private static void checkNativeLibraryLoaded() {
251251
if (!nativeLibraryLoaded) {
252-
String vendorName = LibraryLoader.computeLibraryShortName(true);
253-
String baseName = LibraryLoader.computeLibraryShortName(false);
254-
String message = "J2V8 native library not loaded (" + baseName + "/" + vendorName + ")";
255-
256252
if (nativeLoadError != null) {
257-
throw new IllegalStateException(message, nativeLoadError);
253+
throw new IllegalStateException("J2V8 native library not loaded", nativeLoadError);
258254
} else if (nativeLoadException != null) {
259-
throw new IllegalStateException(message, nativeLoadException);
255+
throw new IllegalStateException("J2V8 native library not loaded", nativeLoadException);
260256
} else {
261-
throw new IllegalStateException(message);
257+
throw new IllegalStateException("J2V8 native library not loaded");
262258
}
263259
}
264260
}
@@ -1567,19 +1563,6 @@ protected void releaseMethodDescriptor(final long v8RuntimePtr, final long metho
15671563

15681564
private native static boolean _isRunning(final long v8RuntimePtr);
15691565

1570-
private native static boolean _isNodeCompatible();
1571-
1572-
public static boolean isNodeCompatible() {
1573-
if (!nativeLibraryLoaded) {
1574-
synchronized (lock) {
1575-
if (!nativeLibraryLoaded) {
1576-
load(null);
1577-
}
1578-
}
1579-
}
1580-
return _isNodeCompatible();
1581-
}
1582-
15831566
void addObjRef(final V8Value reference) {
15841567
objectReferences++;
15851568
if (!referenceHandlers.isEmpty()) {

0 commit comments

Comments
 (0)