1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/WebDebug.java Thu Sep 12 14:15:47 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,189 +0,0 @@
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.File;
1.25 -import java.io.IOException;
1.26 -import java.io.InputStream;
1.27 -import java.io.InterruptedIOException;
1.28 -import java.io.OutputStream;
1.29 -import java.io.UnsupportedEncodingException;
1.30 -import java.lang.reflect.Method;
1.31 -import java.util.concurrent.ArrayBlockingQueue;
1.32 -import java.util.concurrent.Executors;
1.33 -import java.util.concurrent.TimeUnit;
1.34 -import java.util.concurrent.atomic.AtomicReference;
1.35 -import java.util.logging.Level;
1.36 -import java.util.logging.Logger;
1.37 -import javafx.application.Platform;
1.38 -import javafx.util.Callback;
1.39 -
1.40 -/** Simulates WebKit protocol over WebSockets.
1.41 - *
1.42 - * @author Jaroslav Tulach
1.43 - */
1.44 -abstract class WebDebug extends OutputStream
1.45 -implements Callback<String, Void>, Runnable {
1.46 - private static final Logger LOG = Logger.getLogger(WebDebug.class.getName());
1.47 -
1.48 - private final Debugger debug;
1.49 - private final StringBuilder cmd = new StringBuilder();
1.50 - private final ToDbgInputStream toDbg;
1.51 - private final String ud;
1.52 -
1.53 - WebDebug(Debugger debug, String ud) throws Exception {
1.54 - this.debug = debug;
1.55 - this.ud = ud;
1.56 - toDbg = new ToDbgInputStream();
1.57 - debug.setEnabled(true);
1.58 - debug.setMessageCallback(this);
1.59 - }
1.60 -
1.61 - static WebDebug create(Debugger debug, String ud) throws Exception {
1.62 - WebDebug web = new WebDebug(debug, ud) {};
1.63 -
1.64 - Executors.newFixedThreadPool(1).execute(web);
1.65 -
1.66 - return web;
1.67 - }
1.68 -
1.69 - @Override
1.70 - public void run() {
1.71 - try {
1.72 - String p = System.getProperty("startpage.file");
1.73 - File f;
1.74 - if (p != null && (f = new File(p)).exists()) {
1.75 - String[] args = {"--livehtml", f.getAbsolutePath()};
1.76 - File dir = f.getParentFile();
1.77 - cliHandler(args, toDbg, this, System.err, dir);
1.78 - }
1.79 - } catch (Exception ex) {
1.80 - LOG.log(Level.SEVERE, null, ex);
1.81 - }
1.82 - }
1.83 -
1.84 - @Override
1.85 - public void close() {
1.86 - try {
1.87 - toDbg.close();
1.88 - } catch (IOException ex) {
1.89 - LOG.log(Level.SEVERE, null, ex);
1.90 - }
1.91 - }
1.92 -
1.93 - @Override
1.94 - public Void call(String p) {
1.95 - assert p.indexOf('\n') == -1 : "No new line: " + p;
1.96 - LOG.log(Level.INFO, "toDbgr: {0}", p);
1.97 - toDbg.pushMsg(p);
1.98 - return null;
1.99 - }
1.100 -
1.101 - @Override
1.102 - public void write(int b) throws IOException {
1.103 - if (b == '\n') {
1.104 - final String msg = cmd.toString();
1.105 - Platform.runLater(new Runnable() {
1.106 - @Override
1.107 - public void run() {
1.108 - LOG.log(Level.INFO, "toView: {0}", msg);
1.109 - debug.sendMessage(msg);
1.110 - }
1.111 - });
1.112 - cmd.setLength(0);
1.113 - } else {
1.114 - if (cmd.length() > 100000) {
1.115 - LOG.log(Level.WARNING, "Too big:\n{0}", cmd);
1.116 - }
1.117 - cmd.append((char)b);
1.118 - }
1.119 - }
1.120 -
1.121 - private void cliHandler(
1.122 - String[] args, InputStream is, OutputStream os, OutputStream err, File dir
1.123 - ) {
1.124 - try {
1.125 - Class<?> main = Class.forName("org.netbeans.MainImpl");
1.126 - Method m = main.getDeclaredMethod("execute", String[].class, InputStream.class,
1.127 - OutputStream.class, OutputStream.class, AtomicReference.class
1.128 - );
1.129 - m.setAccessible(true);
1.130 - System.setProperty("netbeans.user", ud);
1.131 - int ret = (Integer)m.invoke(null, args, is, os, err, null);
1.132 - LOG.log(Level.INFO, "Return value: {0}", ret);
1.133 - } catch (Exception ex) {
1.134 - LOG.log(Level.SEVERE, null, ex);
1.135 - } finally {
1.136 - LOG.info("Communication is over");
1.137 - }
1.138 -
1.139 - }
1.140 -
1.141 - private class ToDbgInputStream extends InputStream {
1.142 - private byte[] current;
1.143 - private int currentPos;
1.144 - private final ArrayBlockingQueue<byte[]> pending = new ArrayBlockingQueue<byte[]>(64);
1.145 -
1.146 - public ToDbgInputStream() {
1.147 - }
1.148 -
1.149 - @Override
1.150 - public int read() throws IOException {
1.151 - return read(null, 0, 1);
1.152 - }
1.153 -
1.154 - @Override
1.155 - public int read(byte[] arr, int offset, int len) throws IOException {
1.156 - if (current == null || current.length <= currentPos) {
1.157 - for (;;) {
1.158 - WebDebug.this.flush();
1.159 - try {
1.160 - current = pending.poll(5, TimeUnit.MILLISECONDS);
1.161 - if (current == null) {
1.162 - return 0;
1.163 - }
1.164 - break;
1.165 - } catch (InterruptedException ex) {
1.166 - throw (InterruptedIOException)new InterruptedIOException().initCause(ex);
1.167 - }
1.168 - }
1.169 - LOG.info("Will return: " + new String(current));
1.170 - currentPos = 0;
1.171 - }
1.172 - int cnt = 0;
1.173 - while (len-- > 0 && currentPos < current.length) {
1.174 - final byte nextByte = current[currentPos++];
1.175 - if (arr == null) {
1.176 - return nextByte;
1.177 - }
1.178 - arr[offset + cnt++] = nextByte;
1.179 - }
1.180 - LOG.log(Level.INFO, "read returns: {0}", new String(arr, offset, cnt));
1.181 - return cnt;
1.182 - }
1.183 -
1.184 - private void pushMsg(String p) {
1.185 - try {
1.186 - pending.offer((p + '\n').getBytes("UTF-8"));
1.187 - } catch (UnsupportedEncodingException ex) {
1.188 - throw new IllegalStateException(ex);
1.189 - }
1.190 - }
1.191 - }
1.192 -}