2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.launcher.fximpl;
20 import com.sun.javafx.scene.web.Debugger;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InterruptedIOException;
25 import java.io.OutputStream;
26 import java.io.UnsupportedEncodingException;
27 import java.lang.reflect.Method;
28 import java.util.concurrent.ArrayBlockingQueue;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicReference;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import javafx.application.Platform;
35 import javafx.util.Callback;
37 /** Simulates WebKit protocol over WebSockets.
39 * @author Jaroslav Tulach
41 abstract class WebDebug extends OutputStream
42 implements Callback<String, Void>, Runnable {
43 private static final Logger LOG = Logger.getLogger(WebDebug.class.getName());
45 private final Debugger debug;
46 private final StringBuilder cmd = new StringBuilder();
47 private final ToDbgInputStream toDbg;
48 private final String ud;
50 WebDebug(Debugger debug, String ud) throws Exception {
53 toDbg = new ToDbgInputStream();
54 debug.setEnabled(true);
55 debug.setMessageCallback(this);
58 static WebDebug create(Debugger debug, String ud) throws Exception {
59 WebDebug web = new WebDebug(debug, ud) {};
61 Executors.newFixedThreadPool(1).execute(web);
69 String p = System.getProperty("startpage.file");
71 if (p != null && (f = new File(p)).exists()) {
72 String[] args = {"--livehtml", f.getAbsolutePath()};
73 File dir = f.getParentFile();
74 cliHandler(args, toDbg, this, System.err, dir);
76 } catch (Exception ex) {
77 LOG.log(Level.SEVERE, null, ex);
85 } catch (IOException ex) {
86 LOG.log(Level.SEVERE, null, ex);
91 public Void call(String p) {
92 assert p.indexOf('\n') == -1 : "No new line: " + p;
93 LOG.log(Level.INFO, "toDbgr: {0}", p);
99 public void write(int b) throws IOException {
101 final String msg = cmd.toString();
102 Platform.runLater(new Runnable() {
105 LOG.log(Level.INFO, "toView: {0}", msg);
106 debug.sendMessage(msg);
111 if (cmd.length() > 100000) {
112 LOG.log(Level.WARNING, "Too big:\n{0}", cmd);
118 private void cliHandler(
119 String[] args, InputStream is, OutputStream os, OutputStream err, File dir
122 Class<?> main = Class.forName("org.netbeans.MainImpl");
123 Method m = main.getDeclaredMethod("execute", String[].class, InputStream.class,
124 OutputStream.class, OutputStream.class, AtomicReference.class
126 m.setAccessible(true);
127 System.setProperty("netbeans.user", ud);
128 int ret = (Integer)m.invoke(null, args, is, os, err, null);
129 LOG.log(Level.INFO, "Return value: {0}", ret);
130 } catch (Exception ex) {
131 LOG.log(Level.SEVERE, null, ex);
133 LOG.info("Communication is over");
138 private class ToDbgInputStream extends InputStream {
139 private byte[] current;
140 private int currentPos;
141 private final ArrayBlockingQueue<byte[]> pending = new ArrayBlockingQueue<byte[]>(64);
143 public ToDbgInputStream() {
147 public int read() throws IOException {
148 return read(null, 0, 1);
152 public int read(byte[] arr, int offset, int len) throws IOException {
153 if (current == null || current.length <= currentPos) {
155 WebDebug.this.flush();
157 current = pending.poll(5, TimeUnit.MILLISECONDS);
158 if (current == null) {
162 } catch (InterruptedException ex) {
163 throw (InterruptedIOException)new InterruptedIOException().initCause(ex);
166 LOG.info("Will return: " + new String(current));
170 while (len-- > 0 && currentPos < current.length) {
171 final byte nextByte = current[currentPos++];
175 arr[offset + cnt++] = nextByte;
177 LOG.log(Level.INFO, "read returns: {0}", new String(arr, offset, cnt));
181 private void pushMsg(String p) {
183 pending.offer((p + '\n').getBytes("UTF-8"));
184 } catch (UnsupportedEncodingException ex) {
185 throw new IllegalStateException(ex);