launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXInspect.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Apr 2014 15:04:10 +0200
branchclosure
changeset 1513 ba912ef24b27
parent 1283 1d0e583ac981
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Merging from default branch and resolving conflicts. mvn install -DskipTests passes OK.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher.fximpl;
    19 
    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 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 final class FXInspect implements Runnable {
    38     private static final Logger LOG = Logger.getLogger(FXInspect.class.getName());
    39     
    40     
    41     private final WebEngine engine;
    42     private final ObjectInputStream input;
    43     
    44     private FXInspect(WebEngine engine, int port) throws IOException {
    45         this.engine = engine;
    46         
    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);
    51     }
    52     
    53     static boolean initialize(WebEngine engine) {
    54         final int inspectPort = Integer.getInteger("netbeans.inspect.port", -1); // NOI18N
    55         if (inspectPort != -1) {
    56             try {
    57                 FXInspect inspector = new FXInspect(engine, inspectPort);
    58                 Thread t = new Thread(inspector, "FX<->NetBeans Inspector");
    59                 t.start();
    60                 return true;
    61             } catch (IOException ex) {
    62                 LOG.log(Level.INFO, "Cannot connect to NetBeans IDE to port " + inspectPort, ex); // NOI18N
    63             }
    64         }
    65         return false;
    66     }
    67     
    68     private void initializeDebugger(final ObjectOutputStream output) {
    69         Platform.runLater(new Runnable() {
    70             @Override
    71             public void run() {
    72                 Debugger debugger = engine.impl_getDebugger();
    73                 debugger.setEnabled(true);
    74                 debugger.setMessageCallback(new Callback<String,Void>() {
    75                     @Override
    76                     public Void call(String message) {
    77                         try {
    78                             byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
    79                             output.writeInt(bytes.length);
    80                             output.write(bytes);
    81                             output.flush();
    82                         } catch (IOException ioex) {
    83                             ioex.printStackTrace();
    84                         }
    85                         return null;
    86                     }
    87                 });
    88             }
    89         });
    90     }
    91 
    92     @Override
    93     public void run() {
    94         try {
    95             while (true) {
    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() {
   101                     @Override
   102                     public void run() {
   103                         engine.impl_getDebugger().sendMessage(message);
   104                     }
   105                 });
   106             }
   107         } catch (IOException ioex) {
   108             ioex.printStackTrace();
   109         }
   110     }
   111 }