jaroslav@370
|
1 |
/**
|
jaroslav@370
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@370
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@370
|
4 |
*
|
jaroslav@370
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@370
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@370
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@370
|
8 |
*
|
jaroslav@370
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@370
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@370
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@370
|
12 |
* GNU General Public License for more details.
|
jaroslav@370
|
13 |
*
|
jaroslav@370
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@370
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@370
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@370
|
17 |
*/
|
jaroslav@370
|
18 |
package org.apidesign.bck2brwsr.launcher;
|
jaroslav@370
|
19 |
|
jaroslav@622
|
20 |
import java.io.IOException;
|
jaroslav@626
|
21 |
import java.io.InputStream;
|
jaroslav@371
|
22 |
import java.util.concurrent.CountDownLatch;
|
jaroslav@371
|
23 |
import java.util.concurrent.TimeUnit;
|
jaroslav@371
|
24 |
|
jaroslav@622
|
25 |
/** Represents individual method invocation, its context and its result.
|
jaroslav@370
|
26 |
*
|
jaroslav@370
|
27 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@370
|
28 |
*/
|
jaroslav@622
|
29 |
public final class InvocationContext {
|
jaroslav@371
|
30 |
final CountDownLatch wait = new CountDownLatch(1);
|
jaroslav@622
|
31 |
final Class<?> clazz;
|
jaroslav@370
|
32 |
final String methodName;
|
jaroslav@622
|
33 |
private final Launcher launcher;
|
jaroslav@371
|
34 |
private String result;
|
jaroslav@385
|
35 |
private Throwable exception;
|
jaroslav@622
|
36 |
String html;
|
jaroslav@626
|
37 |
InputStream httpContent;
|
jaroslav@622
|
38 |
String httpType;
|
jaroslav@622
|
39 |
String httpPath;
|
jaroslav@370
|
40 |
|
jaroslav@622
|
41 |
InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
|
jaroslav@622
|
42 |
this.launcher = launcher;
|
jaroslav@622
|
43 |
this.clazz = clazz;
|
jaroslav@370
|
44 |
this.methodName = methodName;
|
jaroslav@622
|
45 |
}
|
jaroslav@622
|
46 |
|
jaroslav@622
|
47 |
/** An HTML fragment to be available for the execution. Useful primarily when
|
jaroslav@622
|
48 |
* executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
|
jaroslav@622
|
49 |
* @param html the html fragment
|
jaroslav@622
|
50 |
*/
|
jaroslav@622
|
51 |
public void setHtmlFragment(String html) {
|
jaroslav@526
|
52 |
this.html = html;
|
jaroslav@370
|
53 |
}
|
jaroslav@371
|
54 |
|
jaroslav@622
|
55 |
/** HTTP resource to be available during execution. An invocation may
|
jaroslav@622
|
56 |
* perform an HTTP query and obtain a resource relative to the page.
|
jaroslav@622
|
57 |
*/
|
jaroslav@626
|
58 |
public void setHttpResource(String relativePath, String mimeType, InputStream content) {
|
jaroslav@623
|
59 |
if (relativePath == null || mimeType == null || content == null) {
|
jaroslav@623
|
60 |
throw new NullPointerException();
|
jaroslav@623
|
61 |
}
|
jaroslav@622
|
62 |
this.httpPath = relativePath;
|
jaroslav@622
|
63 |
this.httpType = mimeType;
|
jaroslav@622
|
64 |
this.httpContent = content;
|
jaroslav@622
|
65 |
}
|
jaroslav@622
|
66 |
|
jaroslav@622
|
67 |
/** Invokes the associated method.
|
jaroslav@622
|
68 |
* @return the textual result of the invocation
|
jaroslav@622
|
69 |
*/
|
jaroslav@622
|
70 |
public String invoke() throws IOException {
|
jaroslav@622
|
71 |
launcher.runMethod(this);
|
jaroslav@622
|
72 |
return toString();
|
jaroslav@622
|
73 |
}
|
jaroslav@622
|
74 |
|
jaroslav@622
|
75 |
/** Obtains textual result of the invocation.
|
jaroslav@622
|
76 |
* @return text representing the exception or result value
|
jaroslav@622
|
77 |
*/
|
jaroslav@622
|
78 |
@Override
|
jaroslav@622
|
79 |
public String toString() {
|
jaroslav@622
|
80 |
if (exception != null) {
|
jaroslav@622
|
81 |
return exception.toString();
|
jaroslav@622
|
82 |
}
|
jaroslav@622
|
83 |
return result;
|
jaroslav@622
|
84 |
}
|
jaroslav@622
|
85 |
|
jaroslav@622
|
86 |
/**
|
jaroslav@622
|
87 |
* @param timeOut
|
jaroslav@622
|
88 |
* @throws InterruptedException
|
jaroslav@622
|
89 |
*/
|
jaroslav@371
|
90 |
void await(long timeOut) throws InterruptedException {
|
jaroslav@371
|
91 |
wait.await(timeOut, TimeUnit.MILLISECONDS);
|
jaroslav@371
|
92 |
}
|
jaroslav@371
|
93 |
|
jaroslav@385
|
94 |
void result(String r, Throwable e) {
|
jaroslav@371
|
95 |
this.result = r;
|
jaroslav@371
|
96 |
this.exception = e;
|
jaroslav@371
|
97 |
wait.countDown();
|
jaroslav@371
|
98 |
}
|
jaroslav@370
|
99 |
|
jaroslav@370
|
100 |
|
jaroslav@370
|
101 |
}
|