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
jaroslav@370
     1
/**
jaroslav@370
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 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@667
    22
import java.util.ArrayList;
jaroslav@667
    23
import java.util.List;
jaroslav@371
    24
import java.util.concurrent.CountDownLatch;
jaroslav@371
    25
import java.util.concurrent.TimeUnit;
jaroslav@371
    26
jaroslav@622
    27
/** Represents individual method invocation, its context and its result.
jaroslav@370
    28
 *
jaroslav@370
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@370
    30
 */
jaroslav@622
    31
public final class InvocationContext {
jaroslav@371
    32
    final CountDownLatch wait = new CountDownLatch(1);
jaroslav@622
    33
    final Class<?> clazz;
jaroslav@370
    34
    final String methodName;
jaroslav@622
    35
    private final Launcher launcher;
jaroslav@371
    36
    private String result;
jaroslav@385
    37
    private Throwable exception;
jaroslav@622
    38
    String html;
jaroslav@1908
    39
    String[] args;
jaroslav@667
    40
    final List<Resource> resources = new ArrayList<>();
jaroslav@1860
    41
    private int time;
jaroslav@370
    42
jaroslav@622
    43
    InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
jaroslav@622
    44
        this.launcher = launcher;
jaroslav@622
    45
        this.clazz = clazz;
jaroslav@370
    46
        this.methodName = methodName;
jaroslav@622
    47
    }
jaroslav@622
    48
    
jaroslav@622
    49
    /** An HTML fragment to be available for the execution. Useful primarily when
jaroslav@622
    50
     * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
jaroslav@622
    51
     * @param html the html fragment
jaroslav@622
    52
     */
jaroslav@622
    53
    public void setHtmlFragment(String html) {
jaroslav@526
    54
        this.html = html;
jaroslav@370
    55
    }
jaroslav@1908
    56
jaroslav@1908
    57
    /** Arguments to pass to the invoked method.
jaroslav@1908
    58
     * @param args textual arguments to pass to the method
jaroslav@1908
    59
     * @since 0.18
jaroslav@1908
    60
     */
jaroslav@1908
    61
    public void setArguments(String... args) {
jaroslav@1908
    62
        this.args = args;
jaroslav@1908
    63
    }
jaroslav@371
    64
    
jaroslav@622
    65
    /** HTTP resource to be available during execution. An invocation may
jaroslav@622
    66
     * perform an HTTP query and obtain a resource relative to the page.
jaroslav@622
    67
     */
jaroslav@954
    68
    public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
jaroslav@954
    69
        if (relativePath == null || mimeType == null || content == null || parameters == null) {
jaroslav@623
    70
            throw new NullPointerException();
jaroslav@623
    71
        }
jaroslav@954
    72
        resources.add(new Resource(content, mimeType, relativePath, parameters));
jaroslav@622
    73
    }
jaroslav@622
    74
    
jaroslav@622
    75
    /** Invokes the associated method. 
jaroslav@622
    76
     * @return the textual result of the invocation
jaroslav@1860
    77
     * @throws java.io.IOException if execution fails
jaroslav@622
    78
     */
jaroslav@622
    79
    public String invoke() throws IOException {
jaroslav@622
    80
        launcher.runMethod(this);
jaroslav@622
    81
        return toString();
jaroslav@622
    82
    }
jaroslav@1860
    83
jaroslav@1860
    84
    /** Invokes the associated method.
jaroslav@1860
    85
     * @param time one element array to store the length of the invocation
jaroslav@1860
    86
     *    - can be <code>null</code>
jaroslav@1860
    87
     * @return the textual result of the invocation
jaroslav@1860
    88
     * @throws java.io.IOException if execution fails
jaroslav@1860
    89
     * @since 0.20
jaroslav@1860
    90
     */
jaroslav@1860
    91
    public String invoke(int[] time) throws IOException {
jaroslav@1860
    92
        launcher.runMethod(this);
jaroslav@1860
    93
        if (time != null) {
jaroslav@1860
    94
            time[0] = this.time;
jaroslav@1860
    95
        }
jaroslav@1860
    96
        return toString();
jaroslav@1860
    97
    }
jaroslav@622
    98
    
jaroslav@622
    99
    /** Obtains textual result of the invocation.
jaroslav@622
   100
     * @return text representing the exception or result value
jaroslav@622
   101
     */
jaroslav@622
   102
    @Override
jaroslav@622
   103
    public String toString() {
jaroslav@622
   104
        if (exception != null) {
jaroslav@622
   105
            return exception.toString();
jaroslav@622
   106
        }
jaroslav@622
   107
        return result;
jaroslav@622
   108
    }
jaroslav@622
   109
    
jaroslav@622
   110
    /**
jaroslav@622
   111
     * @param timeOut
jaroslav@622
   112
     * @throws InterruptedException 
jaroslav@622
   113
     */
jaroslav@371
   114
    void await(long timeOut) throws InterruptedException {
jaroslav@371
   115
        wait.await(timeOut, TimeUnit.MILLISECONDS);
jaroslav@371
   116
    }
jaroslav@371
   117
    
jaroslav@1860
   118
    void result(String r, int time, Throwable e) {
jaroslav@1860
   119
        this.time = time;
jaroslav@371
   120
        this.result = r;
jaroslav@371
   121
        this.exception = e;
jaroslav@371
   122
        wait.countDown();
jaroslav@371
   123
    }
jaroslav@370
   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
}