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