rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 17 Apr 2013 17:04:40 +0200
branchfx
changeset 1004 04efef2a9c1e
parent 946 986cb1517ead
permissions -rw-r--r--
Rather than piggybacking on first alert call, use the fact that the server and FX Web View are in the same VM and notify the view that bck2brwsr.js is about to be served from the server.
     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.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 
    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 addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
    59         if (relativePath == null || mimeType == null || content == null || parameters == null) {
    60             throw new NullPointerException();
    61         }
    62         resources.add(new Resource(content, mimeType, relativePath, parameters));
    63     }
    64     
    65     /** Invokes the associated method. 
    66      * @return the textual result of the invocation
    67      */
    68     public String invoke() throws IOException {
    69         launcher.runMethod(this);
    70         return toString();
    71     }
    72     
    73     /** Obtains textual result of the invocation.
    74      * @return text representing the exception or result value
    75      */
    76     @Override
    77     public String toString() {
    78         if (exception != null) {
    79             return exception.toString();
    80         }
    81         return result;
    82     }
    83     
    84     /**
    85      * @param timeOut
    86      * @throws InterruptedException 
    87      */
    88     void await(long timeOut) throws InterruptedException {
    89         wait.await(timeOut, TimeUnit.MILLISECONDS);
    90     }
    91     
    92     void result(String r, Throwable e) {
    93         this.result = r;
    94         this.exception = e;
    95         wait.countDown();
    96     }
    97 
    98 
    99     static final class Resource {
   100         final InputStream httpContent;
   101         final String httpType;
   102         final String httpPath;
   103         final String[] parameters;
   104 
   105         Resource(InputStream httpContent, String httpType, String httpPath,
   106             String[] parameters
   107         ) {
   108             httpContent.mark(Integer.MAX_VALUE);
   109             this.httpContent = httpContent;
   110             this.httpType = httpType;
   111             this.httpPath = httpPath;
   112             this.parameters = parameters;
   113         }
   114     }
   115 }