1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXInspect.java Sun Sep 15 18:24:48 2013 +0200
1.3 @@ -0,0 +1,112 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
1.22 +
1.23 +import com.sun.javafx.scene.web.Debugger;
1.24 +import java.io.IOException;
1.25 +import java.io.ObjectInputStream;
1.26 +import java.io.ObjectOutputStream;
1.27 +import java.net.InetAddress;
1.28 +import java.net.Socket;
1.29 +import java.nio.charset.StandardCharsets;
1.30 +import java.util.logging.Level;
1.31 +import java.util.logging.Logger;
1.32 +import javafx.application.Platform;
1.33 +import javafx.scene.web.WebEngine;
1.34 +import javafx.util.Callback;
1.35 +import org.openide.util.Exceptions;
1.36 +
1.37 +/**
1.38 + *
1.39 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.40 + */
1.41 +final class FXInspect implements Runnable {
1.42 + private static final Logger LOG = Logger.getLogger(FXInspect.class.getName());
1.43 +
1.44 +
1.45 + private final WebEngine engine;
1.46 + private final ObjectInputStream input;
1.47 +
1.48 + private FXInspect(WebEngine engine, int port) throws IOException {
1.49 + this.engine = engine;
1.50 +
1.51 + Socket socket = new Socket(InetAddress.getByName(null), port);
1.52 + ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
1.53 + this.input = new ObjectInputStream(socket.getInputStream());
1.54 + initializeDebugger(output);
1.55 + }
1.56 +
1.57 + static boolean initialize(WebEngine engine) {
1.58 + final int inspectPort = Integer.getInteger("netbeans.inspect.port", -1); // NOI18N
1.59 + if (inspectPort != -1) {
1.60 + try {
1.61 + FXInspect inspector = new FXInspect(engine, inspectPort);
1.62 + Thread t = new Thread(inspector, "FX<->NetBeans Inspector");
1.63 + t.start();
1.64 + return true;
1.65 + } catch (IOException ex) {
1.66 + LOG.log(Level.INFO, "Cannot connect to NetBeans IDE to port " + inspectPort, ex); // NOI18N
1.67 + }
1.68 + }
1.69 + return false;
1.70 + }
1.71 +
1.72 + private void initializeDebugger(final ObjectOutputStream output) {
1.73 + Platform.runLater(new Runnable() {
1.74 + @Override
1.75 + public void run() {
1.76 + Debugger debugger = engine.impl_getDebugger();
1.77 + debugger.setEnabled(true);
1.78 + debugger.setMessageCallback(new Callback<String,Void>() {
1.79 + @Override
1.80 + public Void call(String message) {
1.81 + try {
1.82 + byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
1.83 + output.writeInt(bytes.length);
1.84 + output.write(bytes);
1.85 + output.flush();
1.86 + } catch (IOException ioex) {
1.87 + ioex.printStackTrace();
1.88 + }
1.89 + return null;
1.90 + }
1.91 + });
1.92 + }
1.93 + });
1.94 + }
1.95 +
1.96 + @Override
1.97 + public void run() {
1.98 + try {
1.99 + while (true) {
1.100 + int length = input.readInt();
1.101 + byte[] bytes = new byte[length];
1.102 + input.readFully(bytes);
1.103 + final String message = new String(bytes, StandardCharsets.UTF_8);
1.104 + Platform.runLater(new Runnable() {
1.105 + @Override
1.106 + public void run() {
1.107 + engine.impl_getDebugger().sendMessage(message);
1.108 + }
1.109 + });
1.110 + }
1.111 + } catch (IOException ioex) {
1.112 + ioex.printStackTrace();
1.113 + }
1.114 + }
1.115 +}