1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,115 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.launcher;
1.22 +
1.23 +import java.io.Closeable;
1.24 +import java.io.File;
1.25 +import java.io.IOException;
1.26 +import java.net.URLClassLoader;
1.27 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.28 +
1.29 +/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
1.30 + * Either in {@linkm Launcher#createJavaScript JavaScript engine},
1.31 + * or in {@linkm Launcher#createBrowser external browser}.
1.32 + *
1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.34 + */
1.35 +public abstract class Launcher {
1.36 +
1.37 + Launcher() {
1.38 + }
1.39 +
1.40 + /** Initializes the launcher. This may mean starting a web browser or
1.41 + * initializing execution engine.
1.42 + * @throws IOException if something goes wrong
1.43 + */
1.44 + public abstract void initialize() throws IOException;
1.45 +
1.46 + /** Shuts down the launcher.
1.47 + * @throws IOException if something goes wrong
1.48 + */
1.49 + public abstract void shutdown() throws IOException;
1.50 +
1.51 +
1.52 + /** Builds an invocation context. The context can later be customized
1.53 + * and {@link InvocationContext#invoke() invoked}.
1.54 + *
1.55 + * @param clazz the class to execute method from
1.56 + * @param method the method to execute
1.57 + * @return the context pointing to the selected method
1.58 + */
1.59 + public InvocationContext createInvocation(Class<?> clazz, String method) {
1.60 + return new InvocationContext(this, clazz, method);
1.61 + }
1.62 +
1.63 +
1.64 + /** Creates launcher that uses internal JavaScript engine (Rhino).
1.65 + * @return the launcher
1.66 + */
1.67 + public static Launcher createJavaScript() {
1.68 + final JSLauncher l = new JSLauncher();
1.69 + l.addClassLoader(Bck2Brwsr.class.getClassLoader());
1.70 + return l;
1.71 + }
1.72 +
1.73 + /** Creates launcher that is using external browser.
1.74 + *
1.75 + * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
1.76 + * or a string to execute in an external process (with a parameter to the URL)
1.77 + * @return launcher executing in external browser.
1.78 + */
1.79 + public static Launcher createBrowser(String cmd) {
1.80 + final Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(cmd);
1.81 + l.addClassLoader(Bck2Brwsr.class.getClassLoader());
1.82 + l.setTimeout(180000);
1.83 + return l;
1.84 + }
1.85 +
1.86 + /** Starts an HTTP server which provides access to classes and resources
1.87 + * available in the <code>classes</code> URL and shows a start page
1.88 + * available as {@link ClassLoader#getResource(java.lang.String)} from the
1.89 + * provide classloader. Opens a browser with URL showing the start page.
1.90 + *
1.91 + * @param classes classloader offering access to classes and resources
1.92 + * @param startpage page to show in the browser
1.93 + * @return interface that allows one to stop the server
1.94 + * @throws IOException if something goes wrong
1.95 + */
1.96 + public static Closeable showURL(URLClassLoader classes, String startpage) throws IOException {
1.97 + Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
1.98 + l.addClassLoader(classes);
1.99 + l.showURL(startpage);
1.100 + return l;
1.101 + }
1.102 + /** Starts an HTTP server which provides access to certain directory.
1.103 + * The <code>startpage</code> should be relative location inside the root
1.104 + * driecotry
1.105 + * Opens a browser with URL showing the start page.
1.106 + *
1.107 + * @param directory the root directory on disk
1.108 + * @praam startpage relative path from the root to the page
1.109 + * @exception IOException if something goes wrong.
1.110 + */
1.111 + public static Closeable showDir(File directory, String startpage) throws IOException {
1.112 + Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
1.113 + l.showDirectory(directory, startpage);
1.114 + return l;
1.115 + }
1.116 +
1.117 + abstract InvocationContext runMethod(InvocationContext c) throws IOException;
1.118 +}