Support for external resources in -Dbrowser.rootdir or next to the main JAR's location
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 18 Jul 2013 15:39:20 +0200
changeset 2025a50f193bdff
parent 201 6292ef04c40b
child 203 3c6d52ddc8ce
child 204 d03faa1feaca
Support for external resources in -Dbrowser.rootdir or next to the main JAR's location
boot/src/main/java/net/java/html/boot/BrowserBuilder.java
     1.1 --- a/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Thu Jul 18 12:49:16 2013 +0200
     1.2 +++ b/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Thu Jul 18 15:39:20 2013 +0200
     1.3 @@ -20,6 +20,7 @@
     1.4   */
     1.5  package net.java.html.boot;
     1.6  
     1.7 +import java.io.File;
     1.8  import java.io.IOException;
     1.9  import java.lang.reflect.Method;
    1.10  import java.net.MalformedURLException;
    1.11 @@ -103,8 +104,14 @@
    1.12  
    1.13      /** Page to load into the browser. If the <code>page</code> represents
    1.14       * a {@link URL} known to the Java system, the URL is passed to the browser. 
    1.15 -     * Otherwise the system seeks for the resource via {@link Class#getResource(java.lang.String)}
    1.16 -     * method.
    1.17 +     * If system property <code>browser.rootdir</code> is specified, then a
    1.18 +     * file <code>page</code> relative to this directory is used as the URL.
    1.19 +     * If no such file exists, the system seeks for the 
    1.20 +     * resource via {@link Class#getResource(java.lang.String)}
    1.21 +     * method (relative to the {@link #loadClass(java.lang.Class) specified class}). 
    1.22 +     * If such resource is not found, a file relative to the location JAR
    1.23 +     * that contains the {@link #loadClass(java.lang.Class) main class} is 
    1.24 +     * searched for.
    1.25       * 
    1.26       * @param page the location (relative, absolute, or URL) of a page to load
    1.27       * @return this browser
    1.28 @@ -146,7 +153,12 @@
    1.29          URL url = null;
    1.30          MalformedURLException mal = null;
    1.31          try {
    1.32 -            url = new URL(resource);
    1.33 +            String baseURL = System.getProperty("browser.rootdir");
    1.34 +            if (baseURL != null) {
    1.35 +                url = new File(baseURL, resource).toURI().toURL();
    1.36 +            } else {
    1.37 +                url = new URL(resource);
    1.38 +            }
    1.39          } catch (MalformedURLException ex) {
    1.40              mal = ex;
    1.41          }
    1.42 @@ -154,6 +166,15 @@
    1.43              url = clazz.getResource(resource);
    1.44          }
    1.45          if (url == null) {
    1.46 +            URL jar = clazz.getProtectionDomain().getCodeSource().getLocation();
    1.47 +            try {
    1.48 +                url = new URL(jar, resource);
    1.49 +            } catch (MalformedURLException ex) {
    1.50 +                ex.initCause(mal);
    1.51 +                mal = ex;
    1.52 +            }
    1.53 +        }
    1.54 +        if (url == null) {
    1.55              IllegalStateException ise = new IllegalStateException("Can't find resouce: " + resource + " relative to " + clazz);
    1.56              if (mal != null) {
    1.57                  ise.initCause(mal);