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