2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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;
32 import org.openide.util.Exceptions;
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 final class FXInspect implements Runnable {
39 private static final Logger LOG = Logger.getLogger(FXInspect.class.getName());
42 private final WebEngine engine;
43 private final ObjectInputStream input;
45 private FXInspect(WebEngine engine, int port) throws IOException {
48 Socket socket = new Socket(InetAddress.getByName(null), port);
49 ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
50 this.input = new ObjectInputStream(socket.getInputStream());
51 initializeDebugger(output);
54 static boolean initialize(WebEngine engine) {
55 final int inspectPort = Integer.getInteger("netbeans.inspect.port", -1); // NOI18N
56 if (inspectPort != -1) {
58 FXInspect inspector = new FXInspect(engine, inspectPort);
59 Thread t = new Thread(inspector, "FX<->NetBeans Inspector");
62 } catch (IOException ex) {
63 LOG.log(Level.INFO, "Cannot connect to NetBeans IDE to port " + inspectPort, ex); // NOI18N
69 private void initializeDebugger(final ObjectOutputStream output) {
70 Platform.runLater(new Runnable() {
73 Debugger debugger = engine.impl_getDebugger();
74 debugger.setEnabled(true);
75 debugger.setMessageCallback(new Callback<String,Void>() {
77 public Void call(String message) {
79 byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
80 output.writeInt(bytes.length);
83 } catch (IOException ioex) {
84 ioex.printStackTrace();
97 int length = input.readInt();
98 byte[] bytes = new byte[length];
99 input.readFully(bytes);
100 final String message = new String(bytes, StandardCharsets.UTF_8);
101 Platform.runLater(new Runnable() {
104 engine.impl_getDebugger().sendMessage(message);
108 } catch (IOException ioex) {
109 ioex.printStackTrace();