jaroslav@1283
|
1 |
/**
|
jaroslav@1283
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1787
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1283
|
4 |
*
|
jaroslav@1283
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1283
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1283
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1283
|
8 |
*
|
jaroslav@1283
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1283
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1283
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1283
|
12 |
* GNU General Public License for more details.
|
jaroslav@1283
|
13 |
*
|
jaroslav@1283
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1283
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1283
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1283
|
17 |
*/
|
jaroslav@1283
|
18 |
package org.apidesign.bck2brwsr.launcher.fximpl;
|
jaroslav@1283
|
19 |
|
jaroslav@1283
|
20 |
import com.sun.javafx.scene.web.Debugger;
|
jaroslav@1283
|
21 |
import java.io.IOException;
|
jaroslav@1283
|
22 |
import java.io.ObjectInputStream;
|
jaroslav@1283
|
23 |
import java.io.ObjectOutputStream;
|
jaroslav@1283
|
24 |
import java.net.InetAddress;
|
jaroslav@1283
|
25 |
import java.net.Socket;
|
jaroslav@1283
|
26 |
import java.nio.charset.StandardCharsets;
|
jaroslav@1283
|
27 |
import java.util.logging.Level;
|
jaroslav@1283
|
28 |
import java.util.logging.Logger;
|
jaroslav@1283
|
29 |
import javafx.application.Platform;
|
jaroslav@1283
|
30 |
import javafx.scene.web.WebEngine;
|
jaroslav@1283
|
31 |
import javafx.util.Callback;
|
jaroslav@1283
|
32 |
|
jaroslav@1283
|
33 |
/**
|
jaroslav@1283
|
34 |
*
|
jaroslav@1283
|
35 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1283
|
36 |
*/
|
jaroslav@1283
|
37 |
final class FXInspect implements Runnable {
|
jaroslav@1283
|
38 |
private static final Logger LOG = Logger.getLogger(FXInspect.class.getName());
|
jaroslav@1283
|
39 |
|
jaroslav@1283
|
40 |
|
jaroslav@1283
|
41 |
private final WebEngine engine;
|
jaroslav@1283
|
42 |
private final ObjectInputStream input;
|
jaroslav@1283
|
43 |
|
jaroslav@1283
|
44 |
private FXInspect(WebEngine engine, int port) throws IOException {
|
jaroslav@1283
|
45 |
this.engine = engine;
|
jaroslav@1283
|
46 |
|
jaroslav@1283
|
47 |
Socket socket = new Socket(InetAddress.getByName(null), port);
|
jaroslav@1283
|
48 |
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
|
jaroslav@1283
|
49 |
this.input = new ObjectInputStream(socket.getInputStream());
|
jaroslav@1283
|
50 |
initializeDebugger(output);
|
jaroslav@1283
|
51 |
}
|
jaroslav@1283
|
52 |
|
jaroslav@1283
|
53 |
static boolean initialize(WebEngine engine) {
|
jaroslav@1283
|
54 |
final int inspectPort = Integer.getInteger("netbeans.inspect.port", -1); // NOI18N
|
jaroslav@1283
|
55 |
if (inspectPort != -1) {
|
jaroslav@1283
|
56 |
try {
|
jaroslav@1283
|
57 |
FXInspect inspector = new FXInspect(engine, inspectPort);
|
jaroslav@1283
|
58 |
Thread t = new Thread(inspector, "FX<->NetBeans Inspector");
|
jaroslav@1283
|
59 |
t.start();
|
jaroslav@1283
|
60 |
return true;
|
jaroslav@1283
|
61 |
} catch (IOException ex) {
|
jaroslav@1283
|
62 |
LOG.log(Level.INFO, "Cannot connect to NetBeans IDE to port " + inspectPort, ex); // NOI18N
|
jaroslav@1283
|
63 |
}
|
jaroslav@1283
|
64 |
}
|
jaroslav@1283
|
65 |
return false;
|
jaroslav@1283
|
66 |
}
|
jaroslav@1283
|
67 |
|
jaroslav@1283
|
68 |
private void initializeDebugger(final ObjectOutputStream output) {
|
jaroslav@1283
|
69 |
Platform.runLater(new Runnable() {
|
jaroslav@1283
|
70 |
@Override
|
jaroslav@1283
|
71 |
public void run() {
|
jaroslav@1283
|
72 |
Debugger debugger = engine.impl_getDebugger();
|
jaroslav@1283
|
73 |
debugger.setEnabled(true);
|
jaroslav@1283
|
74 |
debugger.setMessageCallback(new Callback<String,Void>() {
|
jaroslav@1283
|
75 |
@Override
|
jaroslav@1283
|
76 |
public Void call(String message) {
|
jaroslav@1283
|
77 |
try {
|
jaroslav@1283
|
78 |
byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
|
jaroslav@1283
|
79 |
output.writeInt(bytes.length);
|
jaroslav@1283
|
80 |
output.write(bytes);
|
jaroslav@1283
|
81 |
output.flush();
|
jaroslav@1283
|
82 |
} catch (IOException ioex) {
|
jaroslav@1283
|
83 |
ioex.printStackTrace();
|
jaroslav@1283
|
84 |
}
|
jaroslav@1283
|
85 |
return null;
|
jaroslav@1283
|
86 |
}
|
jaroslav@1283
|
87 |
});
|
jaroslav@1283
|
88 |
}
|
jaroslav@1283
|
89 |
});
|
jaroslav@1283
|
90 |
}
|
jaroslav@1283
|
91 |
|
jaroslav@1283
|
92 |
@Override
|
jaroslav@1283
|
93 |
public void run() {
|
jaroslav@1283
|
94 |
try {
|
jaroslav@1283
|
95 |
while (true) {
|
jaroslav@1283
|
96 |
int length = input.readInt();
|
jaroslav@1283
|
97 |
byte[] bytes = new byte[length];
|
jaroslav@1283
|
98 |
input.readFully(bytes);
|
jaroslav@1283
|
99 |
final String message = new String(bytes, StandardCharsets.UTF_8);
|
jaroslav@1283
|
100 |
Platform.runLater(new Runnable() {
|
jaroslav@1283
|
101 |
@Override
|
jaroslav@1283
|
102 |
public void run() {
|
jaroslav@1283
|
103 |
engine.impl_getDebugger().sendMessage(message);
|
jaroslav@1283
|
104 |
}
|
jaroslav@1283
|
105 |
});
|
jaroslav@1283
|
106 |
}
|
jaroslav@1283
|
107 |
} catch (IOException ioex) {
|
jaroslav@1283
|
108 |
ioex.printStackTrace();
|
jaroslav@1283
|
109 |
}
|
jaroslav@1283
|
110 |
}
|
jaroslav@1283
|
111 |
}
|