launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/WebDebug.java
changeset 1169 c19ac78b940e
child 1173 aa82f9de8e33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/WebDebug.java	Sat Jun 08 12:09:10 2013 +0200
     1.3 @@ -0,0 +1,187 @@
     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 = "/home/jarda/src/bck2brwsr/html~demo/twitter/src/main/resources/org/apidesign/html/demo/twitter/index.html";
    1.73 +            File f = new File(p);
    1.74 +            String[] args = {"--livehtml", f.getAbsolutePath()};
    1.75 +            File dir = f.getParentFile();
    1.76 +            cliHandler(args, toDbg, this, System.err, dir);
    1.77 +        } catch (Exception ex) {
    1.78 +            LOG.log(Level.SEVERE, null, ex);
    1.79 +        }
    1.80 +    }
    1.81 +
    1.82 +    @Override
    1.83 +    public void close() {
    1.84 +        try {
    1.85 +            toDbg.close();
    1.86 +        } catch (IOException ex) {
    1.87 +            LOG.log(Level.SEVERE, null, ex);
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    @Override
    1.92 +    public Void call(String p) {
    1.93 +        assert p.indexOf('\n') == -1 : "No new line: " + p;
    1.94 +        LOG.log(Level.INFO, "toDbgr: {0}", p);
    1.95 +        toDbg.pushMsg(p);
    1.96 +        return null;
    1.97 +    }
    1.98 +
    1.99 +    @Override
   1.100 +    public void write(int b) throws IOException {
   1.101 +        if (b == '\n') {
   1.102 +            final String msg = cmd.toString();
   1.103 +            Platform.runLater(new Runnable() {
   1.104 +                @Override
   1.105 +                public void run() {
   1.106 +                    LOG.log(Level.INFO, "toView: {0}", msg);
   1.107 +                    debug.sendMessage(msg);
   1.108 +                }
   1.109 +            });
   1.110 +            cmd.setLength(0);
   1.111 +        } else {
   1.112 +            if (cmd.length() > 100000) {
   1.113 +                LOG.log(Level.WARNING, "Too big:\n{0}", cmd);
   1.114 +            }
   1.115 +            cmd.append((char)b);
   1.116 +        }
   1.117 +    }   
   1.118 +
   1.119 +    private void cliHandler(
   1.120 +        String[] args, InputStream is, OutputStream os, OutputStream err, File dir
   1.121 +    ) {
   1.122 +        try {
   1.123 +            Class<?> main = Class.forName("org.netbeans.MainImpl");
   1.124 +            Method m = main.getDeclaredMethod("execute", String[].class, InputStream.class, 
   1.125 +                OutputStream.class, OutputStream.class, AtomicReference.class
   1.126 +            );
   1.127 +            m.setAccessible(true);
   1.128 +            System.setProperty("netbeans.user", ud);
   1.129 +            int ret = (Integer)m.invoke(null, args, is, os, err, null);
   1.130 +            LOG.log(Level.INFO, "Return value: {0}", ret);
   1.131 +        } catch (Exception ex) {
   1.132 +            LOG.log(Level.SEVERE, null, ex);
   1.133 +        } finally {
   1.134 +            LOG.info("Communication is over");
   1.135 +        }
   1.136 +        
   1.137 +    }
   1.138 +
   1.139 +    private class ToDbgInputStream extends InputStream {
   1.140 +        private byte[] current;
   1.141 +        private int currentPos;
   1.142 +        private final ArrayBlockingQueue<byte[]> pending = new ArrayBlockingQueue<byte[]>(64);
   1.143 +
   1.144 +        public ToDbgInputStream() {
   1.145 +        }
   1.146 +
   1.147 +        @Override
   1.148 +        public int read() throws IOException {
   1.149 +            return read(null, 0, 1);
   1.150 +        }
   1.151 +        
   1.152 +        @Override
   1.153 +        public int read(byte[] arr, int offset, int len) throws IOException {
   1.154 +            if (current == null || current.length <= currentPos) {
   1.155 +                for (;;) {
   1.156 +                    WebDebug.this.flush();
   1.157 +                    try {
   1.158 +                        current = pending.poll(100, TimeUnit.MILLISECONDS);
   1.159 +                        if (current == null) {
   1.160 +                            return 0;
   1.161 +                        }
   1.162 +                        break;
   1.163 +                    } catch (InterruptedException ex) {
   1.164 +                        throw (InterruptedIOException)new InterruptedIOException().initCause(ex);
   1.165 +                    }
   1.166 +                }
   1.167 +                LOG.info("Will return: " + new String(current));
   1.168 +                currentPos = 0;
   1.169 +            }
   1.170 +            int cnt = 0;
   1.171 +            while (len-- > 0 && currentPos < current.length) {
   1.172 +                final byte nextByte = current[currentPos++];
   1.173 +                if (arr == null) {
   1.174 +                    return nextByte;
   1.175 +                }
   1.176 +                arr[offset + cnt++] = nextByte;
   1.177 +            }
   1.178 +            LOG.log(Level.INFO, "read returns: {0}", new String(arr, offset, cnt));
   1.179 +            return cnt;
   1.180 +        }
   1.181 +
   1.182 +        private void pushMsg(String p) {
   1.183 +            try {
   1.184 +                pending.offer((p + '\n').getBytes("UTF-8"));
   1.185 +            } catch (UnsupportedEncodingException ex) {
   1.186 +                throw new IllegalStateException(ex);
   1.187 +            }
   1.188 +        }
   1.189 +    }
   1.190 +}