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