-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathCN1Bootstrap.java
More file actions
277 lines (247 loc) · 11.9 KB
/
CN1Bootstrap.java
File metadata and controls
277 lines (247 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.impl.javase;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* A utility class which will dynamically add CEF to the classpath if it is available. This is
* used by the CSS compiler, and should also be used by other desktop CN1 apps that need to use
* CEF.
* @author shannah
*/
public class CN1Bootstrap {
private static ClassPathLoader rootClassLoader;
/**
* Checks if JavaFX is available on the classpath.
* @return
*/
public static boolean isJavaFXLoaded() {
try {
Class.forName("javafx.embed.swing.JFXPanel");
return true;
} catch (Throwable ex) {}
return false;
}
/**
* Checks if CEF is available on the classpath.
* @return
*/
public static boolean isCEFLoaded() {
try {
Class.forName("org.cef.CefApp");
return true;
} catch (Throwable ex){}
return false;
}
private static boolean hasFFmpeg() {
File ffmpegDir = getFFmpegDir();
if (ffmpegDir == null || !ffmpegDir.exists()) {
return false;
}
String suffix = isWindows ? ".exe" : "";
return new File(ffmpegDir, "ffmpeg" + suffix).exists() && new File(ffmpegDir, "ffprobe" + suffix).exists();
}
private static File getFFmpegDir() {
String path = System.getProperty("ffmpeg.dir");
if (path == null || path.isEmpty()) {
return null;
}
return new File(path);
}
/**
* Checks to see if this has already bootstrapped the classpath. This doesn't necessarily
* mean that CEF is on the classpath - it just means that this class has already attempted
* to add it to the classpath.
* @return
*/
public static boolean isBootstrapped() {
return System.getProperty("CN1Bootstrap", null) != null;
}
/**
* <p>Run the given mainclass with a bootstrapped classpath (i.e. it will first attempt to
* add CEF to the classpath, and then run the main() method of the given class using the
* bootstrapped classloader.</p>
*
* <p>NOTE: This will only execute the main class if bootstrapping had not already occurred. This is
* is to allow you to call this method inside your class's main() method without infinite recursion, as follows:</p>
*
* <pre>{@code
* public void main(String[] args) {
* if (CNBootstrap.run(MyClass.class, args)) return;
* // rest of main.. method
* }
* }</pre>
*
* @param mainClass The main class to run.
* @param argv Args to pass to the main() method.
* @return True If this method triggered a bootstrapping and ran the main method of the given class. False, if
* bootstrapping had already occurred and the main method was not executed.
* @throws Exception
*/
public static boolean run(Class mainClass, String[] argv) throws Exception {
return run(mainClass.getName(), argv);
}
/**
* Variant of run that takes the name of the main class rather than the class reference.
* This is useful for ensuring that the main class is loaded using the bootstrapped classloader.
* @param mainClass THe name of the main class
* @param argv Args to be passed to the main() method
* @return false if CN1 was already bootstrapped. true if CN1 was not yet bootstrapped, and this bootstrapped it.
* @throws Exception
*/
public static boolean run(String mainClass, String[] argv) throws Exception {
if (isBootstrapped()) {
return false;
}
System.setProperty("CN1Bootstrap", "true");
try {
// Load the sqlite database Engine JDBC driver in the top level classloader so it's shared
// this works around the exception: java.lang.UnsatisfiedLinkError: Native Library sqlite already loaded in another classloader
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ex) {
}
System.setProperty("NSHighResolutionCapable", "true");
StringTokenizer t = new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
List<File> files = new ArrayList<File>();
int len = t.countTokens();
for (int iter = 0; iter < len; iter++) {
files.add(new File(t.nextToken()));
}
File javase = new File("native" + File.separator + "javase");
File libJavase = new File("lib" + File.separator + "impl" + File.separator + "native" + File.separator + "javase");
for (File dir : new File[]{javase, libJavase}) {
if (dir.exists()) {
for (File jar : dir.listFiles()) {
if (jar.getName().endsWith(".jar") || jar.getName().endsWith(".zip")) {
if (!files.contains(jar)) {
files.add(jar);
System.setProperty("java.class.path", System.getProperty("java.class.path")+File.pathSeparator+jar.getAbsolutePath());
}
}
}
}
}
boolean cefSupported = false;
boolean fxSupported = false;
try {
Class.forName("javafx.embed.swing.JFXPanel");
fxSupported = true;
} catch (Throwable ex) {}
boolean fxOnSystemPath = fxSupported;
File cef = System.getProperty("cef.dir") != null ? new File(System.getProperty("cef.dir")) : new File(System.getProperty("user.home") + File.separator + ".codenameone" + File.separator + "cef");
//File cef = new File(System.getProperty("user.home") + File.separator + ".codenameone" + File.separator + "cef");
if (cef.exists()) {
if (isUnix && !is64Bit) {
System.out.println("Found CEF, but not using because CEF is only supported on 64 bit platforms. Try running inside a 64 bit JVM");
} else {
cefSupported = true;
System.out.println("Adding CEF to classpath " + cef);
String cn1LibPath = System.getProperty("cn1.library.path", ".");
String bitSuffix = is64Bit ? "64" : "32";
String nativeDir = isMac ? "macos64" : isWindows ? ("lib" + File.separator + "win"+bitSuffix) : ("lib" + File.separator + "linux"+bitSuffix);
System.setProperty("cn1.library.path", cn1LibPath + File.pathSeparator + cef.getAbsolutePath() + File.separator + nativeDir);
// Necessary to modify java.libary.path property on windows as it is used by CefApp to locate jcef_helper.exe
System.setProperty("java.library.path", cef.getAbsolutePath()+File.separator+nativeDir+File.pathSeparator+System.getProperty("java.library.path", "."));
for (File jar : cef.listFiles()) {
if (jar.getName().endsWith(".jar")) {
files.add(jar);
}
}
}
}
File jmf = new File(System.getProperty("user.home") + File.separator + ".codenameone" + File.separator + "jmf-2.1.1e.jar");
if (jmf.exists()) {
System.setProperty("java.class.path", System.getProperty("java.class.path") + File.pathSeparator + jmf.getAbsolutePath());
files.add(jmf);
}
String implementation = System.getProperty("cn1.javase.implementation", "");
if (implementation.equalsIgnoreCase("cef") && !cefSupported) {
// We will use CEF
System.err.println("cn1.javase.implementation=cef but CEF was not found. Please update your Codename One libraries and try again.\nAlternatively, you can try using a different implementation.");
System.exit(1);
}
if (implementation.equalsIgnoreCase("fx") && !fxSupported) {
System.err.println("cn1.javase.implementation=fx but JavaFX was not found. Please use a JDK that has JavaFX such as ZuluFX. https://www.azul.com/downloads/zulu-community/");
System.exit(1);
}
if ("".equals(implementation)) {
if (cefSupported) {
System.setProperty("cn1.javase.implementation", "cef");
} else if (fxSupported) {
System.setProperty("cn1.javase.implementation", "fx");
} else {
System.setProperty("cn1.javase.implementation", "jmf");
}
}
String mediaImplementation = System.getProperty("cn1.javase.mediaImplementation", "");
if ("".equals(mediaImplementation)) {
if (hasFFmpeg()) {
System.setProperty("cn1.javase.mediaImplementation", "ffmpeg");
} else if (fxSupported) {
System.setProperty("cn1.javase.mediaImplementation", "fx");
} else {
System.setProperty("cn1.javase.mediaImplementation", "jmf");
}
}
//loadFXRuntime();
ClassLoader ldr = rootClassLoader == null ?
new ClassPathLoader( files.toArray(new File[files.size()])) :
new ClassPathLoader(rootClassLoader, files.toArray(new File[files.size()]));
if (rootClassLoader == null) {
rootClassLoader = (ClassPathLoader)ldr;
ldr = new ClassPathLoader(rootClassLoader, files.toArray(new File[files.size()]));
}
((ClassPathLoader)ldr).addExclude("org.cef.");
final ClassLoader fLdr = ldr;
Class c = Class.forName(mainClass, true, ldr);
Method m = c.getDeclaredMethod("main", String[].class);
m.invoke(null, new Object[]{argv});
return true;
}
private static String OS = System.getProperty("os.name").toLowerCase();
private static boolean isWindows = (OS.indexOf("win") >= 0);
private static boolean isMac = (OS.indexOf("mac") >= 0);
private static final String ARCH = System.getProperty("os.arch");
private static boolean isUnix = (OS.indexOf("nux") >= 0);
private static final boolean is64Bit = is64Bit();
private static final boolean is64Bit() {
String model = System.getProperty("sun.arch.data.model",
System.getProperty("com.ibm.vm.bitmode"));
if (model != null) {
return "64".equals(model);
}
if ("x86-64".equals(ARCH)
|| "ia64".equals(ARCH)
|| "ppc64".equals(ARCH) || "ppc64le".equals(ARCH)
|| "sparcv9".equals(ARCH)
|| "mips64".equals(ARCH) || "mips64el".equals(ARCH)
|| "amd64".equals(ARCH)
|| "aarch64".equals(ARCH)) {
return true;
}
return false;
}
}