rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,110 @@
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.IOException;
1.24 +import java.io.InputStream;
1.25 +import java.util.ArrayList;
1.26 +import java.util.List;
1.27 +import java.util.concurrent.CountDownLatch;
1.28 +import java.util.concurrent.TimeUnit;
1.29 +
1.30 +/** Represents individual method invocation, its context and its result.
1.31 + *
1.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.33 + */
1.34 +public final class InvocationContext {
1.35 + final CountDownLatch wait = new CountDownLatch(1);
1.36 + final Class<?> clazz;
1.37 + final String methodName;
1.38 + private final Launcher launcher;
1.39 + private String result;
1.40 + private Throwable exception;
1.41 + String html;
1.42 + final List<Resource> resources = new ArrayList<>();
1.43 +
1.44 + InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
1.45 + this.launcher = launcher;
1.46 + this.clazz = clazz;
1.47 + this.methodName = methodName;
1.48 + }
1.49 +
1.50 + /** An HTML fragment to be available for the execution. Useful primarily when
1.51 + * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
1.52 + * @param html the html fragment
1.53 + */
1.54 + public void setHtmlFragment(String html) {
1.55 + this.html = html;
1.56 + }
1.57 +
1.58 + /** HTTP resource to be available during execution. An invocation may
1.59 + * perform an HTTP query and obtain a resource relative to the page.
1.60 + */
1.61 + public void addHttpResource(String relativePath, String mimeType, InputStream content) {
1.62 + if (relativePath == null || mimeType == null || content == null) {
1.63 + throw new NullPointerException();
1.64 + }
1.65 + resources.add(new Resource(content, mimeType, relativePath));
1.66 + }
1.67 +
1.68 + /** Invokes the associated method.
1.69 + * @return the textual result of the invocation
1.70 + */
1.71 + public String invoke() throws IOException {
1.72 + launcher.runMethod(this);
1.73 + return toString();
1.74 + }
1.75 +
1.76 + /** Obtains textual result of the invocation.
1.77 + * @return text representing the exception or result value
1.78 + */
1.79 + @Override
1.80 + public String toString() {
1.81 + if (exception != null) {
1.82 + return exception.toString();
1.83 + }
1.84 + return result;
1.85 + }
1.86 +
1.87 + /**
1.88 + * @param timeOut
1.89 + * @throws InterruptedException
1.90 + */
1.91 + void await(long timeOut) throws InterruptedException {
1.92 + wait.await(timeOut, TimeUnit.MILLISECONDS);
1.93 + }
1.94 +
1.95 + void result(String r, Throwable e) {
1.96 + this.result = r;
1.97 + this.exception = e;
1.98 + wait.countDown();
1.99 + }
1.100 +
1.101 +
1.102 + static final class Resource {
1.103 + final InputStream httpContent;
1.104 + final String httpType;
1.105 + final String httpPath;
1.106 +
1.107 + Resource(InputStream httpContent, String httpType, String httpPath) {
1.108 + this.httpContent = httpContent;
1.109 + this.httpType = httpType;
1.110 + this.httpPath = httpPath;
1.111 + }
1.112 + }
1.113 +}