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
jaroslav@370
     1
/**
jaroslav@370
     2
 * Back 2 Browser Bytecode Translator
jaroslav@370
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@370
     4
 *
jaroslav@370
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@370
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@370
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@370
     8
 *
jaroslav@370
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@370
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@370
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@370
    12
 * GNU General Public License for more details.
jaroslav@370
    13
 *
jaroslav@370
    14
 * You should have received a copy of the GNU General Public License
jaroslav@370
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@370
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@370
    17
 */
jaroslav@370
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@370
    19
jaroslav@622
    20
import java.io.IOException;
jaroslav@626
    21
import java.io.InputStream;
jaroslav@1186
    22
import java.net.URI;
jaroslav@667
    23
import java.util.ArrayList;
jaroslav@667
    24
import java.util.List;
jaroslav@371
    25
import java.util.concurrent.CountDownLatch;
jaroslav@371
    26
import java.util.concurrent.TimeUnit;
jaroslav@371
    27
jaroslav@622
    28
/** Represents individual method invocation, its context and its result.
jaroslav@370
    29
 *
jaroslav@370
    30
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@370
    31
 */
jaroslav@622
    32
public final class InvocationContext {
jaroslav@371
    33
    final CountDownLatch wait = new CountDownLatch(1);
jaroslav@622
    34
    final Class<?> clazz;
jaroslav@370
    35
    final String methodName;
jaroslav@622
    36
    private final Launcher launcher;
jaroslav@371
    37
    private String result;
jaroslav@385
    38
    private Throwable exception;
jaroslav@622
    39
    String html;
jaroslav@667
    40
    final List<Resource> resources = new ArrayList<>();
jaroslav@370
    41
jaroslav@622
    42
    InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
jaroslav@622
    43
        this.launcher = launcher;
jaroslav@622
    44
        this.clazz = clazz;
jaroslav@370
    45
        this.methodName = methodName;
jaroslav@622
    46
    }
jaroslav@622
    47
    
jaroslav@622
    48
    /** An HTML fragment to be available for the execution. Useful primarily when
jaroslav@622
    49
     * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
jaroslav@622
    50
     * @param html the html fragment
jaroslav@622
    51
     */
jaroslav@622
    52
    public void setHtmlFragment(String html) {
jaroslav@526
    53
        this.html = html;
jaroslav@370
    54
    }
jaroslav@371
    55
    
jaroslav@622
    56
    /** HTTP resource to be available during execution. An invocation may
jaroslav@622
    57
     * perform an HTTP query and obtain a resource relative to the page.
jaroslav@622
    58
     */
jaroslav@954
    59
    public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
jaroslav@954
    60
        if (relativePath == null || mimeType == null || content == null || parameters == null) {
jaroslav@623
    61
            throw new NullPointerException();
jaroslav@623
    62
        }
jaroslav@954
    63
        resources.add(new Resource(content, mimeType, relativePath, parameters));
jaroslav@622
    64
    }
jaroslav@622
    65
    
jaroslav@622
    66
    /** Invokes the associated method. 
jaroslav@622
    67
     * @return the textual result of the invocation
jaroslav@622
    68
     */
jaroslav@622
    69
    public String invoke() throws IOException {
jaroslav@622
    70
        launcher.runMethod(this);
jaroslav@622
    71
        return toString();
jaroslav@622
    72
    }
jaroslav@622
    73
    
jaroslav@622
    74
    /** Obtains textual result of the invocation.
jaroslav@622
    75
     * @return text representing the exception or result value
jaroslav@622
    76
     */
jaroslav@622
    77
    @Override
jaroslav@622
    78
    public String toString() {
jaroslav@622
    79
        if (exception != null) {
jaroslav@622
    80
            return exception.toString();
jaroslav@622
    81
        }
jaroslav@622
    82
        return result;
jaroslav@622
    83
    }
jaroslav@622
    84
    
jaroslav@622
    85
    /**
jaroslav@622
    86
     * @param timeOut
jaroslav@622
    87
     * @throws InterruptedException 
jaroslav@622
    88
     */
jaroslav@371
    89
    void await(long timeOut) throws InterruptedException {
jaroslav@371
    90
        wait.await(timeOut, TimeUnit.MILLISECONDS);
jaroslav@371
    91
    }
jaroslav@371
    92
    
jaroslav@385
    93
    void result(String r, Throwable e) {
jaroslav@371
    94
        this.result = r;
jaroslav@371
    95
        this.exception = e;
jaroslav@371
    96
        wait.countDown();
jaroslav@371
    97
    }
jaroslav@370
    98
jaroslav@1186
    99
    private static RegisterResource RR;
jaroslav@1186
   100
    static void register(RegisterResource rr) {
jaroslav@1186
   101
        RR = rr;
jaroslav@1186
   102
    }
jaroslav@1186
   103
    /** A running {@link InvocationContext test} can register additional 
jaroslav@1186
   104
     * resources to the associated browser (if any). 
jaroslav@1186
   105
     * 
jaroslav@1186
   106
     * @param content the stream to deliver content from
jaroslav@1186
   107
     * @param mimeType mime type for the HTTP reply
jaroslav@1186
   108
     * @param path suggested path in the server to use
jaroslav@1186
   109
     * @param parameters additional parameters
jaroslav@1186
   110
     * @return the URI the resource is made available at
jaroslav@1186
   111
     * @throws NullPointerException if there is no {@link InvocationContext test} 
jaroslav@1186
   112
     *    currently running
jaroslav@1186
   113
     * @since 0.8
jaroslav@1186
   114
     */
jaroslav@1186
   115
    public static URI register(
jaroslav@1186
   116
        InputStream content, String mimeType, String path, String[] parameters
jaroslav@1186
   117
    ) {
jaroslav@1186
   118
        final Resource r = new Resource(content, mimeType, path, parameters);
jaroslav@1186
   119
        return RR.registerResource(r);
jaroslav@1186
   120
    }
jaroslav@1186
   121
    static interface RegisterResource {
jaroslav@1186
   122
        public URI registerResource(Resource r);
jaroslav@1186
   123
    }
jaroslav@667
   124
jaroslav@667
   125
    static final class Resource {
jaroslav@667
   126
        final InputStream httpContent;
jaroslav@667
   127
        final String httpType;
jaroslav@667
   128
        final String httpPath;
jaroslav@954
   129
        final String[] parameters;
jaroslav@667
   130
jaroslav@954
   131
        Resource(InputStream httpContent, String httpType, String httpPath,
jaroslav@954
   132
            String[] parameters
jaroslav@954
   133
        ) {
jaroslav@946
   134
            httpContent.mark(Integer.MAX_VALUE);
jaroslav@667
   135
            this.httpContent = httpContent;
jaroslav@667
   136
            this.httpType = httpType;
jaroslav@667
   137
            this.httpPath = httpPath;
jaroslav@954
   138
            this.parameters = parameters;
jaroslav@667
   139
        }
jaroslav@667
   140
    }
jaroslav@370
   141
}