jaroslav@382: /** jaroslav@382: * Back 2 Browser Bytecode Translator jaroslav@382: * Copyright (C) 2012 Jaroslav Tulach jaroslav@382: * jaroslav@382: * This program is free software: you can redistribute it and/or modify jaroslav@382: * it under the terms of the GNU General Public License as published by jaroslav@382: * the Free Software Foundation, version 2 of the License. jaroslav@382: * jaroslav@382: * This program is distributed in the hope that it will be useful, jaroslav@382: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@382: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@382: * GNU General Public License for more details. jaroslav@382: * jaroslav@382: * You should have received a copy of the GNU General Public License jaroslav@382: * along with this program. Look for COPYING file in the top folder. jaroslav@382: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@382: */ jaroslav@382: package org.apidesign.bck2brwsr.launcher; jaroslav@382: jaroslav@382: import java.io.Closeable; jaroslav@613: import java.io.File; jaroslav@382: import java.io.IOException; jaroslav@844: import java.lang.reflect.Method; jaroslav@382: import java.net.URLClassLoader; jaroslav@382: import org.apidesign.vm4brwsr.Bck2Brwsr; jaroslav@382: jaroslav@382: /** An abstraction for executing tests in a Bck2Brwsr virtual machine. jaroslav@622: * Either in {@linkm Launcher#createJavaScript JavaScript engine}, jaroslav@622: * or in {@linkm Launcher#createBrowser external browser}. jaroslav@794: *

jaroslav@794: * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages} jaroslav@794: * in an external browser served by internal HTTP server. jaroslav@382: * jaroslav@382: * @author Jaroslav Tulach jaroslav@382: */ jaroslav@382: public abstract class Launcher { jaroslav@382: jaroslav@382: Launcher() { jaroslav@382: } jaroslav@622: jaroslav@622: /** Initializes the launcher. This may mean starting a web browser or jaroslav@622: * initializing execution engine. jaroslav@622: * @throws IOException if something goes wrong jaroslav@622: */ jaroslav@622: public abstract void initialize() throws IOException; jaroslav@382: jaroslav@622: /** Shuts down the launcher. jaroslav@622: * @throws IOException if something goes wrong jaroslav@622: */ jaroslav@382: public abstract void shutdown() throws IOException; jaroslav@622: jaroslav@622: jaroslav@622: /** Builds an invocation context. The context can later be customized jaroslav@622: * and {@link InvocationContext#invoke() invoked}. jaroslav@622: * jaroslav@622: * @param clazz the class to execute method from jaroslav@622: * @param method the method to execute jaroslav@622: * @return the context pointing to the selected method jaroslav@622: */ jaroslav@622: public InvocationContext createInvocation(Class clazz, String method) { jaroslav@622: return new InvocationContext(this, clazz, method); jaroslav@382: } jaroslav@382: jaroslav@382: jaroslav@622: /** Creates launcher that uses internal JavaScript engine (Rhino). jaroslav@622: * @return the launcher jaroslav@622: */ jaroslav@382: public static Launcher createJavaScript() { jaroslav@382: final JSLauncher l = new JSLauncher(); jaroslav@382: l.addClassLoader(Bck2Brwsr.class.getClassLoader()); jaroslav@382: return l; jaroslav@382: } jaroslav@382: jaroslav@844: /** Creates launcher that is using external browser. Value of cmd jaroslav@844: * may be "fx" to indicate one wants to use JavaFX's WebView jaroslav@844: * instead of command line browser. jaroslav@622: * jaroslav@622: * @param cmd null to use java.awt.Desktop to show the launcher jaroslav@622: * or a string to execute in an external process (with a parameter to the URL) jaroslav@622: * @return launcher executing in external browser. jaroslav@622: */ jaroslav@382: public static Launcher createBrowser(String cmd) { jaroslav@844: final Bck2BrwsrLauncher l; jaroslav@844: if ("fx".equals(cmd)) { jaroslav@844: l = new WebViewLauncher(); jaroslav@844: } else { jaroslav@844: l = new Bck2BrwsrLauncher(cmd); jaroslav@844: } jaroslav@382: l.addClassLoader(Bck2Brwsr.class.getClassLoader()); jaroslav@382: l.setTimeout(180000); jaroslav@382: return l; jaroslav@382: } jaroslav@622: jaroslav@622: /** Starts an HTTP server which provides access to classes and resources jaroslav@622: * available in the classes URL and shows a start page jaroslav@622: * available as {@link ClassLoader#getResource(java.lang.String)} from the jaroslav@622: * provide classloader. Opens a browser with URL showing the start page. jaroslav@622: * jaroslav@622: * @param classes classloader offering access to classes and resources jaroslav@622: * @param startpage page to show in the browser jaroslav@622: * @return interface that allows one to stop the server jaroslav@622: * @throws IOException if something goes wrong jaroslav@622: */ jaroslav@382: public static Closeable showURL(URLClassLoader classes, String startpage) throws IOException { jaroslav@845: Bck2BrwsrLauncher l = new WebViewLauncher(); jaroslav@382: l.addClassLoader(classes); jaroslav@382: l.showURL(startpage); jaroslav@382: return l; jaroslav@382: } jaroslav@622: /** Starts an HTTP server which provides access to certain directory. jaroslav@622: * The startpage should be relative location inside the root jaroslav@794: * directory. Opens a browser with URL showing the start page. jaroslav@622: * jaroslav@622: * @param directory the root directory on disk jaroslav@794: * @param startpage relative path from the root to the page jaroslav@622: * @exception IOException if something goes wrong. jaroslav@622: */ jaroslav@613: public static Closeable showDir(File directory, String startpage) throws IOException { jaroslav@845: Bck2BrwsrLauncher l = new WebViewLauncher(); jaroslav@613: l.showDirectory(directory, startpage); jaroslav@613: return l; jaroslav@613: } jaroslav@613: jaroslav@622: abstract InvocationContext runMethod(InvocationContext c) throws IOException; jaroslav@382: }