2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.io.Closeable;
22 import java.io.IOException;
23 import java.lang.reflect.Constructor;
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}.
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.
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 public abstract class Launcher {
39 /** Initializes the launcher. This may mean starting a web browser or
40 * initializing execution engine.
41 * @throws IOException if something goes wrong
43 public abstract void initialize() throws IOException;
45 /** Shuts down the launcher.
46 * @throws IOException if something goes wrong
48 public abstract void shutdown() throws IOException;
51 /** Builds an invocation context. The context can later be customized
52 * and {@link InvocationContext#invoke() invoked}.
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
58 public InvocationContext createInvocation(Class<?> clazz, String method) {
59 return new InvocationContext(this, clazz, method);
63 /** Creates launcher that uses internal JavaScript engine (Rhino).
64 * @return the launcher
66 public static Launcher createJavaScript() {
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);
75 /** Creates launcher that is using external browser.
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.
81 public static Launcher createBrowser(String cmd) {
82 return createBrowser(cmd, null, null);
85 /** Creates launcher that is using external browser.
87 * @param classes classloader offering access to classes and resources
88 * @param startpage page to show in the browser
89 * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
90 * or a string to execute in an external process (with a parameter to the URL)
91 * @return launcher executing in external browser.
93 public static Launcher createBrowser(String cmd, ClassLoader classes, String startpage) {
94 String msg = "Trying to create browser '" + cmd + "'";
97 if ("fxbrwsr".equals(cmd)) { // NOI18N
98 msg = "Please include org.apidesign.bck2brwsr:launcher.fx dependency!";
99 c = loadClass("org.apidesign.bck2brwsr.launcher.FXBrwsrLauncher"); // NOI18N
101 msg = "Please include org.apidesign.bck2brwsr:launcher.html dependency!";
102 c = loadClass("org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher"); // NOI18N
103 if ("bck2brwsr".equals(cmd)) { // NOI18N
104 // use default executable
108 Constructor<?> cnstr = c.getConstructor(String.class);
109 final Launcher launcher = (Launcher) cnstr.newInstance(cmd);
110 if (classes != null) {
111 launcher.addClassLoader(classes);
113 launcher.rootPage(startpage);
115 } catch (Exception ex) {
116 throw new IllegalStateException(msg, ex);
120 /** Starts an HTTP server which provides access to classes and resources
121 * available in the <code>classes</code> URL and shows a start page
122 * available as {@link ClassLoader#getResource(java.lang.String)} from the
123 * provide classloader. Opens a browser with URL showing the start page.
125 * @param classes classloader offering access to classes and resources
126 * @param startpage page to show in the browser
127 * @return interface that allows one to stop the server
128 * @throws IOException if something goes wrong
130 public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
131 return showURL(null, classes, startpage);
133 /** Starts an HTTP server which provides access to classes and resources
134 * available in the <code>classes</code> URL and shows a start page
135 * available as {@link ClassLoader#getResource(java.lang.String)} from the
136 * provide classloader. Opens a browser with URL showing the start page.
138 * @param brwsr name of browser to use or <code>null</code>
139 * @param classes classloader offering access to classes and resources
140 * @param startpage page to show in the browser
141 * @return interface that allows one to stop the server
142 * @throws IOException if something goes wrong
145 public static Closeable showURL(String brwsr, ClassLoader classes, String startpage) throws IOException {
146 Launcher l = createBrowser(brwsr);
147 l.addClassLoader(classes);
148 l.showURL(startpage);
149 return (Closeable) l;
151 /** Starts an HTTP server which provides access to certain directory.
152 * The <code>startpage</code> should be relative location inside the root
153 * directory. Opens a browser with URL showing the start page.
155 * @param brwsr type of the browser to use
156 * @param directory the root directory on disk
157 * @param classes additional classloader with access to classes or <code>null</code>
158 * @param startpage relative path from the root to the page
159 * @return instance of server that can be closed
160 * @exception IOException if something goes wrong.
163 public static Closeable showDir(String brwsr, File directory, ClassLoader classes, String startpage) throws IOException {
164 Launcher l = createBrowser(brwsr);
165 if (classes != null) {
166 l.addClassLoader(classes);
168 l.showDirectory(directory, startpage, classes != null);
169 return (Closeable) l;
172 /** Starts an HTTP server which provides access to certain directory.
173 * The <code>startpage</code> should be relative location inside the root
174 * directory. Opens a browser with URL showing the start page.
176 * @param directory the root directory on disk
177 * @param startpage relative path from the root to the page
178 * @return instance of server that can be closed
179 * @exception IOException if something goes wrong.
181 public static Closeable showDir(File directory, String startpage) throws IOException {
182 return showDir(null, directory, null, startpage);
185 abstract InvocationContext runMethod(InvocationContext c) throws IOException;
188 private static Class<?> loadClass(String cn) throws ClassNotFoundException {
189 return Launcher.class.getClassLoader().loadClass(cn);
192 void showDirectory(File directory, String startpage, boolean addClasses) throws IOException {
193 throw new UnsupportedOperationException();
196 void showURL(String startpage) throws IOException {
197 throw new UnsupportedOperationException();
200 void addClassLoader(ClassLoader classes) {
201 throw new UnsupportedOperationException();
204 void rootPage(String startpage) {
205 throw new UnsupportedOperationException();