launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 31 Jan 2013 17:39:47 +0100
branchemul
changeset 623 4af0d3dedb9d
parent 622 a07253cf2ca4
child 626 f08eb4df84c1
permissions -rw-r--r--
@HttpResource allows a test to connect back to the server and read static resource
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher;
    19 
    20 import java.io.IOException;
    21 import java.util.concurrent.CountDownLatch;
    22 import java.util.concurrent.TimeUnit;
    23 
    24 /** Represents individual method invocation, its context and its result.
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public final class InvocationContext {
    29     final CountDownLatch wait = new CountDownLatch(1);
    30     final Class<?> clazz;
    31     final String methodName;
    32     private final Launcher launcher;
    33     private String result;
    34     private Throwable exception;
    35     String html;
    36     String httpContent;
    37     String httpType;
    38     String httpPath;
    39 
    40     InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
    41         this.launcher = launcher;
    42         this.clazz = clazz;
    43         this.methodName = methodName;
    44     }
    45     
    46     /** An HTML fragment to be available for the execution. Useful primarily when
    47      * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
    48      * @param html the html fragment
    49      */
    50     public void setHtmlFragment(String html) {
    51         this.html = html;
    52     }
    53     
    54     /** HTTP resource to be available during execution. An invocation may
    55      * perform an HTTP query and obtain a resource relative to the page.
    56      */
    57     public void setHttpResource(String relativePath, String mimeType, String content) {
    58         if (relativePath == null || mimeType == null || content == null) {
    59             throw new NullPointerException();
    60         }
    61         this.httpPath = relativePath;
    62         this.httpType = mimeType;
    63         this.httpContent = content;
    64     }
    65     
    66     /** Invokes the associated method. 
    67      * @return the textual result of the invocation
    68      */
    69     public String invoke() throws IOException {
    70         launcher.runMethod(this);
    71         return toString();
    72     }
    73     
    74     /** Obtains textual result of the invocation.
    75      * @return text representing the exception or result value
    76      */
    77     @Override
    78     public String toString() {
    79         if (exception != null) {
    80             return exception.toString();
    81         }
    82         return result;
    83     }
    84     
    85     /**
    86      * @param timeOut
    87      * @throws InterruptedException 
    88      */
    89     void await(long timeOut) throws InterruptedException {
    90         wait.await(timeOut, TimeUnit.MILLISECONDS);
    91     }
    92     
    93     void result(String r, Throwable e) {
    94         this.result = r;
    95         this.exception = e;
    96         wait.countDown();
    97     }
    98 
    99     
   100 }