Trying really hard to obtain location of own JARs even if the system does not support getCodeSource (like Android libraries like to do)
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 18 May 2014 11:34:48 +0200
changeset 65341b0cfec61b1
parent 652 19ff3aea95f1
child 654 ee519e3681e9
Trying really hard to obtain location of own JARs even if the system does not support getCodeSource (like Android libraries like to do)
boot/src/main/java/net/java/html/boot/BrowserBuilder.java
boot/src/main/resources/net/java/html/boot/html4j.txt
     1.1 --- a/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Tue May 13 13:47:09 2014 +0200
     1.2 +++ b/boot/src/main/java/net/java/html/boot/BrowserBuilder.java	Sun May 18 11:34:48 2014 +0200
     1.3 @@ -43,10 +43,15 @@
     1.4  package net.java.html.boot;
     1.5  
     1.6  import java.io.File;
     1.7 +import java.io.FileNotFoundException;
     1.8  import java.io.IOException;
     1.9 +import java.io.InputStream;
    1.10  import java.lang.reflect.Method;
    1.11 +import java.net.JarURLConnection;
    1.12  import java.net.MalformedURLException;
    1.13  import java.net.URL;
    1.14 +import java.net.URLConnection;
    1.15 +import java.security.ProtectionDomain;
    1.16  import java.util.Arrays;
    1.17  import java.util.Collection;
    1.18  import java.util.Enumeration;
    1.19 @@ -180,7 +185,7 @@
    1.20          }
    1.21          
    1.22          URL url = null;
    1.23 -        MalformedURLException mal = null;
    1.24 +        IOException mal = null;
    1.25          try {
    1.26              String baseURL = System.getProperty("browser.rootdir");
    1.27              if (baseURL != null) {
    1.28 @@ -195,11 +200,41 @@
    1.29              url = clazz.getResource(resource);
    1.30          }
    1.31          if (url == null) {
    1.32 -            URL jar = clazz.getProtectionDomain().getCodeSource().getLocation();
    1.33 -            try {
    1.34 -                url = new URL(jar, resource);
    1.35 -            } catch (MalformedURLException ex) {
    1.36 -                ex.initCause(mal);
    1.37 +            final ProtectionDomain pd = clazz.getProtectionDomain();
    1.38 +            if (pd != null && pd.getCodeSource() != null) {
    1.39 +                URL jar = pd.getCodeSource().getLocation();
    1.40 +                try {
    1.41 +                    url = new URL(jar, resource);
    1.42 +                } catch (MalformedURLException ex) {
    1.43 +                    ex.initCause(mal);
    1.44 +                    mal = ex;
    1.45 +                }
    1.46 +            }
    1.47 +        }
    1.48 +        if (url == null) {
    1.49 +            URL res = BrowserBuilder.class.getResource("html4j.txt");
    1.50 +            LOG.log(Level.FINE, "Found html4j {0}", res);
    1.51 +            if (res != null) try {
    1.52 +                URLConnection c = res.openConnection();
    1.53 +                LOG.log(Level.FINE, "testing : {0}", c);
    1.54 +                if (c instanceof JarURLConnection) {
    1.55 +                    JarURLConnection jc = (JarURLConnection)c;
    1.56 +                    URL base = jc.getJarFileURL();
    1.57 +                    for (int i = 0; i < 50; i++) {
    1.58 +                        URL u = new URL(base, resource);
    1.59 +                        try {
    1.60 +                            InputStream is = u.openStream();
    1.61 +                            is.close();
    1.62 +                            url = u;
    1.63 +                            LOG.log(Level.FINE, "found real url: {0}", url);
    1.64 +                            break;
    1.65 +                        } catch (FileNotFoundException ignore) {
    1.66 +                            LOG.log(Level.FINE, "Cannot open " + u, ignore);
    1.67 +                        }
    1.68 +                        base = new URL(base, "..");
    1.69 +                    }
    1.70 +                }
    1.71 +            } catch (IOException ex) {
    1.72                  mal = ex;
    1.73              }
    1.74          }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot/src/main/resources/net/java/html/boot/html4j.txt	Sun May 18 11:34:48 2014 +0200
     2.3 @@ -0,0 +1,43 @@
     2.4 +====
     2.5 +    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 +
     2.7 +    Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     2.8 +
     2.9 +    Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 +    Other names may be trademarks of their respective owners.
    2.11 +
    2.12 +    The contents of this file are subject to the terms of either the GNU
    2.13 +    General Public License Version 2 only ("GPL") or the Common
    2.14 +    Development and Distribution License("CDDL") (collectively, the
    2.15 +    "License"). You may not use this file except in compliance with the
    2.16 +    License. You can obtain a copy of the License at
    2.17 +    http://www.netbeans.org/cddl-gplv2.html
    2.18 +    or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 +    specific language governing permissions and limitations under the
    2.20 +    License.  When distributing the software, include this License Header
    2.21 +    Notice in each file and include the License file at
    2.22 +    nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 +    particular file as subject to the "Classpath" exception as provided
    2.24 +    by Oracle in the GPL Version 2 section of the License file that
    2.25 +    accompanied this code. If applicable, add the following below the
    2.26 +    License Header, with the fields enclosed by brackets [] replaced by
    2.27 +    your own identifying information:
    2.28 +    "Portions Copyrighted [year] [name of copyright owner]"
    2.29 +
    2.30 +    Contributor(s):
    2.31 +
    2.32 +    The Original Software is NetBeans. The Initial Developer of the Original
    2.33 +    Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    2.34 +
    2.35 +    If you wish your version of this file to be governed by only the CDDL
    2.36 +    or only the GPL Version 2, indicate your decision by adding
    2.37 +    "[Contributor] elects to include this software in this distribution
    2.38 +    under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.39 +    single choice of license, a recipient has the option to distribute
    2.40 +    your version of this file under either the CDDL, the GPL Version 2 or
    2.41 +    to extend the choice of license to its licensees as provided above.
    2.42 +    However, if you add GPL Version 2 code and therefore, elected the GPL
    2.43 +    Version 2 license, then the option applies only if the new code is
    2.44 +    made subject to such option by the copyright holder.
    2.45 +====
    2.46 +