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@1053
|
22 |
import java.io.InputStream;
|
jaroslav@1041
|
23 |
import java.lang.reflect.Method;
|
jaroslav@1041
|
24 |
import java.net.URI;
|
jaroslav@1041
|
25 |
import java.net.URL;
|
jaroslav@1041
|
26 |
import java.net.URLClassLoader;
|
jaroslav@1053
|
27 |
import java.util.Enumeration;
|
jaroslav@1041
|
28 |
|
jaroslav@1041
|
29 |
import java.util.concurrent.Executors;
|
jaroslav@1053
|
30 |
import java.util.jar.Manifest;
|
jaroslav@1041
|
31 |
import java.util.logging.Level;
|
jaroslav@1041
|
32 |
import java.util.logging.Logger;
|
jaroslav@1041
|
33 |
import javafx.application.Platform;
|
jaroslav@1041
|
34 |
import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
|
jaroslav@1041
|
35 |
|
jaroslav@1041
|
36 |
/**
|
jaroslav@1041
|
37 |
*
|
jaroslav@1041
|
38 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1041
|
39 |
*/
|
jaroslav@1041
|
40 |
final class FXBrwsrLauncher extends BaseHTTPLauncher {
|
jaroslav@1041
|
41 |
private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
|
jaroslav@1041
|
42 |
static {
|
jaroslav@1041
|
43 |
try {
|
jaroslav@1041
|
44 |
Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
jaroslav@1041
|
45 |
m.setAccessible(true);
|
jaroslav@1041
|
46 |
URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
|
jaroslav@1041
|
47 |
LOG.log(Level.INFO, "url : {0}", l);
|
jaroslav@1041
|
48 |
m.invoke(ClassLoader.getSystemClassLoader(), l);
|
jaroslav@1041
|
49 |
} catch (Exception ex) {
|
jaroslav@1041
|
50 |
throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
|
jaroslav@1041
|
51 |
}
|
jaroslav@1041
|
52 |
}
|
jaroslav@1053
|
53 |
|
jaroslav@1041
|
54 |
public FXBrwsrLauncher(String ignore) {
|
jaroslav@1041
|
55 |
super(null);
|
jaroslav@1041
|
56 |
}
|
jaroslav@1041
|
57 |
|
jaroslav@1041
|
58 |
@Override
|
jaroslav@1041
|
59 |
protected Object[] showBrwsr(final URI url) throws IOException {
|
jaroslav@1041
|
60 |
try {
|
jaroslav@1041
|
61 |
LOG.log(Level.INFO, "showBrwsr for {0}", url);
|
jaroslav@1041
|
62 |
JVMBridge.registerClassLoaders(loaders());
|
jaroslav@1041
|
63 |
LOG.info("About to launch WebView");
|
jaroslav@1041
|
64 |
Executors.newSingleThreadExecutor().submit(new Runnable() {
|
jaroslav@1041
|
65 |
@Override
|
jaroslav@1041
|
66 |
public void run() {
|
jaroslav@1041
|
67 |
LOG.log(Level.INFO, "In FX thread. Launching!");
|
jaroslav@1041
|
68 |
try {
|
jaroslav@1041
|
69 |
FXBrwsr.launch(FXBrwsr.class, url.toString());
|
jaroslav@1041
|
70 |
LOG.log(Level.INFO, "Launcher is back. Closing");
|
jaroslav@1041
|
71 |
close();
|
jaroslav@1041
|
72 |
} catch (Throwable ex) {
|
jaroslav@1041
|
73 |
LOG.log(Level.WARNING, "Error launching Web View", ex);
|
jaroslav@1041
|
74 |
}
|
jaroslav@1041
|
75 |
}
|
jaroslav@1041
|
76 |
});
|
jaroslav@1041
|
77 |
} catch (Throwable ex) {
|
jaroslav@1041
|
78 |
LOG.log(Level.WARNING, "Can't open WebView", ex);
|
jaroslav@1041
|
79 |
}
|
jaroslav@1041
|
80 |
return null;
|
jaroslav@1041
|
81 |
}
|
jaroslav@1041
|
82 |
|
jaroslav@1041
|
83 |
@Override
|
jaroslav@1088
|
84 |
void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
|
jaroslav@1041
|
85 |
sb.append("(function() {\n"
|
jaroslav@1041
|
86 |
+ " var impl = this.bck2brwsr;\n"
|
jaroslav@1041
|
87 |
+ " this.bck2brwsr = function() { return impl; };\n"
|
jaroslav@1041
|
88 |
+ "})(window);\n"
|
jaroslav@1041
|
89 |
);
|
jaroslav@1041
|
90 |
JVMBridge.onBck2BrwsrLoad();
|
jaroslav@1041
|
91 |
}
|
jaroslav@1041
|
92 |
|
jaroslav@1041
|
93 |
|
jaroslav@1041
|
94 |
|
jaroslav@1041
|
95 |
@Override
|
jaroslav@1041
|
96 |
public void close() throws IOException {
|
jaroslav@1041
|
97 |
super.close();
|
jaroslav@1041
|
98 |
Platform.exit();
|
jaroslav@1041
|
99 |
}
|
jaroslav@1053
|
100 |
|
jaroslav@1088
|
101 |
String harnessResource() {
|
jaroslav@1088
|
102 |
return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
|
jaroslav@1088
|
103 |
}
|
jaroslav@1088
|
104 |
|
jaroslav@1053
|
105 |
public static void main(String... args) throws IOException {
|
jaroslav@1053
|
106 |
String startPage = null;
|
jaroslav@1053
|
107 |
|
jaroslav@1053
|
108 |
final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
|
jaroslav@1053
|
109 |
startPage = findStartPage(cl, startPage);
|
jaroslav@1053
|
110 |
if (startPage == null) {
|
jaroslav@1053
|
111 |
throw new NullPointerException("Can't find StartPage tag in manifests!");
|
jaroslav@1053
|
112 |
}
|
jaroslav@1053
|
113 |
|
jaroslav@1053
|
114 |
Launcher.showURL("fxbrwsr", cl, startPage);
|
jaroslav@1053
|
115 |
}
|
jaroslav@1041
|
116 |
|
jaroslav@1053
|
117 |
private static String findStartPage(final ClassLoader cl, String startPage) throws IOException {
|
jaroslav@1053
|
118 |
Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
|
jaroslav@1053
|
119 |
while (en.hasMoreElements()) {
|
jaroslav@1053
|
120 |
URL url = en.nextElement();
|
jaroslav@1053
|
121 |
Manifest mf;
|
jaroslav@1053
|
122 |
try (InputStream is = url.openStream()) {
|
jaroslav@1053
|
123 |
mf = new Manifest(is);
|
jaroslav@1053
|
124 |
}
|
jaroslav@1053
|
125 |
String sp = mf.getMainAttributes().getValue("StartPage");
|
jaroslav@1053
|
126 |
if (sp != null) {
|
jaroslav@1053
|
127 |
startPage = sp;
|
jaroslav@1053
|
128 |
break;
|
jaroslav@1053
|
129 |
}
|
jaroslav@1053
|
130 |
}
|
jaroslav@1053
|
131 |
return startPage;
|
jaroslav@1053
|
132 |
}
|
jaroslav@1041
|
133 |
}
|