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