7
7
*
8
8
* Contributors:
9
9
* EclipseSource - initial API and implementation
10
- * Wolfgang Steiner - code separation PlatformDetector/LibraryLoader
11
10
******************************************************************************/
12
11
package com .eclipsesource .v8 ;
13
12
@@ -28,86 +27,53 @@ class LibraryLoader {
28
27
SEPARATOR = System .getProperty ("file.separator" ); //$NON-NLS-1$
29
28
}
30
29
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 ;
51
35
}
52
36
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 ();
55
39
}
56
40
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 ;
61
52
62
53
/* Try loading library from java library path */
63
54
if (load (libFullName , message )) {
64
- return true ;
55
+ return ;
65
56
}
66
57
if (load (libShortName , message )) {
67
- return true ;
58
+ return ;
68
59
}
69
60
70
61
/* Try loading library from the IDE location */
71
62
if (new File (ideLocation ).exists ()) {
72
63
if (load (ideLocation , message )) {
73
- return true ;
64
+ return ;
74
65
}
75
66
}
76
67
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
-
98
68
if (tempDirectory != null ) {
99
69
path = tempDirectory ;
100
70
} else {
101
71
path = System .getProperty ("java.io.tmpdir" ); //$NON-NLS-1$
102
72
}
103
73
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 )) {
110
75
return ;
76
+ }
111
77
112
78
/* Failed to find the library */
113
79
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) {
132
98
return false ;
133
99
}
134
100
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
-
140
101
static boolean extract (final String fileName , final String mappedName , final StringBuffer message ) {
141
102
FileOutputStream os = null ;
142
103
InputStream is = null ;
@@ -183,12 +144,77 @@ static boolean extract(final String fileName, final String mappedName, final Str
183
144
}
184
145
185
146
static void chmod (final String permision , final String path ) {
186
- if (PlatformDetector . OS . isWindows ()) {
147
+ if (isWindows ()) {
187
148
return ;
188
149
}
189
150
try {
190
151
Runtime .getRuntime ().exec (new String [] { "chmod" , permision , path }).waitFor (); //$NON-NLS-1$
191
152
} catch (Throwable e ) {
192
153
}
193
154
}
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
+
194
220
}
0 commit comments