launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXInspect.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 06 Jan 2014 13:31:04 +0100
branchNbHtml4J
changeset 1418 b8ff900a542d
child 1513 ba912ef24b27
permissions -rw-r--r--
Switching to new 'NetBeans' packaging schema of forthcoming 0.7 version
     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 import org.openide.util.Exceptions;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 final class FXInspect implements Runnable {
    39     private static final Logger LOG = Logger.getLogger(FXInspect.class.getName());
    40     
    41     
    42     private final WebEngine engine;
    43     private final ObjectInputStream input;
    44     
    45     private FXInspect(WebEngine engine, int port) throws IOException {
    46         this.engine = engine;
    47         
    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);
    52     }
    53     
    54     static boolean initialize(WebEngine engine) {
    55         final int inspectPort = Integer.getInteger("netbeans.inspect.port", -1); // NOI18N
    56         if (inspectPort != -1) {
    57             try {
    58                 FXInspect inspector = new FXInspect(engine, inspectPort);
    59                 Thread t = new Thread(inspector, "FX<->NetBeans Inspector");
    60                 t.start();
    61                 return true;
    62             } catch (IOException ex) {
    63                 LOG.log(Level.INFO, "Cannot connect to NetBeans IDE to port " + inspectPort, ex); // NOI18N
    64             }
    65         }
    66         return false;
    67     }
    68     
    69     private void initializeDebugger(final ObjectOutputStream output) {
    70         Platform.runLater(new Runnable() {
    71             @Override
    72             public void run() {
    73                 Debugger debugger = engine.impl_getDebugger();
    74                 debugger.setEnabled(true);
    75                 debugger.setMessageCallback(new Callback<String,Void>() {
    76                     @Override
    77                     public Void call(String message) {
    78                         try {
    79                             byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
    80                             output.writeInt(bytes.length);
    81                             output.write(bytes);
    82                             output.flush();
    83                         } catch (IOException ioex) {
    84                             ioex.printStackTrace();
    85                         }
    86                         return null;
    87                     }
    88                 });
    89             }
    90         });
    91     }
    92 
    93     @Override
    94     public void run() {
    95         try {
    96             while (true) {
    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() {
   102                     @Override
   103                     public void run() {
   104                         engine.impl_getDebugger().sendMessage(message);
   105                     }
   106                 });
   107             }
   108         } catch (IOException ioex) {
   109             ioex.printStackTrace();
   110         }
   111     }
   112 }