launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 21 Jun 2013 22:34:09 +0200
branchclassloader
changeset 1186 265edcee24ed
parent 1033 b8773b7b9ecd
child 1188 4d324bf6ede9
permissions -rw-r--r--
Dynamic way of registering Http resources
     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.net.URI;
    23 import java.util.ArrayList;
    24 import java.util.List;
    25 import java.util.concurrent.CountDownLatch;
    26 import java.util.concurrent.TimeUnit;
    27 
    28 /** Represents individual method invocation, its context and its result.
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public final class InvocationContext {
    33     final CountDownLatch wait = new CountDownLatch(1);
    34     final Class<?> clazz;
    35     final String methodName;
    36     private final Launcher launcher;
    37     private String result;
    38     private Throwable exception;
    39     String html;
    40     final List<Resource> resources = new ArrayList<>();
    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      */
    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     private static RegisterResource RR;
   100     static void register(RegisterResource rr) {
   101         RR = rr;
   102     }
   103     /** A running {@link InvocationContext test} can register additional 
   104      * resources to the associated browser (if any). 
   105      * 
   106      * @param content the stream to deliver content from
   107      * @param mimeType mime type for the HTTP reply
   108      * @param path suggested path in the server to use
   109      * @param parameters additional parameters
   110      * @return the URI the resource is made available at
   111      * @throws NullPointerException if there is no {@link InvocationContext test} 
   112      *    currently running
   113      * @since 0.8
   114      */
   115     public static URI register(
   116         InputStream content, String mimeType, String path, String[] parameters
   117     ) {
   118         final Resource r = new Resource(content, mimeType, path, parameters);
   119         return RR.registerResource(r);
   120     }
   121     static interface RegisterResource {
   122         public URI registerResource(Resource r);
   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 }