launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 25 Mar 2016 11:12:16 +0100
changeset 1908 4f4554f69892
parent 1860 4ce38f21f4cd
permissions -rw-r--r--
Invoke methods with string parameters
     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     String[] args;
    40     final List<Resource> resources = new ArrayList<>();
    41     private int time;
    42 
    43     InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
    44         this.launcher = launcher;
    45         this.clazz = clazz;
    46         this.methodName = methodName;
    47     }
    48     
    49     /** An HTML fragment to be available for the execution. Useful primarily when
    50      * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
    51      * @param html the html fragment
    52      */
    53     public void setHtmlFragment(String html) {
    54         this.html = html;
    55     }
    56 
    57     /** Arguments to pass to the invoked method.
    58      * @param args textual arguments to pass to the method
    59      * @since 0.18
    60      */
    61     public void setArguments(String... args) {
    62         this.args = args;
    63     }
    64     
    65     /** HTTP resource to be available during execution. An invocation may
    66      * perform an HTTP query and obtain a resource relative to the page.
    67      */
    68     public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
    69         if (relativePath == null || mimeType == null || content == null || parameters == null) {
    70             throw new NullPointerException();
    71         }
    72         resources.add(new Resource(content, mimeType, relativePath, parameters));
    73     }
    74     
    75     /** Invokes the associated method. 
    76      * @return the textual result of the invocation
    77      * @throws java.io.IOException if execution fails
    78      */
    79     public String invoke() throws IOException {
    80         launcher.runMethod(this);
    81         return toString();
    82     }
    83 
    84     /** Invokes the associated method.
    85      * @param time one element array to store the length of the invocation
    86      *    - can be <code>null</code>
    87      * @return the textual result of the invocation
    88      * @throws java.io.IOException if execution fails
    89      * @since 0.20
    90      */
    91     public String invoke(int[] time) throws IOException {
    92         launcher.runMethod(this);
    93         if (time != null) {
    94             time[0] = this.time;
    95         }
    96         return toString();
    97     }
    98     
    99     /** Obtains textual result of the invocation.
   100      * @return text representing the exception or result value
   101      */
   102     @Override
   103     public String toString() {
   104         if (exception != null) {
   105             return exception.toString();
   106         }
   107         return result;
   108     }
   109     
   110     /**
   111      * @param timeOut
   112      * @throws InterruptedException 
   113      */
   114     void await(long timeOut) throws InterruptedException {
   115         wait.await(timeOut, TimeUnit.MILLISECONDS);
   116     }
   117     
   118     void result(String r, int time, Throwable e) {
   119         this.time = time;
   120         this.result = r;
   121         this.exception = e;
   122         wait.countDown();
   123     }
   124 
   125     static final class Resource {
   126         final InputStream httpContent;
   127         final String httpType;
   128         final String httpPath;
   129         final String[] parameters;
   130 
   131         Resource(InputStream httpContent, String httpType, String httpPath,
   132             String[] parameters
   133         ) {
   134             httpContent.mark(Integer.MAX_VALUE);
   135             this.httpContent = httpContent;
   136             this.httpType = httpType;
   137             this.httpPath = httpPath;
   138             this.parameters = parameters;
   139         }
   140     }
   141 }