boot/src/main/java/net/java/html/boot/BrowserBuilder.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 771 ee3614350fc8
child 834 5b6567dcc966
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
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
 */
jaroslav@123
    43
package net.java.html.boot;
jaroslav@123
    44
jaroslav@202
    45
import java.io.File;
jtulach@653
    46
import java.io.FileNotFoundException;
jaroslav@127
    47
import java.io.IOException;
jtulach@653
    48
import java.io.InputStream;
jaroslav@156
    49
import java.lang.reflect.Method;
jtulach@653
    50
import java.net.JarURLConnection;
jaroslav@148
    51
import java.net.MalformedURLException;
jaroslav@127
    52
import java.net.URL;
jtulach@653
    53
import java.net.URLConnection;
jtulach@653
    54
import java.security.ProtectionDomain;
jaroslav@156
    55
import java.util.Arrays;
jaroslav@127
    56
import java.util.Collection;
jaroslav@127
    57
import java.util.Enumeration;
jaroslav@127
    58
import java.util.ServiceLoader;
jaroslav@574
    59
import java.util.concurrent.Executor;
jaroslav@156
    60
import java.util.logging.Level;
jaroslav@156
    61
import java.util.logging.Logger;
jtulach@569
    62
import net.java.html.BrwsrCtx;
jaroslav@166
    63
import net.java.html.js.JavaScriptBody;
jaroslav@127
    64
import org.apidesign.html.boot.spi.Fn;
jaroslav@574
    65
import org.apidesign.html.context.spi.Contexts;
jaroslav@362
    66
import org.netbeans.html.boot.impl.FindResources;
jaroslav@362
    67
import org.netbeans.html.boot.impl.FnContext;
jaroslav@509
    68
import org.netbeans.html.boot.impl.FnUtils;
jaroslav@127
    69
jaroslav@166
    70
/** Use this builder to launch your Java/HTML based application. Typical
jaroslav@166
    71
 * usage in a main method of your application looks like this: 
jaroslav@166
    72
 * <pre>
jaroslav@166
    73
 * 
jaroslav@166
    74
 * <b>public static void</b> <em>main</em>(String... args) {
jtulach@700
    75
 *     BrowserBuilder.{@link #newBrowser newBrowser()}.
jaroslav@166
    76
 *          {@link #loadClass(java.lang.Class) loadClass(YourMain.class)}.
jaroslav@166
    77
 *          {@link #loadPage(java.lang.String) loadPage("index.html")}.
jaroslav@166
    78
 *          {@link #invoke(java.lang.String, java.lang.String[]) invoke("initialized", args)}.
jaroslav@166
    79
 *          {@link #showAndWait()};
jaroslav@166
    80
 *     System.exit(0);
jaroslav@166
    81
 * }
jaroslav@166
    82
 * </pre>
jaroslav@166
    83
 * The above will load <code>YourMain</code> class via
jaroslav@166
    84
 * a special classloader, it will locate an <code>index.html</code> (relative
jaroslav@166
    85
 * to <code>YourMain</code> class) and show it in a browser window. When the
jaroslav@166
    86
 * initialization is over, a <b>public static</b> method <em>initialized</em>
jaroslav@166
    87
 * in <code>YourMain</code> will be called with provided string parameters.
jaroslav@166
    88
 * <p>
jaroslav@166
    89
 * This module provides only API for building browsers. To use it properly one
jaroslav@166
    90
 * also needs an implementation on the classpath of one's application. For example
jaroslav@166
    91
 * use: <pre>
jaroslav@166
    92
 * &lt;dependency&gt;
jaroslav@361
    93
 *   &lt;groupId&gt;org.netbeans.html&lt;/groupId&gt;
jaroslav@361
    94
 *   &lt;artifactId&gt;net.java.html.boot.fx&lt;/artifactId&gt;
jaroslav@166
    95
 *   &lt;scope&gt;runtime&lt;/scope&gt;
jaroslav@166
    96
 * &lt;/dependency&gt;
jaroslav@166
    97
 * </pre>
jaroslav@123
    98
 *
jtulach@790
    99
 * @author Jaroslav Tulach
jaroslav@123
   100
 */
jaroslav@123
   101
public final class BrowserBuilder {
jaroslav@156
   102
    private static final Logger LOG = Logger.getLogger(BrowserBuilder.class.getName());
jaroslav@156
   103
    
jaroslav@127
   104
    private String resource;
jaroslav@127
   105
    private Class<?> clazz;
jaroslav@146
   106
    private Class[] browserClass;
jaroslav@146
   107
    private Runnable onLoad;
jaroslav@156
   108
    private String methodName;
jaroslav@156
   109
    private String[] methodArgs;
jaroslav@288
   110
    private final Object[] context;
jtulach@771
   111
    private ClassLoader loader;
jaroslav@156
   112
    
jaroslav@288
   113
    private BrowserBuilder(Object[] context) {
jaroslav@288
   114
        this.context = context;
jaroslav@123
   115
    }
jaroslav@166
   116
jaroslav@166
   117
    /** Entry method to obtain a new browser builder. Follow by calling 
jaroslav@166
   118
     * its instance methods like {@link #loadClass(java.lang.Class)} and
jaroslav@166
   119
     * {@link #loadPage(java.lang.String)}.
jaroslav@166
   120
     * 
jaroslav@288
   121
     * @param context any instances that should be available to the builder -
jaroslav@458
   122
     *   implementation dependant
jaroslav@166
   123
     * @return new browser builder
jaroslav@166
   124
     */
jaroslav@288
   125
    public static BrowserBuilder newBrowser(Object... context) {
jaroslav@288
   126
        return new BrowserBuilder(context);
jaroslav@123
   127
    }
jaroslav@123
   128
    
jaroslav@166
   129
    /** The class to load when the browser is initialized. This class
jaroslav@166
   130
     * is loaded by a special classloader (that supports {@link JavaScriptBody}
jaroslav@166
   131
     * and co.). 
jaroslav@166
   132
     * 
jaroslav@166
   133
     * @param mainClass the class to load and resolve when the browser is ready
jaroslav@166
   134
     * @return this builder
jaroslav@166
   135
     */
jaroslav@127
   136
    public BrowserBuilder loadClass(Class<?> mainClass) {
jaroslav@127
   137
        this.clazz = mainClass;
jaroslav@123
   138
        return this;
jaroslav@123
   139
    }
jtulach@656
   140
    
jtulach@656
   141
    /** Allows one to specify a runnable that should be invoked when a load
jtulach@656
   142
     * of a page is finished. This method may be used in addition or instead
jtulach@656
   143
     * of {@link #loadClass(java.lang.Class)} and 
jtulach@656
   144
     * {@link #invoke(java.lang.String, java.lang.String...)} methods.
jtulach@656
   145
     * 
jtulach@656
   146
     * @param r the code to run when the page is loaded
jtulach@656
   147
     * @return this builder
jtulach@656
   148
     * @since 0.8.1
jtulach@656
   149
     */
jtulach@656
   150
    public BrowserBuilder loadFinished(Runnable r) {
jtulach@656
   151
        this.onLoad = r;
jtulach@656
   152
        return this;
jtulach@656
   153
    }
jaroslav@156
   154
jaroslav@166
   155
    /** Page to load into the browser. If the <code>page</code> represents
jaroslav@166
   156
     * a {@link URL} known to the Java system, the URL is passed to the browser. 
jaroslav@202
   157
     * If system property <code>browser.rootdir</code> is specified, then a
jaroslav@202
   158
     * file <code>page</code> relative to this directory is used as the URL.
jaroslav@202
   159
     * If no such file exists, the system seeks for the 
jaroslav@202
   160
     * resource via {@link Class#getResource(java.lang.String)}
jaroslav@202
   161
     * method (relative to the {@link #loadClass(java.lang.Class) specified class}). 
jaroslav@202
   162
     * If such resource is not found, a file relative to the location JAR
jaroslav@202
   163
     * that contains the {@link #loadClass(java.lang.Class) main class} is 
jaroslav@202
   164
     * searched for.
jaroslav@166
   165
     * 
jaroslav@166
   166
     * @param page the location (relative, absolute, or URL) of a page to load
jaroslav@166
   167
     * @return this browser
jaroslav@166
   168
     */
jaroslav@166
   169
    public BrowserBuilder loadPage(String page) {
jaroslav@166
   170
        this.resource = page;
jaroslav@166
   171
        return this;
jaroslav@166
   172
    }
jaroslav@166
   173
    
jaroslav@166
   174
    /** Specifies callback method to notify the application that the browser is ready.
jaroslav@166
   175
     * There should be a <b>public static</b> method in the class specified
jaroslav@166
   176
     * by {@link #loadClass(java.lang.Class)} which takes an array of {@link String}
jaroslav@166
   177
     * argument. The method is called on the browser dispatch thread one
jaroslav@166
   178
     * the browser finishes loading of the {@link #loadPage(java.lang.String) HTML page}.
jaroslav@166
   179
     * 
jaroslav@166
   180
     * @param methodName name of a method to seek for
jaroslav@166
   181
     * @param args parameters to pass to the method
jaroslav@166
   182
     * @return this builder
jaroslav@166
   183
     */
jaroslav@156
   184
    public BrowserBuilder invoke(String methodName, String... args) {
jaroslav@156
   185
        this.methodName = methodName;
jaroslav@156
   186
        this.methodArgs = args;
jaroslav@156
   187
        return this;
jaroslav@156
   188
    }
jaroslav@166
   189
jtulach@771
   190
    /** Loader to use when searching for classes to initialize. 
jtulach@771
   191
     * If specified, this loader is going to be used to load {@link Fn.Presenter}
jtulach@771
   192
     * and {@link Contexts#fillInByProviders(java.lang.Class, org.apidesign.html.context.spi.Contexts.Builder) fill} {@link BrwsrCtx} in.
jtulach@771
   193
     * Specifying special classloader may be useful in modular systems, 
jtulach@771
   194
     * like OSGi, where one needs to load classes from many otherwise independent
jtulach@771
   195
     * modules.
jtulach@771
   196
     * 
jtulach@771
   197
     * @param l the loader to use (or <code>null</code>)
jtulach@771
   198
     * @return this builder
jtulach@771
   199
     * @since 0.9
jtulach@771
   200
     */
jtulach@771
   201
    public BrowserBuilder classloader(ClassLoader l) {
jtulach@771
   202
        this.loader = l;
jtulach@771
   203
        return this;
jtulach@771
   204
    }
jtulach@771
   205
jaroslav@166
   206
    /** Shows the browser, loads specified page in and executes the 
jaroslav@166
   207
     * {@link #invoke(java.lang.String, java.lang.String[]) initialization method}.
jaroslav@166
   208
     * The method returns when the browser is closed.
jaroslav@166
   209
     * 
jaroslav@166
   210
     * @throws NullPointerException if some of essential parameters (like {@link #loadPage(java.lang.String) page} or
jaroslav@166
   211
     *    {@link #loadClass(java.lang.Class) class} have not been specified
jaroslav@166
   212
     */
jaroslav@123
   213
    public void showAndWait() {
jaroslav@146
   214
        if (resource == null) {
jaroslav@166
   215
            throw new NullPointerException("Need to specify resource via loadPage method");
jaroslav@146
   216
        }
jaroslav@146
   217
        
jaroslav@148
   218
        URL url = null;
jtulach@653
   219
        IOException mal = null;
jaroslav@148
   220
        try {
jaroslav@202
   221
            String baseURL = System.getProperty("browser.rootdir");
jaroslav@202
   222
            if (baseURL != null) {
jaroslav@202
   223
                url = new File(baseURL, resource).toURI().toURL();
jaroslav@202
   224
            } else {
jaroslav@202
   225
                url = new URL(resource);
jaroslav@202
   226
            }
jaroslav@148
   227
        } catch (MalformedURLException ex) {
jaroslav@148
   228
            mal = ex;
jaroslav@148
   229
        }
jtulach@656
   230
        final Class<?> myCls;
jtulach@656
   231
        if (clazz != null) {
jtulach@656
   232
            myCls = clazz;
jtulach@656
   233
        } else if (onLoad != null) {
jtulach@656
   234
            myCls = onLoad.getClass();
jtulach@656
   235
        } else {
jtulach@656
   236
            throw new NullPointerException("loadClass, neither loadFinished was called!");
jtulach@656
   237
        }
jtulach@656
   238
        
jaroslav@128
   239
        if (url == null) {
jtulach@656
   240
            url = myCls.getResource(resource);
jaroslav@148
   241
        }
jaroslav@148
   242
        if (url == null) {
jtulach@656
   243
            final ProtectionDomain pd = myCls.getProtectionDomain();
jtulach@653
   244
            if (pd != null && pd.getCodeSource() != null) {
jtulach@653
   245
                URL jar = pd.getCodeSource().getLocation();
jtulach@653
   246
                try {
jtulach@653
   247
                    url = new URL(jar, resource);
jtulach@653
   248
                } catch (MalformedURLException ex) {
jtulach@653
   249
                    ex.initCause(mal);
jtulach@653
   250
                    mal = ex;
jtulach@653
   251
                }
jtulach@653
   252
            }
jtulach@653
   253
        }
jtulach@653
   254
        if (url == null) {
jtulach@653
   255
            URL res = BrowserBuilder.class.getResource("html4j.txt");
jtulach@653
   256
            LOG.log(Level.FINE, "Found html4j {0}", res);
jtulach@653
   257
            if (res != null) try {
jtulach@653
   258
                URLConnection c = res.openConnection();
jtulach@653
   259
                LOG.log(Level.FINE, "testing : {0}", c);
jtulach@653
   260
                if (c instanceof JarURLConnection) {
jtulach@653
   261
                    JarURLConnection jc = (JarURLConnection)c;
jtulach@653
   262
                    URL base = jc.getJarFileURL();
jtulach@653
   263
                    for (int i = 0; i < 50; i++) {
jtulach@653
   264
                        URL u = new URL(base, resource);
jtulach@653
   265
                        try {
jtulach@653
   266
                            InputStream is = u.openStream();
jtulach@653
   267
                            is.close();
jtulach@653
   268
                            url = u;
jtulach@653
   269
                            LOG.log(Level.FINE, "found real url: {0}", url);
jtulach@653
   270
                            break;
jtulach@653
   271
                        } catch (FileNotFoundException ignore) {
jtulach@653
   272
                            LOG.log(Level.FINE, "Cannot open " + u, ignore);
jtulach@653
   273
                        }
jtulach@653
   274
                        base = new URL(base, "..");
jtulach@653
   275
                    }
jtulach@653
   276
                }
jtulach@653
   277
            } catch (IOException ex) {
jaroslav@202
   278
                mal = ex;
jaroslav@202
   279
            }
jaroslav@202
   280
        }
jaroslav@202
   281
        if (url == null) {
jtulach@656
   282
            IllegalStateException ise = new IllegalStateException("Can't find resouce: " + resource + " relative to " + myCls);
jaroslav@148
   283
            if (mal != null) {
jaroslav@148
   284
                ise.initCause(mal);
jaroslav@148
   285
            }
jaroslav@148
   286
            throw ise;
jaroslav@128
   287
        }
jaroslav@288
   288
        
jaroslav@288
   289
        Fn.Presenter dfnr = null;
jaroslav@288
   290
        for (Object o : context) {
jaroslav@288
   291
            if (o instanceof Fn.Presenter) {
jaroslav@288
   292
                dfnr = (Fn.Presenter)o;
jaroslav@288
   293
                break;
jaroslav@288
   294
            }
jaroslav@288
   295
        }
jaroslav@127
   296
jtulach@771
   297
        if (dfnr == null && loader != null) for (Fn.Presenter o : ServiceLoader.load(Fn.Presenter.class, loader)) {
jtulach@771
   298
            dfnr = o;
jtulach@771
   299
            break;
jtulach@771
   300
        }
jtulach@771
   301
        
jaroslav@288
   302
        if (dfnr == null) for (Fn.Presenter o : ServiceLoader.load(Fn.Presenter.class)) {
jaroslav@288
   303
            dfnr = o;
jaroslav@288
   304
            break;
jaroslav@288
   305
        }
jaroslav@288
   306
        
jaroslav@288
   307
        if (dfnr == null) {
jaroslav@288
   308
            throw new IllegalStateException("Can't find any Fn.Presenter");
jaroslav@288
   309
        }
jaroslav@288
   310
        
jtulach@771
   311
        final ClassLoader activeLoader;
jtulach@771
   312
        if (loader != null) {
jtulach@771
   313
            if (!FnUtils.isJavaScriptCapable(loader)) {
jtulach@771
   314
                throw new IllegalStateException("Loader cannot resolve @JavaScriptBody: " + loader);
jtulach@771
   315
            }
jtulach@771
   316
            activeLoader = loader;
jtulach@771
   317
        } else if (FnUtils.isJavaScriptCapable(myCls.getClassLoader())) {
jtulach@771
   318
            activeLoader = myCls.getClassLoader();
jaroslav@509
   319
        } else {
jtulach@656
   320
            FImpl impl = new FImpl(myCls.getClassLoader());
jtulach@771
   321
            activeLoader = FnUtils.newLoader(impl, dfnr, myCls.getClassLoader().getParent());
jaroslav@509
   322
        }
jtulach@736
   323
        
jtulach@736
   324
        final Fn.Presenter dP = dfnr;
jaroslav@128
   325
jaroslav@288
   326
        class OnPageLoad implements Runnable {
jaroslav@288
   327
            @Override
jaroslav@288
   328
            public void run() {
jaroslav@288
   329
                try {
jtulach@736
   330
                    final Fn.Presenter aP = Fn.activePresenter();
jtulach@736
   331
                    final Fn.Presenter currentP = aP != null ? aP : dP;
jtulach@736
   332
                    
jtulach@771
   333
                    Thread.currentThread().setContextClassLoader(activeLoader);
jtulach@771
   334
                    final Class<?> newClazz = Class.forName(myCls.getName(), true, activeLoader);
jtulach@736
   335
                    if (browserClass != null) {
jtulach@736
   336
                        browserClass[0] = newClazz;
jtulach@736
   337
                    }
jtulach@736
   338
                    Contexts.Builder cb = Contexts.newBuilder();
jtulach@736
   339
                    if (!Contexts.fillInByProviders(newClazz, cb)) {
jtulach@736
   340
                        LOG.log(Level.WARNING, "Using empty technology for {0}", newClazz);
jtulach@736
   341
                    }
jtulach@736
   342
                    if (currentP instanceof Executor) {
jtulach@736
   343
                        cb.register(Executor.class, (Executor)currentP, 1000);
jtulach@736
   344
                    }
jtulach@736
   345
                    cb.register(Fn.Presenter.class, currentP, 1000);
jtulach@736
   346
                    BrwsrCtx c = cb.build();
jtulach@736
   347
jtulach@736
   348
                    class CallInitMethod implements Runnable {
jtulach@736
   349
                        @Override
jtulach@736
   350
                        public void run() {
jtulach@736
   351
                            Throwable firstError = null;
jtulach@736
   352
                            if (onLoad != null) {
jtulach@736
   353
                                try {
jtulach@736
   354
                                    FnContext.currentPresenter(currentP);
jtulach@736
   355
                                    onLoad.run();
jtulach@736
   356
                                } catch (Throwable ex) {
jtulach@736
   357
                                    firstError = ex;
jtulach@736
   358
                                } finally {
jtulach@736
   359
                                    FnContext.currentPresenter(null);
jtulach@736
   360
                                }
jtulach@736
   361
                            }
jtulach@736
   362
                            INIT: if (methodName != null) {
jtulach@736
   363
                                if (methodArgs.length == 0) {
jtulach@736
   364
                                    try {
jtulach@736
   365
                                        Method m = newClazz.getMethod(methodName);
jtulach@736
   366
                                        FnContext.currentPresenter(currentP);
jtulach@736
   367
                                        m.invoke(null);
jtulach@736
   368
                                        firstError = null;
jtulach@736
   369
                                        break INIT;
jtulach@736
   370
                                    } catch (Throwable ex) {
jtulach@736
   371
                                        firstError = ex;
jtulach@736
   372
                                    } finally {
jtulach@736
   373
                                        FnContext.currentPresenter(null);
jtulach@736
   374
                                    }
jtulach@736
   375
                                }
jtulach@736
   376
                                try {
jtulach@736
   377
                                    Method m = newClazz.getMethod(methodName, String[].class);
jtulach@736
   378
                                    FnContext.currentPresenter(currentP);
jtulach@736
   379
                                    m.invoke(m, (Object) methodArgs);
jtulach@736
   380
                                    firstError = null;
jtulach@736
   381
                                } catch (Throwable ex) {
jtulach@736
   382
                                    LOG.log(Level.SEVERE, "Can't call " + methodName + " with args " + Arrays.toString(methodArgs), ex);
jtulach@736
   383
                                } finally {
jtulach@736
   384
                                    FnContext.currentPresenter(null);
jtulach@736
   385
                                }
jtulach@736
   386
                            }
jtulach@736
   387
                            if (firstError != null) {
jtulach@736
   388
                                LOG.log(Level.SEVERE, "Can't initialize the view", firstError);
jtulach@736
   389
                            }
jtulach@569
   390
                        }
jaroslav@288
   391
                    }
jtulach@569
   392
                    
jtulach@736
   393
                    c.execute(new CallInitMethod());
jaroslav@288
   394
                } catch (ClassNotFoundException ex) {
jtulach@656
   395
                    LOG.log(Level.SEVERE, "Can't load " + myCls.getName(), ex);
jaroslav@128
   396
                }
jaroslav@127
   397
            }
jaroslav@127
   398
        }
jaroslav@288
   399
        dfnr.displayPage(url, new OnPageLoad());
jaroslav@127
   400
    }
jaroslav@146
   401
jaroslav@128
   402
    private static final class FImpl implements FindResources {
jaroslav@128
   403
        final ClassLoader l;
jaroslav@127
   404
jaroslav@127
   405
        public FImpl(ClassLoader l) {
jaroslav@127
   406
            this.l = l;
jaroslav@127
   407
        }
jaroslav@127
   408
jaroslav@127
   409
        @Override
jaroslav@127
   410
        public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
jaroslav@127
   411
            if (oneIsEnough) {
jaroslav@127
   412
                URL u = l.getResource(path);
jaroslav@127
   413
                if (u != null) {
jaroslav@127
   414
                    results.add(u);
jaroslav@127
   415
                }
jaroslav@127
   416
            } else {
jaroslav@127
   417
                try {
jaroslav@127
   418
                    Enumeration<URL> en = l.getResources(path);
jaroslav@127
   419
                    while (en.hasMoreElements()) {
jaroslav@127
   420
                        results.add(en.nextElement());
jaroslav@127
   421
                    }
jaroslav@127
   422
                } catch (IOException ex) {
jaroslav@127
   423
                    // no results
jaroslav@127
   424
                }
jaroslav@127
   425
            }
jaroslav@127
   426
        }
jaroslav@127
   427
        
jaroslav@123
   428
    }
jaroslav@123
   429
}