rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 622 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java@a07253cf2ca4
child 794 b4284da2fb3b
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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.net.URLClassLoader;
    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  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public abstract class Launcher {
    33 
    34     Launcher() {
    35     }
    36 
    37     /** Initializes the launcher. This may mean starting a web browser or
    38      * initializing execution engine.
    39      * @throws IOException if something goes wrong
    40      */
    41     public abstract void initialize() throws IOException;
    42     
    43     /** Shuts down the launcher.
    44      * @throws IOException if something goes wrong
    45      */
    46     public abstract void shutdown() throws IOException;
    47     
    48     
    49     /** Builds an invocation context. The context can later be customized
    50      * and {@link InvocationContext#invoke() invoked}.
    51      * 
    52      * @param clazz the class to execute method from
    53      * @param method the method to execute
    54      * @return the context pointing to the selected method
    55      */
    56     public InvocationContext createInvocation(Class<?> clazz, String method) {
    57         return new InvocationContext(this, clazz, method);
    58     }
    59     
    60 
    61     /** Creates launcher that uses internal JavaScript engine (Rhino).
    62      * @return the launcher
    63      */
    64     public static Launcher createJavaScript() {
    65         final JSLauncher l = new JSLauncher();
    66         l.addClassLoader(Bck2Brwsr.class.getClassLoader());
    67         return l;
    68     }
    69     
    70     /** Creates launcher that is using external browser.
    71      * 
    72      * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
    73      *    or a string to execute in an external process (with a parameter to the URL)
    74      * @return launcher executing in external browser.
    75      */
    76     public static Launcher createBrowser(String cmd) {
    77         final Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(cmd);
    78         l.addClassLoader(Bck2Brwsr.class.getClassLoader());
    79         l.setTimeout(180000);
    80         return l;
    81     }
    82     
    83     /** Starts an HTTP server which provides access to classes and resources
    84      * available in the <code>classes</code> URL and shows a start page
    85      * available as {@link ClassLoader#getResource(java.lang.String)} from the
    86      * provide classloader. Opens a browser with URL showing the start page.
    87      * 
    88      * @param classes classloader offering access to classes and resources
    89      * @param startpage page to show in the browser
    90      * @return interface that allows one to stop the server
    91      * @throws IOException if something goes wrong
    92      */
    93     public static Closeable showURL(URLClassLoader classes, String startpage) throws IOException {
    94         Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
    95         l.addClassLoader(classes);
    96         l.showURL(startpage);
    97         return l;
    98     }
    99     /** Starts an HTTP server which provides access to certain directory.
   100      * The <code>startpage</code> should be relative location inside the root 
   101      * driecotry
   102      * Opens a browser with URL showing the start page.
   103      * 
   104      * @param directory the root directory on disk
   105      * @praam startpage relative path from the root to the page
   106      * @exception IOException if something goes wrong.
   107      */
   108     public static Closeable showDir(File directory, String startpage) throws IOException {
   109         Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
   110         l.showDirectory(directory, startpage);
   111         return l;
   112     }
   113 
   114     abstract InvocationContext runMethod(InvocationContext c) throws IOException; 
   115 }