jaroslav@1041
|
1 |
/**
|
jaroslav@1041
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1041
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1041
|
4 |
*
|
jaroslav@1041
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1041
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1041
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1041
|
8 |
*
|
jaroslav@1041
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1041
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1041
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1041
|
12 |
* GNU General Public License for more details.
|
jaroslav@1041
|
13 |
*
|
jaroslav@1041
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1041
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1041
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1041
|
17 |
*/
|
jaroslav@1041
|
18 |
package org.apidesign.bck2brwsr.launcher;
|
jaroslav@1041
|
19 |
|
jaroslav@1041
|
20 |
import org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr;
|
jaroslav@1041
|
21 |
import java.io.IOException;
|
jaroslav@1041
|
22 |
import java.lang.reflect.Method;
|
jaroslav@1041
|
23 |
import java.net.URI;
|
jaroslav@1041
|
24 |
import java.net.URL;
|
jaroslav@1041
|
25 |
import java.net.URLClassLoader;
|
jaroslav@1041
|
26 |
|
jaroslav@1041
|
27 |
import java.util.concurrent.Executors;
|
jaroslav@1041
|
28 |
import java.util.logging.Level;
|
jaroslav@1041
|
29 |
import java.util.logging.Logger;
|
jaroslav@1041
|
30 |
import javafx.application.Platform;
|
jaroslav@1041
|
31 |
import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
|
jaroslav@1041
|
32 |
|
jaroslav@1041
|
33 |
/**
|
jaroslav@1041
|
34 |
*
|
jaroslav@1041
|
35 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1041
|
36 |
*/
|
jaroslav@1041
|
37 |
final class FXBrwsrLauncher extends BaseHTTPLauncher {
|
jaroslav@1041
|
38 |
private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
|
jaroslav@1041
|
39 |
static {
|
jaroslav@1041
|
40 |
try {
|
jaroslav@1041
|
41 |
Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
jaroslav@1041
|
42 |
m.setAccessible(true);
|
jaroslav@1041
|
43 |
URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
|
jaroslav@1041
|
44 |
LOG.log(Level.INFO, "url : {0}", l);
|
jaroslav@1041
|
45 |
m.invoke(ClassLoader.getSystemClassLoader(), l);
|
jaroslav@1041
|
46 |
} catch (Exception ex) {
|
jaroslav@1041
|
47 |
throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
|
jaroslav@1041
|
48 |
}
|
jaroslav@1041
|
49 |
}
|
jaroslav@1041
|
50 |
|
jaroslav@1041
|
51 |
public FXBrwsrLauncher(String ignore) {
|
jaroslav@1041
|
52 |
super(null);
|
jaroslav@1041
|
53 |
}
|
jaroslav@1041
|
54 |
|
jaroslav@1041
|
55 |
@Override
|
jaroslav@1041
|
56 |
protected Object[] showBrwsr(final URI url) throws IOException {
|
jaroslav@1041
|
57 |
try {
|
jaroslav@1041
|
58 |
LOG.log(Level.INFO, "showBrwsr for {0}", url);
|
jaroslav@1041
|
59 |
JVMBridge.registerClassLoaders(loaders());
|
jaroslav@1041
|
60 |
LOG.info("About to launch WebView");
|
jaroslav@1041
|
61 |
Executors.newSingleThreadExecutor().submit(new Runnable() {
|
jaroslav@1041
|
62 |
@Override
|
jaroslav@1041
|
63 |
public void run() {
|
jaroslav@1041
|
64 |
LOG.log(Level.INFO, "In FX thread. Launching!");
|
jaroslav@1041
|
65 |
try {
|
jaroslav@1041
|
66 |
FXBrwsr.launch(FXBrwsr.class, url.toString());
|
jaroslav@1041
|
67 |
LOG.log(Level.INFO, "Launcher is back. Closing");
|
jaroslav@1041
|
68 |
close();
|
jaroslav@1041
|
69 |
} catch (Throwable ex) {
|
jaroslav@1041
|
70 |
LOG.log(Level.WARNING, "Error launching Web View", ex);
|
jaroslav@1041
|
71 |
}
|
jaroslav@1041
|
72 |
}
|
jaroslav@1041
|
73 |
});
|
jaroslav@1041
|
74 |
} catch (Throwable ex) {
|
jaroslav@1041
|
75 |
LOG.log(Level.WARNING, "Can't open WebView", ex);
|
jaroslav@1041
|
76 |
}
|
jaroslav@1041
|
77 |
return null;
|
jaroslav@1041
|
78 |
}
|
jaroslav@1041
|
79 |
|
jaroslav@1041
|
80 |
@Override
|
jaroslav@1043
|
81 |
void generateBck2BrwsrJS(StringBuilder sb, Object loader) throws IOException {
|
jaroslav@1041
|
82 |
sb.append("(function() {\n"
|
jaroslav@1041
|
83 |
+ " var impl = this.bck2brwsr;\n"
|
jaroslav@1041
|
84 |
+ " this.bck2brwsr = function() { return impl; };\n"
|
jaroslav@1041
|
85 |
+ "})(window);\n"
|
jaroslav@1041
|
86 |
);
|
jaroslav@1041
|
87 |
JVMBridge.onBck2BrwsrLoad();
|
jaroslav@1041
|
88 |
}
|
jaroslav@1041
|
89 |
|
jaroslav@1041
|
90 |
|
jaroslav@1041
|
91 |
|
jaroslav@1041
|
92 |
@Override
|
jaroslav@1041
|
93 |
public void close() throws IOException {
|
jaroslav@1041
|
94 |
super.close();
|
jaroslav@1041
|
95 |
Platform.exit();
|
jaroslav@1041
|
96 |
}
|
jaroslav@1041
|
97 |
|
jaroslav@1041
|
98 |
}
|