boot/src/main/java/org/netbeans/html/boot/spi/Fn.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 12 Dec 2014 11:22:40 +0100
branchgc
changeset 900 2ee22312e414
parent 838 bdc3d696dd4a
child 902 5c65f811cf55
permissions -rw-r--r--
Giving API users better control over GC aspects of their objects
jaroslav@123
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@123
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@123
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@123
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@123
    42
 */
jtulach@838
    43
package org.netbeans.html.boot.spi;
jaroslav@123
    44
jaroslav@322
    45
import java.io.Closeable;
jtulach@680
    46
import java.io.IOException;
jaroslav@349
    47
import java.io.InputStream;
jaroslav@349
    48
import java.io.InputStreamReader;
jaroslav@163
    49
import java.io.Reader;
jaroslav@123
    50
import java.net.URL;
jaroslav@349
    51
import java.util.HashMap;
jaroslav@349
    52
import java.util.HashSet;
jaroslav@349
    53
import java.util.Map;
jaroslav@349
    54
import java.util.Set;
jaroslav@431
    55
import java.util.concurrent.Executor;
jaroslav@322
    56
import net.java.html.js.JavaScriptBody;
jaroslav@362
    57
import org.netbeans.html.boot.impl.FnContext;
jaroslav@123
    58
jaroslav@289
    59
/** Represents single JavaScript function that can be invoked. 
jaroslav@289
    60
 * Created via {@link Presenter#defineFn(java.lang.String, java.lang.String...)}.
jaroslav@123
    61
 *
jtulach@655
    62
 * @author Jaroslav Tulach
jaroslav@123
    63
 */
jaroslav@123
    64
public abstract class Fn {
jaroslav@288
    65
    private final Presenter presenter;
jaroslav@288
    66
    
jaroslav@289
    67
    /**
jaroslav@289
    68
     * @deprecated Ineffective as of 0.6. 
jtulach@838
    69
     * Provide a presenter via {@link #Fn(org.netbeans.html.boot.spi.Fn.Presenter)}
jaroslav@289
    70
     * constructor
jaroslav@289
    71
     */
jaroslav@289
    72
    @Deprecated
jaroslav@289
    73
    protected Fn() {
jaroslav@289
    74
        this(null);
jaroslav@289
    75
    }
jaroslav@289
    76
    
jaroslav@289
    77
    /** Creates new function object and associates it with given presenter.
jaroslav@289
    78
     * 
jaroslav@289
    79
     * @param presenter the browser presenter associated with this function
jaroslav@289
    80
     * @since 0.6 
jaroslav@289
    81
     */
jaroslav@288
    82
    protected Fn(Presenter presenter) {
jaroslav@288
    83
        this.presenter = presenter;
jaroslav@288
    84
    }
jaroslav@289
    85
jaroslav@289
    86
    /** True, if currently active presenter is the same as presenter this
jtulach@838
    87
     * function has been created for via {@link #Fn(org.netbeans.html.boot.spi.Fn.Presenter)}.
jaroslav@289
    88
     * 
jaroslav@289
    89
     * @return true, if proper presenter is used
jaroslav@289
    90
     */
jaroslav@288
    91
    public final boolean isValid() {
jaroslav@451
    92
        return presenter != null && FnContext.currentPresenter(false) == presenter;
jaroslav@288
    93
    }
jaroslav@288
    94
    
jaroslav@323
    95
    /** Helper method to check if the provided instance is valid function.
jaroslav@323
    96
     * Checks if the parameter is non-null and if so, does {@link #isValid()}
jaroslav@323
    97
     * check.
jaroslav@323
    98
     * 
jaroslav@323
    99
     * @param fnOrNull function or <code>null</code>
jaroslav@323
   100
     * @return true if the parameter is non-null and valid
jaroslav@323
   101
     * @since 0.7
jaroslav@323
   102
     */
jaroslav@323
   103
    public static boolean isValid(Fn fnOrNull) {
jaroslav@323
   104
        return fnOrNull != null && fnOrNull.isValid();
jaroslav@323
   105
    }
jaroslav@323
   106
jaroslav@323
   107
    /** Helper method to find current presenter and ask it to define new
jaroslav@323
   108
     * function by calling {@link Presenter#defineFn(java.lang.String, java.lang.String...)}.
jaroslav@323
   109
     * 
jaroslav@323
   110
     * @param caller the class who wishes to define the function
jaroslav@323
   111
     * @param code the body of the function (can reference <code>this</code> and <code>names</code> variables)
jaroslav@323
   112
     * @param names names of individual parameters
jaroslav@323
   113
     * @return the function object that can be {@link Fn#invoke(java.lang.Object, java.lang.Object...) invoked}
jaroslav@451
   114
     *    - can return <code>null</code> if there is {@link #activePresenter() no presenter}
jaroslav@323
   115
     * @since 0.7
jaroslav@323
   116
     */
jaroslav@323
   117
    public static Fn define(Class<?> caller, String code, String... names) {
jtulach@900
   118
        return define(caller, false, code, names);
jtulach@900
   119
    }
jtulach@900
   120
jtulach@900
   121
    /** Helper method to find current presenter and ask it to define new
jtulach@900
   122
     * function.
jtulach@900
   123
     * 
jtulach@900
   124
     * @param caller the class who wishes to define the function
jtulach@900
   125
     * @param keepParametersAlive whether Java parameters should survive in JavaScript
jtulach@900
   126
     *   after the method invocation is over
jtulach@900
   127
     * @param code the body of the function (can reference <code>this</code> and <code>names</code> variables)
jtulach@900
   128
     * @param names names of individual parameters
jtulach@900
   129
     * @return the function object that can be {@link Fn#invoke(java.lang.Object, java.lang.Object...) invoked}
jtulach@900
   130
     *    - can return <code>null</code> if there is {@link #activePresenter() no presenter}
jtulach@900
   131
     * @since 1.1
jtulach@900
   132
     */
jtulach@900
   133
    public static Fn define(Class<?> caller, boolean keepParametersAlive, String code, String... names) {
jaroslav@451
   134
        final Presenter p = FnContext.currentPresenter(false);
jtulach@900
   135
        if (p == null) {
jtulach@900
   136
            return null;
jtulach@900
   137
        }
jtulach@900
   138
        if (p instanceof KeepAlive) {
jtulach@900
   139
            boolean[] arr;
jtulach@900
   140
            if (!keepParametersAlive) {
jtulach@900
   141
                arr = new boolean[names.length];
jtulach@900
   142
                for (int i = 0; i < arr.length; i++) {
jtulach@900
   143
                    arr[i] = false;
jtulach@900
   144
                }
jtulach@900
   145
            } else {
jtulach@900
   146
                arr = null;
jtulach@900
   147
            }
jtulach@900
   148
            return ((KeepAlive)p).defineFn(code, names, arr);
jtulach@900
   149
        }
jtulach@900
   150
        return p.defineFn(code, names);
jaroslav@323
   151
    }
jaroslav@323
   152
    
jaroslav@349
   153
    private static final Map<String,Set<Presenter>> LOADED = new HashMap<String, Set<Presenter>>();
jaroslav@427
   154
    
jaroslav@427
   155
    /** Wraps function to ensure that the script represented by <code>resource</code>
jaroslav@427
   156
     * gets loaded into the browser environment before the function <code>fn</code>
jaroslav@427
   157
     * is executed.
jaroslav@427
   158
     * 
jaroslav@560
   159
     * @param fn original function to call (if <code>null</code> returns <code>null</code>)
jaroslav@427
   160
     * @param caller the class who wishes to define/call the function
jaroslav@427
   161
     * @param resource resources (accessible via {@link ClassLoader#getResource(java.lang.String)}) 
jaroslav@427
   162
     *   with a <em>JavaScript</em> that is supposed to loaded into the browser
jaroslav@427
   163
     *   environment
jaroslav@427
   164
     * @return function that ensures the script is loaded and then delegates
jaroslav@560
   165
     *   to <code>fn</code>. Returns <code>null</code> if the input <code>fn</code> is null
jaroslav@427
   166
     * @since 0.7
jaroslav@427
   167
     */
jaroslav@349
   168
    public static Fn preload(final Fn fn, final Class<?> caller, final String resource) {
jaroslav@560
   169
        if (fn == null) {
jaroslav@560
   170
            return null;
jaroslav@560
   171
        }
jaroslav@555
   172
        return new Fn(fn.presenter()) {
jaroslav@349
   173
            @Override
jaroslav@349
   174
            public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@577
   175
                loadResource();
jaroslav@577
   176
                return fn.invoke(thiz, args);
jaroslav@577
   177
            }
jaroslav@577
   178
jaroslav@577
   179
            @Override
jaroslav@577
   180
            public void invokeLater(Object thiz, Object... args) throws Exception {
jaroslav@577
   181
                loadResource();
jaroslav@577
   182
                fn.invokeLater(thiz, args);
jaroslav@577
   183
            }
jaroslav@577
   184
            
jaroslav@577
   185
            private void loadResource() throws Exception {
jaroslav@560
   186
                Presenter p = presenter();
jaroslav@560
   187
                if (p == null) {
jaroslav@560
   188
                    p = FnContext.currentPresenter(false);
jaroslav@349
   189
                }
jaroslav@560
   190
                if (p != null) {
jaroslav@560
   191
                    Set<Presenter> there = LOADED.get(resource);
jaroslav@560
   192
                    if (there == null) {
jaroslav@560
   193
                        there = new HashSet<Presenter>();
jaroslav@560
   194
                        LOADED.put(resource, there);
jaroslav@560
   195
                    }
jaroslav@560
   196
                    if (there.add(p)) {
jtulach@680
   197
                        final ClassLoader l = caller.getClassLoader();
jtulach@680
   198
                        InputStream is = l.getResourceAsStream(resource);
jtulach@680
   199
                        if (is == null && resource.startsWith("/")) {
jtulach@680
   200
                            is = l.getResourceAsStream(resource.substring(1));
jtulach@680
   201
                        }
jtulach@680
   202
                        if (is == null) {
jtulach@680
   203
                            throw new IOException("Cannot find " + resource + " in " + l);
jtulach@680
   204
                        }
jaroslav@560
   205
                        try {
jaroslav@560
   206
                            InputStreamReader r = new InputStreamReader(is, "UTF-8");
jaroslav@560
   207
                            p.loadScript(r);
jaroslav@560
   208
                        } finally {
jaroslav@560
   209
                            is.close();
jaroslav@560
   210
                        }
jaroslav@349
   211
                    }
jaroslav@349
   212
                }
jaroslav@349
   213
            }
jaroslav@349
   214
        };
jaroslav@349
   215
    }
jaroslav@577
   216
jaroslav@349
   217
    
jaroslav@322
   218
    /** The currently active presenter.
jaroslav@322
   219
     * 
jaroslav@322
   220
     * @return the currently active presenter or <code>null</code>
jaroslav@322
   221
     * @since 0.7
jaroslav@322
   222
     */
jaroslav@322
   223
    public static Presenter activePresenter() {
jaroslav@434
   224
        return FnContext.currentPresenter(false);
jaroslav@322
   225
    }
jaroslav@322
   226
    
jaroslav@716
   227
    /** Activates given presenter. Used to associate the native 
jaroslav@716
   228
     * JavaScript code specified by 
jaroslav@716
   229
     * {@link JavaScriptBody} annotation with certain presenter:
jaroslav@322
   230
     * <pre>
jaroslav@322
   231
     * try ({@link Closeable} c = Fn.activate(presenter)) {
jaroslav@322
   232
     *   doCallsInPresenterContext();
jaroslav@322
   233
     * }
jaroslav@322
   234
     * </pre>
jaroslav@322
   235
     * 
jaroslav@322
   236
     * @param p the presenter that should be active until closable is closed
jaroslav@322
   237
     * @return the closable to close
jaroslav@322
   238
     * @since 0.7
jaroslav@322
   239
     */
jaroslav@322
   240
    public static Closeable activate(Presenter p) {
jaroslav@322
   241
        return FnContext.activate(p);
jaroslav@322
   242
    }
jaroslav@322
   243
    
jaroslav@289
   244
    /** Invokes the defined function with specified <code>this</code> and
jaroslav@289
   245
     * appropriate arguments.
jaroslav@289
   246
     * 
jaroslav@289
   247
     * @param thiz the meaning of <code>this</code> inside of the JavaScript
jaroslav@289
   248
     *   function - can be <code>null</code>
jaroslav@289
   249
     * @param args arguments for the function
jaroslav@289
   250
     * @return return value from the function
jaroslav@289
   251
     * @throws Exception if something goes wrong, as exception may be thrown
jaroslav@289
   252
     */
jaroslav@289
   253
    public abstract Object invoke(Object thiz, Object... args) throws Exception;
jaroslav@593
   254
jaroslav@593
   255
    /** Invokes the defined function with specified <code>this</code> and
jaroslav@593
   256
     * appropriate arguments asynchronously. The invocation may be 
jaroslav@593
   257
     * happen <em>"later"</em>.
jaroslav@593
   258
     * 
jaroslav@593
   259
     * @param thiz the meaning of <code>this</code> inside of the JavaScript
jaroslav@593
   260
     *   function - can be <code>null</code>
jaroslav@593
   261
     * @param args arguments for the function
jaroslav@593
   262
     * @throws Exception if something goes wrong, as exception may be thrown
jaroslav@593
   263
     * @since 0.7.6
jaroslav@593
   264
     */
jaroslav@593
   265
    public void invokeLater(Object thiz, Object... args) throws Exception {
jaroslav@593
   266
        invoke(this, args);
jaroslav@593
   267
    }
jaroslav@429
   268
    
jaroslav@429
   269
    /** Provides the function implementation access to the presenter provided
jtulach@838
   270
     * in {@link #Fn(org.netbeans.html.boot.spi.Fn.Presenter) the constructor}.
jaroslav@429
   271
     * 
jaroslav@530
   272
     * @return presenter passed in the constructor (may be, but should not be <code>null</code>)
jaroslav@429
   273
     * @since 0.7
jaroslav@429
   274
     */
jaroslav@429
   275
    protected final Presenter presenter() {
jaroslav@429
   276
        return presenter;
jaroslav@429
   277
    }
jtulach@900
   278
    
jaroslav@289
   279
    /** The representation of a <em>presenter</em> - usually a browser window.
jaroslav@315
   280
     * Should be provided by a library included in the application and registered
jaroslav@315
   281
     * in <code>META-INF/services</code>, for example with
jaroslav@315
   282
     * <code>@ServiceProvider(service = Fn.Presenter.class)</code> annotation.
jaroslav@431
   283
     * <p>
jaroslav@431
   284
     * Since 0.7 a presenter may implement {@link Executor} interface, in case
jaroslav@431
   285
     * it supports single threaded execution environment. The executor's
jaroslav@431
   286
     * {@link Executor#execute(java.lang.Runnable)} method is then supposed
jaroslav@431
   287
     * to invoke the runnable immediately (in case we are on the right thread
jaroslav@431
   288
     * already) or return and asynchronously invoke the runnable later on the
jaroslav@431
   289
     * right thread (if we are on wrong thread).
jaroslav@289
   290
     */
jaroslav@127
   291
    public interface Presenter {
jaroslav@289
   292
        /** Creates new function with given parameter names and provided body.
jaroslav@289
   293
         * 
jaroslav@289
   294
         * @param code the body of the function. Can refer to variables named
jaroslav@289
   295
         *   as <code>names</code>
jaroslav@289
   296
         * @param names names of parameters of the function - these will be 
jaroslav@289
   297
         *   available when the <code>code</code> body executes
jaroslav@289
   298
         * 
jaroslav@289
   299
         * @return function that can be later invoked
jaroslav@289
   300
         */
jaroslav@127
   301
        public Fn defineFn(String code, String... names);
jaroslav@289
   302
        
jaroslav@289
   303
        /** Opens the browser, loads provided page and when the
jaroslav@289
   304
         * page is ready, it calls back to the provider runnable.
jaroslav@289
   305
         * 
jaroslav@289
   306
         * @param page the URL for the page to display
jaroslav@289
   307
         * @param onPageLoad callback when the page is ready
jaroslav@289
   308
         */
jaroslav@128
   309
        public void displayPage(URL page, Runnable onPageLoad);
jaroslav@289
   310
        
jaroslav@289
   311
        /** Loads a script into the browser JavaScript interpreter and 
jaroslav@289
   312
         * executes it.
jaroslav@289
   313
         * @param code the script to execute
jaroslav@289
   314
         * @throws Exception if something goes wrong, throw an exception
jaroslav@289
   315
         */
jaroslav@163
   316
        public void loadScript(Reader code) throws Exception;
jaroslav@123
   317
    }
jaroslav@430
   318
    
jaroslav@430
   319
    /** Additional interface to be implemented by {@link Presenter}s that
jaroslav@430
   320
     * wish to control what objects are passed into the JavaScript virtual 
jaroslav@430
   321
     * machine.
jaroslav@430
   322
     * <p>
jaroslav@430
   323
     * If a JavaScript engine makes callback to Java method that returns 
jaroslav@430
   324
     * a value, the {@link #toJavaScript(java.lang.Object)} method is
jaroslav@430
   325
     * consulted to convert the Java value to something reasonable inside
jaroslav@430
   326
     * JavaScript VM.
jaroslav@430
   327
     * <p>
jaroslav@430
   328
     * <em>Note:</em> The implementation based on <em>JavaFX</em> <code>WebView</code>
jaroslav@430
   329
     * uses this interface to convert Java arrays to JavaScript ones.
jaroslav@430
   330
     * 
jaroslav@430
   331
     * @see Presenter
jaroslav@430
   332
     * @since 0.7
jaroslav@430
   333
     */
jaroslav@430
   334
    public interface ToJavaScript {
jaroslav@430
   335
        /** Convert a Java return value into some object suitable for
jaroslav@430
   336
         * JavaScript virtual machine.
jaroslav@430
   337
         * 
jaroslav@430
   338
         * @param toReturn the Java object to be returned
jaroslav@430
   339
         * @return the replacement value to return instead
jaroslav@430
   340
         */
jaroslav@430
   341
        public Object toJavaScript(Object toReturn);
jaroslav@430
   342
    }
jaroslav@446
   343
    
jaroslav@446
   344
    /** Additional interface to be implemented by {@link Presenter}s that
jaroslav@446
   345
     * need to convert JavaScript object (usually array) to Java object 
jaroslav@446
   346
     * when calling back from JavaScript to Java.
jaroslav@446
   347
     * <p>
jaroslav@446
   348
     * <em>Note:</em> The implementation based on <em>JavaFX</em>
jaroslav@446
   349
     * <code>WebView</code> uses this interface to convert JavaScript arrays to
jaroslav@446
   350
     * Java ones.
jaroslav@446
   351
      * 
jaroslav@446
   352
     * @since 0.7
jaroslav@446
   353
     */
jaroslav@446
   354
    public interface FromJavaScript {
jaroslav@446
   355
        /** Convert a JavaScript object into suitable Java representation
jaroslav@446
   356
         * before a Java method is called with this object as an argument.
jaroslav@446
   357
         * 
jaroslav@446
   358
         * @param js the JavaScript object
jaroslav@446
   359
         * @return replacement object for 
jaroslav@446
   360
         */
jaroslav@446
   361
        public Object toJava(Object js);
jaroslav@446
   362
    }
jtulach@900
   363
jtulach@900
   364
    /** Additional interface to {@link Presenter} to control more precisely
jtulach@900
   365
     * garbage collection behavior of individual parameters. See 
jtulach@900
   366
     * {@link JavaScriptBody#keepAlive()} attribute for description of the
jtulach@900
   367
     * actual behavior of the interface.
jtulach@900
   368
     * 
jtulach@900
   369
     * @since 1.1
jtulach@900
   370
     */
jtulach@900
   371
    public interface KeepAlive {
jtulach@900
   372
        /** Creates new function with given parameter names and provided body.
jtulach@900
   373
         * 
jtulach@900
   374
         * @param code the body of the function. Can refer to variables named
jtulach@900
   375
         *   as <code>names</code>
jtulach@900
   376
         * @param names names of parameters of the function - these will be 
jtulach@900
   377
         *   available when the <code>code</code> body executes
jtulach@900
   378
         * @param keepAlive array of booleans describing for each parameter
jtulach@900
   379
         *   whether it should be kept alive or not. Length of the array
jtulach@900
   380
         *   must be the same as length of <code>names</code> array. The
jtulach@900
   381
         *   array may be <code>null</code> to signal that all parameters
jtulach@900
   382
         *   should be <em>kept alive</em>.
jtulach@900
   383
         * 
jtulach@900
   384
         * @return function that can be later invoked
jtulach@900
   385
         */
jtulach@900
   386
        public Fn defineFn(String code, String[] names, boolean[] keepAlive);
jtulach@900
   387
    }
jaroslav@123
   388
}