launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Jan 2016 04:36:23 +0100
changeset 1860 4ce38f21f4cd
parent 1787 ea12a3bb4b33
child 1908 4f4554f69892
permissions -rw-r--r--
JavaScript in a browser can be at most three times slower than HotSpot when computing five or ten thousands of prime numbers
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.ArrayList;
    23 import java.util.List;
    24 import java.util.concurrent.CountDownLatch;
    25 import java.util.concurrent.TimeUnit;
    26 
    27 /** Represents individual method invocation, its context and its result.
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 public final class InvocationContext {
    32     final CountDownLatch wait = new CountDownLatch(1);
    33     final Class<?> clazz;
    34     final String methodName;
    35     private final Launcher launcher;
    36     private String result;
    37     private Throwable exception;
    38     String html;
    39     final List<Resource> resources = new ArrayList<>();
    40     private int time;
    41 
    42     InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
    43         this.launcher = launcher;
    44         this.clazz = clazz;
    45         this.methodName = methodName;
    46     }
    47     
    48     /** An HTML fragment to be available for the execution. Useful primarily when
    49      * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
    50      * @param html the html fragment
    51      */
    52     public void setHtmlFragment(String html) {
    53         this.html = html;
    54     }
    55     
    56     /** HTTP resource to be available during execution. An invocation may
    57      * perform an HTTP query and obtain a resource relative to the page.
    58      */
    59     public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
    60         if (relativePath == null || mimeType == null || content == null || parameters == null) {
    61             throw new NullPointerException();
    62         }
    63         resources.add(new Resource(content, mimeType, relativePath, parameters));
    64     }
    65     
    66     /** Invokes the associated method. 
    67      * @return the textual result of the invocation
    68      * @throws java.io.IOException if execution fails
    69      */
    70     public String invoke() throws IOException {
    71         launcher.runMethod(this);
    72         return toString();
    73     }
    74 
    75     /** Invokes the associated method.
    76      * @param time one element array to store the length of the invocation
    77      *    - can be <code>null</code>
    78      * @return the textual result of the invocation
    79      * @throws java.io.IOException if execution fails
    80      * @since 0.20
    81      */
    82     public String invoke(int[] time) throws IOException {
    83         launcher.runMethod(this);
    84         if (time != null) {
    85             time[0] = this.time;
    86         }
    87         return toString();
    88     }
    89     
    90     /** Obtains textual result of the invocation.
    91      * @return text representing the exception or result value
    92      */
    93     @Override
    94     public String toString() {
    95         if (exception != null) {
    96             return exception.toString();
    97         }
    98         return result;
    99     }
   100     
   101     /**
   102      * @param timeOut
   103      * @throws InterruptedException 
   104      */
   105     void await(long timeOut) throws InterruptedException {
   106         wait.await(timeOut, TimeUnit.MILLISECONDS);
   107     }
   108     
   109     void result(String r, int time, Throwable e) {
   110         this.time = time;
   111         this.result = r;
   112         this.exception = e;
   113         wait.countDown();
   114     }
   115 
   116     static final class Resource {
   117         final InputStream httpContent;
   118         final String httpType;
   119         final String httpPath;
   120         final String[] parameters;
   121 
   122         Resource(InputStream httpContent, String httpType, String httpPath,
   123             String[] parameters
   124         ) {
   125             httpContent.mark(Integer.MAX_VALUE);
   126             this.httpContent = httpContent;
   127             this.httpType = httpType;
   128             this.httpPath = httpPath;
   129             this.parameters = parameters;
   130         }
   131     }
   132 }