boot-fx/src/main/java/org/netbeans/html/boot/fx/FXInspect.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 739 7a2f7066fc67
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@243
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@243
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@243
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@243
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@243
    42
 */
jaroslav@362
    43
package org.netbeans.html.boot.fx;
jaroslav@243
    44
jaroslav@243
    45
import java.io.IOException;
jaroslav@243
    46
import java.io.ObjectInputStream;
jaroslav@243
    47
import java.io.ObjectOutputStream;
jaroslav@243
    48
import java.net.InetAddress;
jaroslav@243
    49
import java.net.Socket;
jaroslav@243
    50
import java.nio.charset.StandardCharsets;
jaroslav@243
    51
import java.util.logging.Level;
jaroslav@243
    52
import java.util.logging.Logger;
jaroslav@243
    53
import javafx.application.Platform;
jaroslav@243
    54
import javafx.scene.web.WebEngine;
jaroslav@243
    55
import javafx.util.Callback;
jaroslav@243
    56
jaroslav@243
    57
/**
jaroslav@243
    58
 *
jaroslav@243
    59
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@243
    60
 */
jaroslav@243
    61
final class FXInspect implements Runnable {
jaroslav@243
    62
    private static final Logger LOG = Logger.getLogger(FXInspect.class.getName());
jaroslav@243
    63
    
jaroslav@243
    64
    
jaroslav@243
    65
    private final WebEngine engine;
jaroslav@243
    66
    private final ObjectInputStream input;
jaroslav@316
    67
    private Dbgr dbg;
jaroslav@243
    68
    
jaroslav@243
    69
    private FXInspect(WebEngine engine, int port) throws IOException {
jaroslav@243
    70
        this.engine = engine;
jaroslav@243
    71
        
jaroslav@243
    72
        Socket socket = new Socket(InetAddress.getByName(null), port);
jaroslav@243
    73
        ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
jaroslav@243
    74
        this.input = new ObjectInputStream(socket.getInputStream());
jaroslav@243
    75
        initializeDebugger(output);
jaroslav@243
    76
    }
jaroslav@243
    77
    
jaroslav@253
    78
    static boolean initialize(WebEngine engine) {
jaroslav@243
    79
        final int inspectPort = Integer.getInteger("netbeans.inspect.port", -1); // NOI18N
jaroslav@243
    80
        if (inspectPort != -1) {
jaroslav@243
    81
            try {
jaroslav@243
    82
                FXInspect inspector = new FXInspect(engine, inspectPort);
jaroslav@243
    83
                Thread t = new Thread(inspector, "FX<->NetBeans Inspector");
jaroslav@243
    84
                t.start();
jaroslav@253
    85
                return true;
jaroslav@243
    86
            } catch (IOException ex) {
jaroslav@243
    87
                LOG.log(Level.INFO, "Cannot connect to NetBeans IDE to port " + inspectPort, ex); // NOI18N
jaroslav@243
    88
            }
jaroslav@243
    89
        }
jaroslav@253
    90
        return false;
jaroslav@243
    91
    }
jaroslav@243
    92
    
jaroslav@243
    93
    private void initializeDebugger(final ObjectOutputStream output) {
jaroslav@243
    94
        Platform.runLater(new Runnable() {
jaroslav@243
    95
            @Override
jaroslav@243
    96
            public void run() {
jaroslav@316
    97
                dbg = new Dbgr(engine, new Callback<String,Void>() {
jaroslav@243
    98
                    @Override
jaroslav@243
    99
                    public Void call(String message) {
jaroslav@243
   100
                        try {
jaroslav@243
   101
                            byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
jaroslav@243
   102
                            output.writeInt(bytes.length);
jaroslav@243
   103
                            output.write(bytes);
jaroslav@243
   104
                            output.flush();
jaroslav@243
   105
                        } catch (IOException ioex) {
jaroslav@243
   106
                            ioex.printStackTrace();
jaroslav@243
   107
                        }
jaroslav@243
   108
                        return null;
jaroslav@243
   109
                    }
jaroslav@243
   110
                });
jaroslav@243
   111
            }
jaroslav@243
   112
        });
jaroslav@243
   113
    }
jaroslav@243
   114
jaroslav@243
   115
    @Override
jaroslav@243
   116
    public void run() {
jaroslav@243
   117
        try {
jaroslav@243
   118
            while (true) {
jaroslav@243
   119
                int length = input.readInt();
jaroslav@243
   120
                byte[] bytes = new byte[length];
jaroslav@243
   121
                input.readFully(bytes);
jaroslav@243
   122
                final String message = new String(bytes, StandardCharsets.UTF_8);
jaroslav@243
   123
                Platform.runLater(new Runnable() {
jaroslav@243
   124
                    @Override
jaroslav@243
   125
                    public void run() {
jaroslav@316
   126
                        dbg.sendMessage(message);
jaroslav@243
   127
                    }
jaroslav@243
   128
                });
jaroslav@243
   129
            }
jaroslav@319
   130
        } catch (IOException ex) {
jaroslav@316
   131
            LOG.log(Level.WARNING, null, ex);
jaroslav@243
   132
        }
jaroslav@243
   133
    }
jaroslav@243
   134
}