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