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
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher;
    19 
    20 import java.io.Closeable;
    21 import java.io.File;
    22 import java.io.IOException;
    23 import java.lang.reflect.Method;
    24 import java.net.URLClassLoader;
    25 import org.apidesign.vm4brwsr.Bck2Brwsr;
    26 
    27 /** An abstraction for executing tests in a Bck2Brwsr virtual machine.
    28  * Either in {@linkm Launcher#createJavaScript JavaScript engine}, 
    29  * or in {@linkm Launcher#createBrowser external browser}.
    30  * <p>
    31  * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages} 
    32  * in an external browser served by internal HTTP server.
    33  *
    34  * @author Jaroslav Tulach <jtulach@netbeans.org>
    35  */
    36 public abstract class Launcher {
    37 
    38     Launcher() {
    39     }
    40 
    41     /** Initializes the launcher. This may mean starting a web browser or
    42      * initializing execution engine.
    43      * @throws IOException if something goes wrong
    44      */
    45     public abstract void initialize() throws IOException;
    46     
    47     /** Shuts down the launcher.
    48      * @throws IOException if something goes wrong
    49      */
    50     public abstract void shutdown() throws IOException;
    51     
    52     
    53     /** Builds an invocation context. The context can later be customized
    54      * and {@link InvocationContext#invoke() invoked}.
    55      * 
    56      * @param clazz the class to execute method from
    57      * @param method the method to execute
    58      * @return the context pointing to the selected method
    59      */
    60     public InvocationContext createInvocation(Class<?> clazz, String method) {
    61         return new InvocationContext(this, clazz, method);
    62     }
    63     
    64 
    65     /** Creates launcher that uses internal JavaScript engine (Rhino).
    66      * @return the launcher
    67      */
    68     public static Launcher createJavaScript() {
    69         final JSLauncher l = new JSLauncher();
    70         l.addClassLoader(Bck2Brwsr.class.getClassLoader());
    71         return l;
    72     }
    73     
    74     /** Creates launcher that is using external browser. Value of <code>cmd</code>
    75      * may be <code>"fx"</code> to indicate one wants to use JavaFX's WebView
    76      * instead of command line browser.
    77      * 
    78      * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
    79      *    or a string to execute in an external process (with a parameter to the URL)
    80      * @return launcher executing in external browser.
    81      */
    82     public static Launcher createBrowser(String cmd) {
    83         final Bck2BrwsrLauncher l;
    84         if ("fx".equals(cmd)) {
    85             l = new WebViewLauncher();
    86         } else {
    87             l = new Bck2BrwsrLauncher(cmd);
    88         }
    89         l.addClassLoader(Bck2Brwsr.class.getClassLoader());
    90         l.setTimeout(180000);
    91         return l;
    92     }
    93     
    94     /** Starts an HTTP server which provides access to classes and resources
    95      * available in the <code>classes</code> URL and shows a start page
    96      * available as {@link ClassLoader#getResource(java.lang.String)} from the
    97      * provide classloader. Opens a browser with URL showing the start page.
    98      * 
    99      * @param classes classloader offering access to classes and resources
   100      * @param startpage page to show in the browser
   101      * @return interface that allows one to stop the server
   102      * @throws IOException if something goes wrong
   103      */
   104     public static Closeable showURL(URLClassLoader classes, String startpage) throws IOException {
   105         Bck2BrwsrLauncher l = new WebViewLauncher();
   106         l.addClassLoader(classes);
   107         l.showURL(startpage);
   108         return l;
   109     }
   110     /** Starts an HTTP server which provides access to certain directory.
   111      * The <code>startpage</code> should be relative location inside the root 
   112      * directory. Opens a browser with URL showing the start page.
   113      * 
   114      * @param directory the root directory on disk
   115      * @param startpage relative path from the root to the page
   116      * @exception IOException if something goes wrong.
   117      */
   118     public static Closeable showDir(File directory, String startpage) throws IOException {
   119         Bck2BrwsrLauncher l = new WebViewLauncher();
   120         l.showDirectory(directory, startpage);
   121         return l;
   122     }
   123 
   124     abstract InvocationContext runMethod(InvocationContext c) throws IOException; 
   125 }