launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 10 Jul 2014 08:17:52 +0200
branchjdk8
changeset 1641 2111057af3b2
parent 1271 46e2b4ef85a4
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Filling in the constant pool
     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.lang.reflect.Constructor;
    24 
    25 /** An abstraction for executing tests in a Bck2Brwsr virtual machine.
    26  * Either in {@link Launcher#createJavaScript JavaScript engine}, 
    27  * or in {@link Launcher#createBrowser external browser}.
    28  * <p>
    29  * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages} 
    30  * in an external browser served by internal HTTP server.
    31  *
    32  * @author Jaroslav Tulach <jtulach@netbeans.org>
    33  */
    34 public abstract class Launcher {
    35 
    36     Launcher() {
    37     }
    38 
    39     /** Initializes the launcher. This may mean starting a web browser or
    40      * initializing execution engine.
    41      * @throws IOException if something goes wrong
    42      */
    43     public abstract void initialize() throws IOException;
    44     
    45     /** Shuts down the launcher.
    46      * @throws IOException if something goes wrong
    47      */
    48     public abstract void shutdown() throws IOException;
    49     
    50     
    51     /** Builds an invocation context. The context can later be customized
    52      * and {@link InvocationContext#invoke() invoked}.
    53      * 
    54      * @param clazz the class to execute method from
    55      * @param method the method to execute
    56      * @return the context pointing to the selected method
    57      */
    58     public InvocationContext createInvocation(Class<?> clazz, String method) {
    59         return new InvocationContext(this, clazz, method);
    60     }
    61     
    62 
    63     /** Creates launcher that uses internal JavaScript engine (Rhino).
    64      * @return the launcher
    65      */
    66     public static Launcher createJavaScript() {
    67         try {
    68             Class<?> c = loadClass("org.apidesign.bck2brwsr.launcher.JSLauncher");
    69             return (Launcher) c.newInstance();
    70         } catch (Exception ex) {
    71             throw new IllegalStateException("Please include org.apidesign.bck2brwsr:launcher.html dependency!", ex);
    72         }
    73     }
    74     
    75     /** Creates launcher that is using external browser.
    76      * 
    77      * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
    78      *    or a string to execute in an external process (with a parameter to the URL)
    79      * @return launcher executing in external browser.
    80      */
    81     public static Launcher createBrowser(String cmd) {
    82         String msg = "Trying to create browser '" + cmd + "'";
    83         try {
    84             Class<?> c;
    85             if ("fxbrwsr".equals(cmd)) { // NOI18N
    86                 msg = "Please include org.apidesign.bck2brwsr:launcher.fx dependency!";
    87                 c = loadClass("org.apidesign.bck2brwsr.launcher.FXBrwsrLauncher"); // NOI18N
    88             } else {
    89                 msg = "Please include org.apidesign.bck2brwsr:launcher.html dependency!";
    90                 c = loadClass("org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher"); // NOI18N
    91                 if ("bck2brwsr".equals(cmd)) { // NOI18N
    92                     // use default executable
    93                     cmd = null;
    94                 }
    95             }
    96             Constructor<?> cnstr = c.getConstructor(String.class);
    97             return (Launcher) cnstr.newInstance(cmd);
    98         } catch (Exception ex) {
    99             throw new IllegalStateException(msg, ex);
   100         }
   101     }
   102     
   103     /** Starts an HTTP server which provides access to classes and resources
   104      * available in the <code>classes</code> URL and shows a start page
   105      * available as {@link ClassLoader#getResource(java.lang.String)} from the
   106      * provide classloader. Opens a browser with URL showing the start page.
   107      * 
   108      * @param classes classloader offering access to classes and resources
   109      * @param startpage page to show in the browser
   110      * @return interface that allows one to stop the server
   111      * @throws IOException if something goes wrong
   112      */
   113     public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
   114         return showURL(null, classes, startpage);
   115     }
   116     /** Starts an HTTP server which provides access to classes and resources
   117      * available in the <code>classes</code> URL and shows a start page
   118      * available as {@link ClassLoader#getResource(java.lang.String)} from the
   119      * provide classloader. Opens a browser with URL showing the start page.
   120      * 
   121      * @param brwsr name of browser to use or <code>null</code>
   122      * @param classes classloader offering access to classes and resources
   123      * @param startpage page to show in the browser
   124      * @return interface that allows one to stop the server
   125      * @throws IOException if something goes wrong
   126      * @since 0.7
   127      */
   128     public static Closeable showURL(String brwsr, ClassLoader classes, String startpage) throws IOException {
   129         Launcher l = createBrowser(brwsr);
   130         l.addClassLoader(classes);
   131         l.showURL(startpage);
   132         return (Closeable) l;
   133     }
   134     /** Starts an HTTP server which provides access to certain directory.
   135      * The <code>startpage</code> should be relative location inside the root 
   136      * directory. Opens a browser with URL showing the start page.
   137      * 
   138      * @param brwsr type of the browser to use
   139      * @param directory the root directory on disk
   140      * @param classes additional classloader with access to classes or <code>null</code>
   141      * @param startpage relative path from the root to the page
   142      * @return instance of server that can be closed
   143      * @exception IOException if something goes wrong.
   144      * @since 0.8
   145      */
   146     public static Closeable showDir(String brwsr, File directory, ClassLoader classes, String startpage) throws IOException {
   147         Launcher l = createBrowser(brwsr);
   148         if (classes != null) {
   149             l.addClassLoader(classes);
   150         }
   151         l.showDirectory(directory, startpage, classes != null);
   152         return (Closeable) l;
   153     }
   154     
   155     /** Starts an HTTP server which provides access to certain directory.
   156      * The <code>startpage</code> should be relative location inside the root 
   157      * directory. Opens a browser with URL showing the start page.
   158      * 
   159      * @param directory the root directory on disk
   160      * @param startpage relative path from the root to the page
   161      * @return instance of server that can be closed
   162      * @exception IOException if something goes wrong.
   163      */
   164     public static Closeable showDir(File directory, String startpage) throws IOException {
   165         return showDir(null, directory, null, startpage);
   166     }
   167 
   168     abstract InvocationContext runMethod(InvocationContext c) throws IOException; 
   169 
   170 
   171     private static Class<?> loadClass(String cn) throws ClassNotFoundException {
   172         return Launcher.class.getClassLoader().loadClass(cn);
   173     }
   174 
   175     void showDirectory(File directory, String startpage, boolean addClasses) throws IOException {
   176         throw new UnsupportedOperationException();
   177     }
   178 
   179     void showURL(String startpage) throws IOException {
   180         throw new UnsupportedOperationException();
   181     }
   182 
   183     void addClassLoader(ClassLoader classes) {
   184         throw new UnsupportedOperationException();
   185     }
   186 }