1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Sun Apr 28 10:14:31 2013 +0200
1.3 @@ -0,0 +1,139 @@
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.lang.reflect.Constructor;
1.27 +
1.28 +/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
1.29 + * Either in {@link Launcher#createJavaScript JavaScript engine},
1.30 + * or in {@link Launcher#createBrowser external browser}.
1.31 + * <p>
1.32 + * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages}
1.33 + * in an external browser served by internal HTTP server.
1.34 + *
1.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.36 + */
1.37 +public abstract class Launcher {
1.38 +
1.39 + Launcher() {
1.40 + }
1.41 +
1.42 + /** Initializes the launcher. This may mean starting a web browser or
1.43 + * initializing execution engine.
1.44 + * @throws IOException if something goes wrong
1.45 + */
1.46 + public abstract void initialize() throws IOException;
1.47 +
1.48 + /** Shuts down the launcher.
1.49 + * @throws IOException if something goes wrong
1.50 + */
1.51 + public abstract void shutdown() throws IOException;
1.52 +
1.53 +
1.54 + /** Builds an invocation context. The context can later be customized
1.55 + * and {@link InvocationContext#invoke() invoked}.
1.56 + *
1.57 + * @param clazz the class to execute method from
1.58 + * @param method the method to execute
1.59 + * @return the context pointing to the selected method
1.60 + */
1.61 + public InvocationContext createInvocation(Class<?> clazz, String method) {
1.62 + return new InvocationContext(this, clazz, method);
1.63 + }
1.64 +
1.65 +
1.66 + /** Creates launcher that uses internal JavaScript engine (Rhino).
1.67 + * @return the launcher
1.68 + */
1.69 + public static Launcher createJavaScript() {
1.70 + try {
1.71 + Class<?> c = loadClass("org.apidesign.bck2brwsr.launcher.JSLauncher");
1.72 + return (Launcher) c.newInstance();
1.73 + } catch (Exception ex) {
1.74 + throw new IllegalStateException("Please include org.apidesign.bck2brwsr:launcher.html dependency!", ex);
1.75 + }
1.76 + }
1.77 +
1.78 + /** Creates launcher that is using external browser.
1.79 + *
1.80 + * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
1.81 + * or a string to execute in an external process (with a parameter to the URL)
1.82 + * @return launcher executing in external browser.
1.83 + */
1.84 + public static Launcher createBrowser(String cmd) {
1.85 + try {
1.86 + Class<?> c = loadClass("org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher");
1.87 + Constructor<?> cnstr = c.getConstructor(String.class);
1.88 + return (Launcher) cnstr.newInstance(cmd);
1.89 + } catch (Exception ex) {
1.90 + throw new IllegalStateException("Please include org.apidesign.bck2brwsr:launcher.html dependency!", ex);
1.91 + }
1.92 + }
1.93 +
1.94 + /** Starts an HTTP server which provides access to classes and resources
1.95 + * available in the <code>classes</code> URL and shows a start page
1.96 + * available as {@link ClassLoader#getResource(java.lang.String)} from the
1.97 + * provide classloader. Opens a browser with URL showing the start page.
1.98 + *
1.99 + * @param classes classloader offering access to classes and resources
1.100 + * @param startpage page to show in the browser
1.101 + * @return interface that allows one to stop the server
1.102 + * @throws IOException if something goes wrong
1.103 + */
1.104 + public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
1.105 + Launcher l = createBrowser(null);
1.106 + l.addClassLoader(classes);
1.107 + l.showURL(startpage);
1.108 + return (Closeable) l;
1.109 + }
1.110 + /** Starts an HTTP server which provides access to certain directory.
1.111 + * The <code>startpage</code> should be relative location inside the root
1.112 + * directory. Opens a browser with URL showing the start page.
1.113 + *
1.114 + * @param directory the root directory on disk
1.115 + * @param startpage relative path from the root to the page
1.116 + * @exception IOException if something goes wrong.
1.117 + */
1.118 + public static Closeable showDir(File directory, String startpage) throws IOException {
1.119 + Launcher l = createBrowser(null);
1.120 + l.showDirectory(directory, startpage);
1.121 + return (Closeable) l;
1.122 + }
1.123 +
1.124 + abstract InvocationContext runMethod(InvocationContext c) throws IOException;
1.125 +
1.126 +
1.127 + private static Class<?> loadClass(String cn) throws ClassNotFoundException {
1.128 + return Launcher.class.getClassLoader().loadClass(cn);
1.129 + }
1.130 +
1.131 + void showDirectory(File directory, String startpage) throws IOException {
1.132 + throw new UnsupportedOperationException();
1.133 + }
1.134 +
1.135 + void showURL(String startpage) throws IOException {
1.136 + throw new UnsupportedOperationException();
1.137 + }
1.138 +
1.139 + void addClassLoader(ClassLoader classes) {
1.140 + throw new UnsupportedOperationException();
1.141 + }
1.142 +}