rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 14 Mar 2013 09:22:28 +0100
branchfx
changeset 845 859804c78010
parent 844 023cda5b8b0b
child 859 d39fbe061519
permissions -rw-r--r--
Hacky way (relies on singleton) to get FX working. Can attach @On(event=CLICK) and show alert in FX WebView now
jaroslav@382
     1
/**
jaroslav@382
     2
 * Back 2 Browser Bytecode Translator
jaroslav@382
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@382
     4
 *
jaroslav@382
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@382
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@382
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@382
     8
 *
jaroslav@382
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@382
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@382
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@382
    12
 * GNU General Public License for more details.
jaroslav@382
    13
 *
jaroslav@382
    14
 * You should have received a copy of the GNU General Public License
jaroslav@382
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@382
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@382
    17
 */
jaroslav@382
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@382
    19
jaroslav@382
    20
import java.io.Closeable;
jaroslav@613
    21
import java.io.File;
jaroslav@382
    22
import java.io.IOException;
jaroslav@844
    23
import java.lang.reflect.Method;
jaroslav@382
    24
import java.net.URLClassLoader;
jaroslav@382
    25
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@382
    26
jaroslav@382
    27
/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
jaroslav@622
    28
 * Either in {@linkm Launcher#createJavaScript JavaScript engine}, 
jaroslav@622
    29
 * or in {@linkm Launcher#createBrowser external browser}.
jaroslav@794
    30
 * <p>
jaroslav@794
    31
 * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages} 
jaroslav@794
    32
 * in an external browser served by internal HTTP server.
jaroslav@382
    33
 *
jaroslav@382
    34
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@382
    35
 */
jaroslav@382
    36
public abstract class Launcher {
jaroslav@382
    37
jaroslav@382
    38
    Launcher() {
jaroslav@382
    39
    }
jaroslav@622
    40
jaroslav@622
    41
    /** Initializes the launcher. This may mean starting a web browser or
jaroslav@622
    42
     * initializing execution engine.
jaroslav@622
    43
     * @throws IOException if something goes wrong
jaroslav@622
    44
     */
jaroslav@622
    45
    public abstract void initialize() throws IOException;
jaroslav@382
    46
    
jaroslav@622
    47
    /** Shuts down the launcher.
jaroslav@622
    48
     * @throws IOException if something goes wrong
jaroslav@622
    49
     */
jaroslav@382
    50
    public abstract void shutdown() throws IOException;
jaroslav@622
    51
    
jaroslav@622
    52
    
jaroslav@622
    53
    /** Builds an invocation context. The context can later be customized
jaroslav@622
    54
     * and {@link InvocationContext#invoke() invoked}.
jaroslav@622
    55
     * 
jaroslav@622
    56
     * @param clazz the class to execute method from
jaroslav@622
    57
     * @param method the method to execute
jaroslav@622
    58
     * @return the context pointing to the selected method
jaroslav@622
    59
     */
jaroslav@622
    60
    public InvocationContext createInvocation(Class<?> clazz, String method) {
jaroslav@622
    61
        return new InvocationContext(this, clazz, method);
jaroslav@382
    62
    }
jaroslav@382
    63
    
jaroslav@382
    64
jaroslav@622
    65
    /** Creates launcher that uses internal JavaScript engine (Rhino).
jaroslav@622
    66
     * @return the launcher
jaroslav@622
    67
     */
jaroslav@382
    68
    public static Launcher createJavaScript() {
jaroslav@382
    69
        final JSLauncher l = new JSLauncher();
jaroslav@382
    70
        l.addClassLoader(Bck2Brwsr.class.getClassLoader());
jaroslav@382
    71
        return l;
jaroslav@382
    72
    }
jaroslav@382
    73
    
jaroslav@844
    74
    /** Creates launcher that is using external browser. Value of <code>cmd</code>
jaroslav@844
    75
     * may be <code>"fx"</code> to indicate one wants to use JavaFX's WebView
jaroslav@844
    76
     * instead of command line browser.
jaroslav@622
    77
     * 
jaroslav@622
    78
     * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
jaroslav@622
    79
     *    or a string to execute in an external process (with a parameter to the URL)
jaroslav@622
    80
     * @return launcher executing in external browser.
jaroslav@622
    81
     */
jaroslav@382
    82
    public static Launcher createBrowser(String cmd) {
jaroslav@844
    83
        final Bck2BrwsrLauncher l;
jaroslav@844
    84
        if ("fx".equals(cmd)) {
jaroslav@844
    85
            l = new WebViewLauncher();
jaroslav@844
    86
        } else {
jaroslav@844
    87
            l = new Bck2BrwsrLauncher(cmd);
jaroslav@844
    88
        }
jaroslav@382
    89
        l.addClassLoader(Bck2Brwsr.class.getClassLoader());
jaroslav@382
    90
        l.setTimeout(180000);
jaroslav@382
    91
        return l;
jaroslav@382
    92
    }
jaroslav@622
    93
    
jaroslav@622
    94
    /** Starts an HTTP server which provides access to classes and resources
jaroslav@622
    95
     * available in the <code>classes</code> URL and shows a start page
jaroslav@622
    96
     * available as {@link ClassLoader#getResource(java.lang.String)} from the
jaroslav@622
    97
     * provide classloader. Opens a browser with URL showing the start page.
jaroslav@622
    98
     * 
jaroslav@622
    99
     * @param classes classloader offering access to classes and resources
jaroslav@622
   100
     * @param startpage page to show in the browser
jaroslav@622
   101
     * @return interface that allows one to stop the server
jaroslav@622
   102
     * @throws IOException if something goes wrong
jaroslav@622
   103
     */
jaroslav@382
   104
    public static Closeable showURL(URLClassLoader classes, String startpage) throws IOException {
jaroslav@845
   105
        Bck2BrwsrLauncher l = new WebViewLauncher();
jaroslav@382
   106
        l.addClassLoader(classes);
jaroslav@382
   107
        l.showURL(startpage);
jaroslav@382
   108
        return l;
jaroslav@382
   109
    }
jaroslav@622
   110
    /** Starts an HTTP server which provides access to certain directory.
jaroslav@622
   111
     * The <code>startpage</code> should be relative location inside the root 
jaroslav@794
   112
     * directory. Opens a browser with URL showing the start page.
jaroslav@622
   113
     * 
jaroslav@622
   114
     * @param directory the root directory on disk
jaroslav@794
   115
     * @param startpage relative path from the root to the page
jaroslav@622
   116
     * @exception IOException if something goes wrong.
jaroslav@622
   117
     */
jaroslav@613
   118
    public static Closeable showDir(File directory, String startpage) throws IOException {
jaroslav@845
   119
        Bck2BrwsrLauncher l = new WebViewLauncher();
jaroslav@613
   120
        l.showDirectory(directory, startpage);
jaroslav@613
   121
        return l;
jaroslav@613
   122
    }
jaroslav@613
   123
jaroslav@622
   124
    abstract InvocationContext runMethod(InvocationContext c) throws IOException; 
jaroslav@382
   125
}