launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 17:42:49 +0200
branchmodel
changeset 1041 f18b7262fe91
parent 1033 b8773b7b9ecd
child 1043 bd80952bfd11
permissions -rw-r--r--
FX launcher can start tests
jaroslav@1033
     1
/**
jaroslav@1033
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1033
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1033
     4
 *
jaroslav@1033
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1033
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1033
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1033
     8
 *
jaroslav@1033
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1033
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1033
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1033
    12
 * GNU General Public License for more details.
jaroslav@1033
    13
 *
jaroslav@1033
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1033
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1033
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1033
    17
 */
jaroslav@1033
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@1033
    19
jaroslav@1033
    20
import java.io.Closeable;
jaroslav@1033
    21
import java.io.File;
jaroslav@1033
    22
import java.io.IOException;
jaroslav@1033
    23
import java.lang.reflect.Constructor;
jaroslav@1033
    24
jaroslav@1033
    25
/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
jaroslav@1033
    26
 * Either in {@link Launcher#createJavaScript JavaScript engine}, 
jaroslav@1033
    27
 * or in {@link Launcher#createBrowser external browser}.
jaroslav@1033
    28
 * <p>
jaroslav@1033
    29
 * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages} 
jaroslav@1033
    30
 * in an external browser served by internal HTTP server.
jaroslav@1033
    31
 *
jaroslav@1033
    32
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1033
    33
 */
jaroslav@1033
    34
public abstract class Launcher {
jaroslav@1033
    35
jaroslav@1033
    36
    Launcher() {
jaroslav@1033
    37
    }
jaroslav@1033
    38
jaroslav@1033
    39
    /** Initializes the launcher. This may mean starting a web browser or
jaroslav@1033
    40
     * initializing execution engine.
jaroslav@1033
    41
     * @throws IOException if something goes wrong
jaroslav@1033
    42
     */
jaroslav@1033
    43
    public abstract void initialize() throws IOException;
jaroslav@1033
    44
    
jaroslav@1033
    45
    /** Shuts down the launcher.
jaroslav@1033
    46
     * @throws IOException if something goes wrong
jaroslav@1033
    47
     */
jaroslav@1033
    48
    public abstract void shutdown() throws IOException;
jaroslav@1033
    49
    
jaroslav@1033
    50
    
jaroslav@1033
    51
    /** Builds an invocation context. The context can later be customized
jaroslav@1033
    52
     * and {@link InvocationContext#invoke() invoked}.
jaroslav@1033
    53
     * 
jaroslav@1033
    54
     * @param clazz the class to execute method from
jaroslav@1033
    55
     * @param method the method to execute
jaroslav@1033
    56
     * @return the context pointing to the selected method
jaroslav@1033
    57
     */
jaroslav@1033
    58
    public InvocationContext createInvocation(Class<?> clazz, String method) {
jaroslav@1033
    59
        return new InvocationContext(this, clazz, method);
jaroslav@1033
    60
    }
jaroslav@1033
    61
    
jaroslav@1033
    62
jaroslav@1033
    63
    /** Creates launcher that uses internal JavaScript engine (Rhino).
jaroslav@1033
    64
     * @return the launcher
jaroslav@1033
    65
     */
jaroslav@1033
    66
    public static Launcher createJavaScript() {
jaroslav@1033
    67
        try {
jaroslav@1033
    68
            Class<?> c = loadClass("org.apidesign.bck2brwsr.launcher.JSLauncher");
jaroslav@1033
    69
            return (Launcher) c.newInstance();
jaroslav@1033
    70
        } catch (Exception ex) {
jaroslav@1033
    71
            throw new IllegalStateException("Please include org.apidesign.bck2brwsr:launcher.html dependency!", ex);
jaroslav@1033
    72
        }
jaroslav@1033
    73
    }
jaroslav@1033
    74
    
jaroslav@1033
    75
    /** Creates launcher that is using external browser.
jaroslav@1033
    76
     * 
jaroslav@1033
    77
     * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
jaroslav@1033
    78
     *    or a string to execute in an external process (with a parameter to the URL)
jaroslav@1033
    79
     * @return launcher executing in external browser.
jaroslav@1033
    80
     */
jaroslav@1033
    81
    public static Launcher createBrowser(String cmd) {
jaroslav@1041
    82
        String msg = "Trying to create browser '" + cmd + "'";
jaroslav@1033
    83
        try {
jaroslav@1041
    84
            Class<?> c;
jaroslav@1041
    85
            if ("fx".equals(cmd)) { // NOI18N
jaroslav@1041
    86
                msg = "Please include org.apidesign.bck2brwsr:launcher.fx dependency!";
jaroslav@1041
    87
                c = loadClass("org.apidesign.bck2brwsr.launcher.FXBrwsrLauncher"); // NOI18N
jaroslav@1041
    88
            } else {
jaroslav@1041
    89
                msg = "Please include org.apidesign.bck2brwsr:launcher.html dependency!";
jaroslav@1041
    90
                c = loadClass("org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher"); // NOI18N
jaroslav@1041
    91
            }
jaroslav@1033
    92
            Constructor<?> cnstr = c.getConstructor(String.class);
jaroslav@1033
    93
            return (Launcher) cnstr.newInstance(cmd);
jaroslav@1033
    94
        } catch (Exception ex) {
jaroslav@1041
    95
            throw new IllegalStateException(msg, ex);
jaroslav@1033
    96
        }
jaroslav@1033
    97
    }
jaroslav@1033
    98
    
jaroslav@1033
    99
    /** Starts an HTTP server which provides access to classes and resources
jaroslav@1033
   100
     * available in the <code>classes</code> URL and shows a start page
jaroslav@1033
   101
     * available as {@link ClassLoader#getResource(java.lang.String)} from the
jaroslav@1033
   102
     * provide classloader. Opens a browser with URL showing the start page.
jaroslav@1033
   103
     * 
jaroslav@1033
   104
     * @param classes classloader offering access to classes and resources
jaroslav@1033
   105
     * @param startpage page to show in the browser
jaroslav@1033
   106
     * @return interface that allows one to stop the server
jaroslav@1033
   107
     * @throws IOException if something goes wrong
jaroslav@1033
   108
     */
jaroslav@1033
   109
    public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
jaroslav@1033
   110
        Launcher l = createBrowser(null);
jaroslav@1033
   111
        l.addClassLoader(classes);
jaroslav@1033
   112
        l.showURL(startpage);
jaroslav@1033
   113
        return (Closeable) l;
jaroslav@1033
   114
    }
jaroslav@1033
   115
    /** Starts an HTTP server which provides access to certain directory.
jaroslav@1033
   116
     * The <code>startpage</code> should be relative location inside the root 
jaroslav@1033
   117
     * directory. Opens a browser with URL showing the start page.
jaroslav@1033
   118
     * 
jaroslav@1033
   119
     * @param directory the root directory on disk
jaroslav@1033
   120
     * @param startpage relative path from the root to the page
jaroslav@1033
   121
     * @exception IOException if something goes wrong.
jaroslav@1033
   122
     */
jaroslav@1033
   123
    public static Closeable showDir(File directory, String startpage) throws IOException {
jaroslav@1033
   124
        Launcher l = createBrowser(null);
jaroslav@1033
   125
        l.showDirectory(directory, startpage);
jaroslav@1033
   126
        return (Closeable) l;
jaroslav@1033
   127
    }
jaroslav@1033
   128
jaroslav@1033
   129
    abstract InvocationContext runMethod(InvocationContext c) throws IOException; 
jaroslav@1033
   130
jaroslav@1033
   131
jaroslav@1033
   132
    private static Class<?> loadClass(String cn) throws ClassNotFoundException {
jaroslav@1033
   133
        return Launcher.class.getClassLoader().loadClass(cn);
jaroslav@1033
   134
    }
jaroslav@1033
   135
jaroslav@1033
   136
    void showDirectory(File directory, String startpage) throws IOException {
jaroslav@1033
   137
        throw new UnsupportedOperationException();
jaroslav@1033
   138
    }
jaroslav@1033
   139
jaroslav@1033
   140
    void showURL(String startpage) throws IOException {
jaroslav@1033
   141
        throw new UnsupportedOperationException();
jaroslav@1033
   142
    }
jaroslav@1033
   143
jaroslav@1033
   144
    void addClassLoader(ClassLoader classes) {
jaroslav@1033
   145
        throw new UnsupportedOperationException();
jaroslav@1033
   146
    }
jaroslav@1033
   147
}