displayPage should be blocking, no need to expose resource finder classloader
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Jun 2013 22:29:41 +0200
branchclassloader
changeset 1283cc64ce45056
parent 127 28ea7ef534e9
child 129 b6907f29a4c1
displayPage should be blocking, no need to expose resource finder
boot/src/main/java/net/java/html/boot/BrowserBuilder.java
boot/src/main/java/org/apidesign/html/boot/impl/FindResources.java
boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java
boot/src/main/java/org/apidesign/html/boot/spi/Fn.java
boot/src/test/java/org/apidesign/html/boot/impl/FnTest.java
boot/src/test/java/org/apidesign/html/boot/spi/FnTest.java
     1.1 --- a/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Wed Jun 19 13:18:44 2013 +0200
     1.2 +++ b/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Wed Jun 19 22:29:41 2013 +0200
     1.3 @@ -27,7 +27,7 @@
     1.4  import java.util.ServiceLoader;
     1.5  import org.apidesign.html.boot.impl.FnUtils;
     1.6  import org.apidesign.html.boot.spi.Fn;
     1.7 -import org.apidesign.html.boot.spi.Fn.Finder;
     1.8 +import org.apidesign.html.boot.impl.FindResources;
     1.9  
    1.10  /**
    1.11   *
    1.12 @@ -55,22 +55,32 @@
    1.13      
    1.14      public void showAndWait() {
    1.15          FImpl impl = new FImpl(clazz.getClassLoader());
    1.16 +        URL url = clazz.getResource(resource);
    1.17 +        if (url == null) {
    1.18 +            throw new IllegalStateException("Can't find resouce: " + resource + " relative to " + clazz);
    1.19 +        }
    1.20  
    1.21          for (Fn.Presenter dfnr : ServiceLoader.load(Fn.Presenter.class)) {
    1.22 -            try {
    1.23 -                ClassLoader loader = FnUtils.newLoader(impl, dfnr, clazz.getClassLoader().getParent());
    1.24 -                dfnr.loadPage(resource);
    1.25 -                Class<?> newClazz = Class.forName(clazz.getName(), true, loader);
    1.26 -                dfnr.waitFinished();
    1.27 -            } catch (ClassNotFoundException ex) {
    1.28 -                throw new IllegalStateException(ex);
    1.29 +            final ClassLoader loader = FnUtils.newLoader(impl, dfnr, clazz.getClassLoader().getParent());
    1.30 +
    1.31 +            class OnPageLoad implements Runnable {
    1.32 +                @Override
    1.33 +                public void run() {
    1.34 +                    try {
    1.35 +                        Class<?> newClazz = Class.forName(clazz.getName(), true, loader);
    1.36 +                    } catch (ClassNotFoundException ex) {
    1.37 +                        throw new IllegalStateException(ex);
    1.38 +                    }
    1.39 +                }
    1.40              }
    1.41 +            dfnr.displayPage(url, new OnPageLoad());
    1.42 +            return;
    1.43          }
    1.44          throw new IllegalStateException("Can't find any Fn.Definer");
    1.45      }
    1.46      
    1.47 -    private static final class FImpl implements Finder {
    1.48 -        private final ClassLoader l;
    1.49 +    private static final class FImpl implements FindResources {
    1.50 +        final ClassLoader l;
    1.51  
    1.52          public FImpl(ClassLoader l) {
    1.53              this.l = l;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot/src/main/java/org/apidesign/html/boot/impl/FindResources.java	Wed Jun 19 22:29:41 2013 +0200
     2.3 @@ -0,0 +1,14 @@
     2.4 +package org.apidesign.html.boot.impl;
     2.5 +
     2.6 +import java.net.URL;
     2.7 +import java.util.Collection;
     2.8 +
     2.9 +/**
    2.10 + *
    2.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.12 + */
    2.13 +public interface FindResources {
    2.14 +
    2.15 +    public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough);
    2.16 +    
    2.17 +}
     3.1 --- a/boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java	Wed Jun 19 13:18:44 2013 +0200
     3.2 +++ b/boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java	Wed Jun 19 22:29:41 2013 +0200
     3.3 @@ -41,7 +41,7 @@
     3.4          return cl.defineFn(code, names);
     3.5      }
     3.6  
     3.7 -    public static ClassLoader newLoader(final Fn.Finder f, final Fn.Presenter d, ClassLoader parent) {
     3.8 +    public static ClassLoader newLoader(final FindResources f, final Fn.Presenter d, ClassLoader parent) {
     3.9          return new JsClassLoader(parent) {
    3.10              @Override
    3.11              protected URL findResource(String name) {
     4.1 --- a/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java	Wed Jun 19 13:18:44 2013 +0200
     4.2 +++ b/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java	Wed Jun 19 22:29:41 2013 +0200
     4.3 @@ -32,11 +32,6 @@
     4.4      
     4.5      public interface Presenter {
     4.6          public Fn defineFn(String code, String... names);
     4.7 -        public void loadPage(String resource);
     4.8 -        public void waitFinished();
     4.9 -    }
    4.10 -    
    4.11 -    public interface Finder {
    4.12 -        public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough);
    4.13 +        public void displayPage(URL page, Runnable onPageLoad);
    4.14      }
    4.15  }
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/boot/src/test/java/org/apidesign/html/boot/impl/FnTest.java	Wed Jun 19 22:29:41 2013 +0200
     5.3 @@ -0,0 +1,109 @@
     5.4 +/**
     5.5 + * HTML via Java(tm) Language Bindings
     5.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, version 2 of the License.
    5.11 + *
    5.12 + * This program is distributed in the hope that it will be useful,
    5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.15 + * GNU General Public License for more details. apidesign.org
    5.16 + * designates this particular file as subject to the
    5.17 + * "Classpath" exception as provided by apidesign.org
    5.18 + * in the License file that accompanied this code.
    5.19 + *
    5.20 + * You should have received a copy of the GNU General Public License
    5.21 + * along with this program. Look for COPYING file in the top folder.
    5.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    5.23 + */
    5.24 +
    5.25 +package org.apidesign.html.boot.impl;
    5.26 +
    5.27 +import java.net.URL;
    5.28 +import java.net.URLClassLoader;
    5.29 +import java.util.ArrayList;
    5.30 +import java.util.Arrays;
    5.31 +import java.util.Collection;
    5.32 +import java.util.List;
    5.33 +import javax.script.Invocable;
    5.34 +import javax.script.ScriptEngine;
    5.35 +import javax.script.ScriptEngineManager;
    5.36 +import javax.script.ScriptException;
    5.37 +import org.apidesign.html.boot.spi.Fn;
    5.38 +import org.testng.annotations.BeforeClass;
    5.39 +
    5.40 +/**
    5.41 + *
    5.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.43 + */
    5.44 +public class FnTest extends JsClassLoaderBase {
    5.45 +    
    5.46 +    public FnTest() {
    5.47 +    }
    5.48 +
    5.49 +    @BeforeClass
    5.50 +    public static void createClassLoader() throws Exception {
    5.51 +        ScriptEngineManager sem = new ScriptEngineManager();
    5.52 +        final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    5.53 +        
    5.54 +        final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
    5.55 +        ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    5.56 +        final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    5.57 +        
    5.58 +        class Impl implements FindResources, Fn.Presenter {
    5.59 +            @Override
    5.60 +            public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
    5.61 +                URL u = ul.findResource(path);
    5.62 +                if (u != null) {
    5.63 +                    results.add(u);
    5.64 +                }
    5.65 +            }
    5.66 +
    5.67 +            @Override
    5.68 +            public Fn defineFn(String code, String... names) {
    5.69 +                StringBuilder sb = new StringBuilder();
    5.70 +                sb.append("(function() {");
    5.71 +                sb.append("return function(");
    5.72 +                String sep = "";
    5.73 +                for (String n : names) {
    5.74 +                    sb.append(sep);
    5.75 +                    sb.append(n);
    5.76 +                    sep = ", ";
    5.77 +                }
    5.78 +                sb.append(") {");
    5.79 +                sb.append(code);
    5.80 +                sb.append("};");
    5.81 +                sb.append("})()");
    5.82 +                try {
    5.83 +                    final Object val = eng.eval(sb.toString());
    5.84 +                    return new Fn() {
    5.85 +                        @Override
    5.86 +                        public Object invoke(Object thiz, Object... args) throws Exception {
    5.87 +                            List<Object> all = new ArrayList<Object>(args.length + 1);
    5.88 +                            all.add(thiz == null ? val : thiz);
    5.89 +                            all.addAll(Arrays.asList(args));
    5.90 +                            Invocable inv = (Invocable)eng;
    5.91 +                            Object ret = inv.invokeMethod(val, "call", all.toArray());
    5.92 +                            return ret == val ? null : ret;
    5.93 +                        }
    5.94 +                    };
    5.95 +                } catch (ScriptException ex) {
    5.96 +                    throw new LinkageError("Can't parse: " + sb, ex);
    5.97 +                }
    5.98 +            }
    5.99 +
   5.100 +            @Override
   5.101 +            public void displayPage(URL resource, Runnable r) {
   5.102 +                throw new UnsupportedOperationException();
   5.103 +            }
   5.104 +        }
   5.105 +        Impl impl = new Impl();
   5.106 +        ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
   5.107 +       
   5.108 +        
   5.109 +        methodClass = loader.loadClass(JsMethods.class.getName());
   5.110 +    }
   5.111 +    
   5.112 +}
     6.1 --- a/boot/src/test/java/org/apidesign/html/boot/spi/FnTest.java	Wed Jun 19 13:18:44 2013 +0200
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,120 +0,0 @@
     6.4 -/**
     6.5 - * HTML via Java(tm) Language Bindings
     6.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     6.7 - *
     6.8 - * This program is free software: you can redistribute it and/or modify
     6.9 - * it under the terms of the GNU General Public License as published by
    6.10 - * the Free Software Foundation, version 2 of the License.
    6.11 - *
    6.12 - * This program is distributed in the hope that it will be useful,
    6.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.15 - * GNU General Public License for more details. apidesign.org
    6.16 - * designates this particular file as subject to the
    6.17 - * "Classpath" exception as provided by apidesign.org
    6.18 - * in the License file that accompanied this code.
    6.19 - *
    6.20 - * You should have received a copy of the GNU General Public License
    6.21 - * along with this program. Look for COPYING file in the top folder.
    6.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    6.23 - */
    6.24 -
    6.25 -package org.apidesign.html.boot.spi;
    6.26 -
    6.27 -
    6.28 -
    6.29 -import java.net.URL;
    6.30 -import java.net.URLClassLoader;
    6.31 -import java.util.ArrayList;
    6.32 -import java.util.Arrays;
    6.33 -import java.util.Collection;
    6.34 -import java.util.List;
    6.35 -import javax.script.Invocable;
    6.36 -import javax.script.ScriptEngine;
    6.37 -import javax.script.ScriptEngineManager;
    6.38 -import javax.script.ScriptException;
    6.39 -import org.apidesign.html.boot.impl.FnUtils;
    6.40 -import org.apidesign.html.boot.impl.JsClassLoaderBase;
    6.41 -import org.apidesign.html.boot.impl.JsClassLoaderTest;
    6.42 -import org.apidesign.html.boot.impl.JsMethods;
    6.43 -import org.testng.annotations.BeforeClass;
    6.44 -
    6.45 -/**
    6.46 - *
    6.47 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    6.48 - */
    6.49 -public class FnTest extends JsClassLoaderBase {
    6.50 -    
    6.51 -    public FnTest() {
    6.52 -    }
    6.53 -
    6.54 -    @BeforeClass
    6.55 -    public static void createClassLoader() throws Exception {
    6.56 -        ScriptEngineManager sem = new ScriptEngineManager();
    6.57 -        final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    6.58 -        
    6.59 -        final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
    6.60 -        ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    6.61 -        final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    6.62 -        
    6.63 -        class Impl implements Fn.Finder, Fn.Presenter {
    6.64 -            @Override
    6.65 -            public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
    6.66 -                URL u = ul.findResource(path);
    6.67 -                if (u != null) {
    6.68 -                    results.add(u);
    6.69 -                }
    6.70 -            }
    6.71 -
    6.72 -            @Override
    6.73 -            public Fn defineFn(String code, String... names) {
    6.74 -                StringBuilder sb = new StringBuilder();
    6.75 -                sb.append("(function() {");
    6.76 -                sb.append("return function(");
    6.77 -                String sep = "";
    6.78 -                for (String n : names) {
    6.79 -                    sb.append(sep);
    6.80 -                    sb.append(n);
    6.81 -                    sep = ", ";
    6.82 -                }
    6.83 -                sb.append(") {");
    6.84 -                sb.append(code);
    6.85 -                sb.append("};");
    6.86 -                sb.append("})()");
    6.87 -                try {
    6.88 -                    final Object val = eng.eval(sb.toString());
    6.89 -                    return new Fn() {
    6.90 -                        @Override
    6.91 -                        public Object invoke(Object thiz, Object... args) throws Exception {
    6.92 -                            List<Object> all = new ArrayList<Object>(args.length + 1);
    6.93 -                            all.add(thiz == null ? val : thiz);
    6.94 -                            all.addAll(Arrays.asList(args));
    6.95 -                            Invocable inv = (Invocable)eng;
    6.96 -                            Object ret = inv.invokeMethod(val, "call", all.toArray());
    6.97 -                            return ret == val ? null : ret;
    6.98 -                        }
    6.99 -                    };
   6.100 -                } catch (ScriptException ex) {
   6.101 -                    throw new LinkageError("Can't parse: " + sb, ex);
   6.102 -                }
   6.103 -            }
   6.104 -
   6.105 -            @Override
   6.106 -            public void loadPage(String resource) {
   6.107 -                throw new UnsupportedOperationException();
   6.108 -            }
   6.109 -
   6.110 -            @Override
   6.111 -            public void waitFinished() {
   6.112 -                throw new UnsupportedOperationException();
   6.113 -            }
   6.114 -            
   6.115 -        }
   6.116 -        Impl impl = new Impl();
   6.117 -        ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
   6.118 -       
   6.119 -        
   6.120 -        methodClass = loader.loadClass(JsMethods.class.getName());
   6.121 -    }
   6.122 -    
   6.123 -}