jaroslav@1033
|
1 |
/**
|
jaroslav@1033
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1033
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1033
|
4 |
*
|
jaroslav@1033
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1033
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1033
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1033
|
8 |
*
|
jaroslav@1033
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1033
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1033
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1033
|
12 |
* GNU General Public License for more details.
|
jaroslav@1033
|
13 |
*
|
jaroslav@1033
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1033
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1033
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1033
|
17 |
*/
|
jaroslav@1033
|
18 |
package org.apidesign.bck2brwsr.launcher;
|
jaroslav@1033
|
19 |
|
jaroslav@1033
|
20 |
import java.io.Closeable;
|
jaroslav@1033
|
21 |
import java.io.File;
|
jaroslav@1033
|
22 |
import java.io.IOException;
|
jaroslav@1033
|
23 |
import java.lang.reflect.Constructor;
|
jaroslav@1033
|
24 |
|
jaroslav@1033
|
25 |
/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
|
jaroslav@1033
|
26 |
* Either in {@link Launcher#createJavaScript JavaScript engine},
|
jaroslav@1033
|
27 |
* or in {@link Launcher#createBrowser external browser}.
|
jaroslav@1033
|
28 |
* <p>
|
jaroslav@1033
|
29 |
* There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages}
|
jaroslav@1033
|
30 |
* in an external browser served by internal HTTP server.
|
jaroslav@1033
|
31 |
*
|
jaroslav@1033
|
32 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1033
|
33 |
*/
|
jaroslav@1033
|
34 |
public abstract class Launcher {
|
jaroslav@1033
|
35 |
|
jaroslav@1033
|
36 |
Launcher() {
|
jaroslav@1033
|
37 |
}
|
jaroslav@1033
|
38 |
|
jaroslav@1033
|
39 |
/** Initializes the launcher. This may mean starting a web browser or
|
jaroslav@1033
|
40 |
* initializing execution engine.
|
jaroslav@1033
|
41 |
* @throws IOException if something goes wrong
|
jaroslav@1033
|
42 |
*/
|
jaroslav@1033
|
43 |
public abstract void initialize() throws IOException;
|
jaroslav@1033
|
44 |
|
jaroslav@1033
|
45 |
/** Shuts down the launcher.
|
jaroslav@1033
|
46 |
* @throws IOException if something goes wrong
|
jaroslav@1033
|
47 |
*/
|
jaroslav@1033
|
48 |
public abstract void shutdown() throws IOException;
|
jaroslav@1033
|
49 |
|
jaroslav@1033
|
50 |
|
jaroslav@1033
|
51 |
/** Builds an invocation context. The context can later be customized
|
jaroslav@1033
|
52 |
* and {@link InvocationContext#invoke() invoked}.
|
jaroslav@1033
|
53 |
*
|
jaroslav@1033
|
54 |
* @param clazz the class to execute method from
|
jaroslav@1033
|
55 |
* @param method the method to execute
|
jaroslav@1033
|
56 |
* @return the context pointing to the selected method
|
jaroslav@1033
|
57 |
*/
|
jaroslav@1033
|
58 |
public InvocationContext createInvocation(Class<?> clazz, String method) {
|
jaroslav@1033
|
59 |
return new InvocationContext(this, clazz, method);
|
jaroslav@1033
|
60 |
}
|
jaroslav@1033
|
61 |
|
jaroslav@1033
|
62 |
|
jaroslav@1033
|
63 |
/** Creates launcher that uses internal JavaScript engine (Rhino).
|
jaroslav@1033
|
64 |
* @return the launcher
|
jaroslav@1033
|
65 |
*/
|
jaroslav@1033
|
66 |
public static Launcher createJavaScript() {
|
jaroslav@1033
|
67 |
try {
|
jaroslav@1033
|
68 |
Class<?> c = loadClass("org.apidesign.bck2brwsr.launcher.JSLauncher");
|
jaroslav@1033
|
69 |
return (Launcher) c.newInstance();
|
jaroslav@1033
|
70 |
} catch (Exception ex) {
|
jaroslav@1033
|
71 |
throw new IllegalStateException("Please include org.apidesign.bck2brwsr:launcher.html dependency!", ex);
|
jaroslav@1033
|
72 |
}
|
jaroslav@1033
|
73 |
}
|
jaroslav@1033
|
74 |
|
jaroslav@1033
|
75 |
/** Creates launcher that is using external browser.
|
jaroslav@1033
|
76 |
*
|
jaroslav@1033
|
77 |
* @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
|
jaroslav@1033
|
78 |
* or a string to execute in an external process (with a parameter to the URL)
|
jaroslav@1033
|
79 |
* @return launcher executing in external browser.
|
jaroslav@1033
|
80 |
*/
|
jaroslav@1033
|
81 |
public static Launcher createBrowser(String cmd) {
|
jaroslav@1041
|
82 |
String msg = "Trying to create browser '" + cmd + "'";
|
jaroslav@1033
|
83 |
try {
|
jaroslav@1041
|
84 |
Class<?> c;
|
jaroslav@1043
|
85 |
if ("fxbrwsr".equals(cmd)) { // NOI18N
|
jaroslav@1041
|
86 |
msg = "Please include org.apidesign.bck2brwsr:launcher.fx dependency!";
|
jaroslav@1041
|
87 |
c = loadClass("org.apidesign.bck2brwsr.launcher.FXBrwsrLauncher"); // NOI18N
|
jaroslav@1041
|
88 |
} else {
|
jaroslav@1041
|
89 |
msg = "Please include org.apidesign.bck2brwsr:launcher.html dependency!";
|
jaroslav@1041
|
90 |
c = loadClass("org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher"); // NOI18N
|
jaroslav@1043
|
91 |
if ("bck2brwsr".equals(cmd)) { // NOI18N
|
jaroslav@1043
|
92 |
// use default executable
|
jaroslav@1043
|
93 |
cmd = null;
|
jaroslav@1043
|
94 |
}
|
jaroslav@1041
|
95 |
}
|
jaroslav@1033
|
96 |
Constructor<?> cnstr = c.getConstructor(String.class);
|
jaroslav@1033
|
97 |
return (Launcher) cnstr.newInstance(cmd);
|
jaroslav@1033
|
98 |
} catch (Exception ex) {
|
jaroslav@1041
|
99 |
throw new IllegalStateException(msg, ex);
|
jaroslav@1033
|
100 |
}
|
jaroslav@1033
|
101 |
}
|
jaroslav@1033
|
102 |
|
jaroslav@1033
|
103 |
/** Starts an HTTP server which provides access to classes and resources
|
jaroslav@1033
|
104 |
* available in the <code>classes</code> URL and shows a start page
|
jaroslav@1033
|
105 |
* available as {@link ClassLoader#getResource(java.lang.String)} from the
|
jaroslav@1033
|
106 |
* provide classloader. Opens a browser with URL showing the start page.
|
jaroslav@1033
|
107 |
*
|
jaroslav@1033
|
108 |
* @param classes classloader offering access to classes and resources
|
jaroslav@1033
|
109 |
* @param startpage page to show in the browser
|
jaroslav@1033
|
110 |
* @return interface that allows one to stop the server
|
jaroslav@1033
|
111 |
* @throws IOException if something goes wrong
|
jaroslav@1033
|
112 |
*/
|
jaroslav@1033
|
113 |
public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
|
jaroslav@1043
|
114 |
return showURL(null, classes, startpage);
|
jaroslav@1043
|
115 |
}
|
jaroslav@1043
|
116 |
/** Starts an HTTP server which provides access to classes and resources
|
jaroslav@1043
|
117 |
* available in the <code>classes</code> URL and shows a start page
|
jaroslav@1043
|
118 |
* available as {@link ClassLoader#getResource(java.lang.String)} from the
|
jaroslav@1043
|
119 |
* provide classloader. Opens a browser with URL showing the start page.
|
jaroslav@1043
|
120 |
*
|
jaroslav@1043
|
121 |
* @param brwsr name of browser to use or <code>null</code>
|
jaroslav@1043
|
122 |
* @param classes classloader offering access to classes and resources
|
jaroslav@1043
|
123 |
* @param startpage page to show in the browser
|
jaroslav@1043
|
124 |
* @return interface that allows one to stop the server
|
jaroslav@1043
|
125 |
* @throws IOException if something goes wrong
|
jaroslav@1043
|
126 |
* @since 0.7
|
jaroslav@1043
|
127 |
*/
|
jaroslav@1043
|
128 |
public static Closeable showURL(String brwsr, ClassLoader classes, String startpage) throws IOException {
|
jaroslav@1043
|
129 |
Launcher l = createBrowser(brwsr);
|
jaroslav@1033
|
130 |
l.addClassLoader(classes);
|
jaroslav@1033
|
131 |
l.showURL(startpage);
|
jaroslav@1033
|
132 |
return (Closeable) l;
|
jaroslav@1033
|
133 |
}
|
jaroslav@1033
|
134 |
/** Starts an HTTP server which provides access to certain directory.
|
jaroslav@1033
|
135 |
* The <code>startpage</code> should be relative location inside the root
|
jaroslav@1033
|
136 |
* directory. Opens a browser with URL showing the start page.
|
jaroslav@1033
|
137 |
*
|
jaroslav@1033
|
138 |
* @param directory the root directory on disk
|
jaroslav@1033
|
139 |
* @param startpage relative path from the root to the page
|
jaroslav@1033
|
140 |
* @exception IOException if something goes wrong.
|
jaroslav@1033
|
141 |
*/
|
jaroslav@1033
|
142 |
public static Closeable showDir(File directory, String startpage) throws IOException {
|
jaroslav@1033
|
143 |
Launcher l = createBrowser(null);
|
jaroslav@1033
|
144 |
l.showDirectory(directory, startpage);
|
jaroslav@1033
|
145 |
return (Closeable) l;
|
jaroslav@1033
|
146 |
}
|
jaroslav@1033
|
147 |
|
jaroslav@1033
|
148 |
abstract InvocationContext runMethod(InvocationContext c) throws IOException;
|
jaroslav@1033
|
149 |
|
jaroslav@1033
|
150 |
|
jaroslav@1033
|
151 |
private static Class<?> loadClass(String cn) throws ClassNotFoundException {
|
jaroslav@1033
|
152 |
return Launcher.class.getClassLoader().loadClass(cn);
|
jaroslav@1033
|
153 |
}
|
jaroslav@1033
|
154 |
|
jaroslav@1033
|
155 |
void showDirectory(File directory, String startpage) throws IOException {
|
jaroslav@1033
|
156 |
throw new UnsupportedOperationException();
|
jaroslav@1033
|
157 |
}
|
jaroslav@1033
|
158 |
|
jaroslav@1033
|
159 |
void showURL(String startpage) throws IOException {
|
jaroslav@1033
|
160 |
throw new UnsupportedOperationException();
|
jaroslav@1033
|
161 |
}
|
jaroslav@1033
|
162 |
|
jaroslav@1033
|
163 |
void addClassLoader(ClassLoader classes) {
|
jaroslav@1033
|
164 |
throw new UnsupportedOperationException();
|
jaroslav@1033
|
165 |
}
|
jaroslav@1033
|
166 |
}
|