boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java
branchclassloader
changeset 163 2652760705d6
parent 161 ea5ca9cc685d
child 171 54ac82353158
     1.1 --- a/boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java	Tue Jun 25 21:09:48 2013 +0200
     1.2 +++ b/boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java	Wed Jun 26 08:43:32 2013 +0200
     1.3 @@ -20,6 +20,9 @@
     1.4   */
     1.5  package org.apidesign.html.boot.impl;
     1.6  
     1.7 +import java.io.InputStream;
     1.8 +import java.io.InputStreamReader;
     1.9 +import java.io.Reader;
    1.10  import java.lang.reflect.Method;
    1.11  import java.net.URL;
    1.12  import java.util.ArrayList;
    1.13 @@ -65,6 +68,11 @@
    1.14              protected Fn defineFn(String code, String... names) {
    1.15                  return d.defineFn(code, names);
    1.16              }
    1.17 +
    1.18 +            @Override
    1.19 +            protected void loadScript(Reader code) throws Exception {
    1.20 +                d.loadScript(code);
    1.21 +            }
    1.22          };
    1.23      }
    1.24  
    1.25 @@ -145,5 +153,24 @@
    1.26          }
    1.27          return Class.forName(t.getClassName(), false, loader);
    1.28      }
    1.29 -    
    1.30 +
    1.31 +    static void loadScript(JsClassLoader jcl, String resource) {
    1.32 +        final InputStream script = jcl.getResourceAsStream(resource);
    1.33 +        if (script == null) {
    1.34 +            throw new NullPointerException("Can't find " + resource);
    1.35 +        }
    1.36 +        try {
    1.37 +            Reader isr = null;
    1.38 +            try {
    1.39 +                isr = new InputStreamReader(script, "UTF-8");
    1.40 +                jcl.loadScript(isr);
    1.41 +            } finally {
    1.42 +                if (isr != null) {
    1.43 +                    isr.close();
    1.44 +                }
    1.45 +            }
    1.46 +        } catch (Exception ex) {
    1.47 +            throw new IllegalStateException("Can't execute " + resource, ex);
    1.48 +        } 
    1.49 +    }
    1.50  }