src/java.base/share/classes/jdk/internal/loader/URLClassPath.java
author godin
Wed, 21 Aug 2019 00:08:35 +0200
changeset 57819 40006c0ada91
parent 54218 3fe41815505c
permissions -rw-r--r--
8139820: URLClassPath.FileLoader constructor redundantly checks protocol Reviewed-by: chegar, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53277
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3436
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3436
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3436
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3436
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3436
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
36674
7ab530dd6f10 8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents: 36670
diff changeset
    26
package jdk.internal.loader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    28
import java.io.Closeable;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    29
import java.io.File;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    30
import java.io.FileInputStream;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    31
import java.io.FileNotFoundException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    32
import java.io.IOException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    33
import java.io.InputStream;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    34
import java.net.HttpURLConnection;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    35
import java.net.JarURLConnection;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    36
import java.net.MalformedURLException;
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
    37
import java.net.URI;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    38
import java.net.URL;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    39
import java.net.URLConnection;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    40
import java.net.URLStreamHandler;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    41
import java.net.URLStreamHandlerFactory;
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
    42
import java.security.AccessControlContext;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    43
import java.security.AccessControlException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    44
import java.security.AccessController;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    45
import java.security.CodeSigner;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    46
import java.security.Permission;
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
    47
import java.security.PrivilegedActionException;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    48
import java.security.PrivilegedExceptionAction;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    49
import java.security.cert.Certificate;
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
    50
import java.util.ArrayDeque;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    51
import java.util.ArrayList;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
    52
import java.util.Arrays;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    53
import java.util.Collections;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    54
import java.util.Enumeration;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    55
import java.util.HashMap;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    56
import java.util.HashSet;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    57
import java.util.LinkedList;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    58
import java.util.List;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    59
import java.util.NoSuchElementException;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    60
import java.util.Properties;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    61
import java.util.Set;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    62
import java.util.StringTokenizer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.util.zip.ZipEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.util.jar.JarEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.util.jar.Attributes.Name;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 36000
diff changeset
    69
import java.util.zip.ZipFile;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    70
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52172
diff changeset
    71
import jdk.internal.access.JavaNetURLAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52172
diff changeset
    72
import jdk.internal.access.JavaUtilZipFileAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52172
diff changeset
    73
import jdk.internal.access.SharedSecrets;
36745
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36674
diff changeset
    74
import jdk.internal.util.jar.InvalidJarIndexError;
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36674
diff changeset
    75
import jdk.internal.util.jar.JarIndex;
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
    76
import sun.net.util.URLUtil;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    77
import sun.net.www.ParseUtil;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    78
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * This class is used to maintain a search path of URLs for loading classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * and resources from both JAR files and directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
public class URLClassPath {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    87
    private static final String USER_AGENT_JAVA_VERSION = "UA-Java-Version";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    88
    private static final String JAVA_VERSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final boolean DEBUG;
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
    90
    private static final boolean DISABLE_JAR_CHECKING;
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
    91
    private static final boolean DISABLE_ACC_CHECKING;
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
    92
    private static final boolean DISABLE_CP_URL_CHECK;
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
    93
    private static final boolean DEBUG_CP_URL_CHECK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    96
        Properties props = GetPropertyAction.privilegedGetProperties();
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    97
        JAVA_VERSION = props.getProperty("java.version");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    98
        DEBUG = (props.getProperty("sun.misc.URLClassPath.debug") != null);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    99
        String p = props.getProperty("sun.misc.URLClassPath.disableJarChecking");
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52427
diff changeset
   100
        DISABLE_JAR_CHECKING = p != null ? p.equals("true") || p.isEmpty() : false;
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   101
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   102
        p = props.getProperty("jdk.net.URLClassPath.disableRestrictedPermissions");
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52427
diff changeset
   103
        DISABLE_ACC_CHECKING = p != null ? p.equals("true") || p.isEmpty() : false;
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
   104
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
   105
        // This property will be removed in a later release
54218
3fe41815505c 8211941: Enable checking/ignoring of non-conforming Class-Path entries
bchristi
parents: 53277
diff changeset
   106
        p = props.getProperty("jdk.net.URLClassPath.disableClassPathURLCheck");
3fe41815505c 8211941: Enable checking/ignoring of non-conforming Class-Path entries
bchristi
parents: 53277
diff changeset
   107
        DISABLE_CP_URL_CHECK = p != null ? p.equals("true") || p.isEmpty() : false;
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
   108
54218
3fe41815505c 8211941: Enable checking/ignoring of non-conforming Class-Path entries
bchristi
parents: 53277
diff changeset
   109
        // Print a message for each Class-Path entry that is ignored (assuming
3fe41815505c 8211941: Enable checking/ignoring of non-conforming Class-Path entries
bchristi
parents: 53277
diff changeset
   110
        // the check is not disabled).
3fe41815505c 8211941: Enable checking/ignoring of non-conforming Class-Path entries
bchristi
parents: 53277
diff changeset
   111
        p = props.getProperty("jdk.net.URLClassPath.showIgnoredClassPathEntries");
3fe41815505c 8211941: Enable checking/ignoring of non-conforming Class-Path entries
bchristi
parents: 53277
diff changeset
   112
        DEBUG_CP_URL_CHECK = p != null ? p.equals("true") || p.isEmpty() : false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /* The original search path of URLs. */
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   116
    private final ArrayList<URL> path;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   118
    /* The deque of unopened URLs */
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   119
    private final ArrayDeque<URL> unopenedUrls;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /* The resulting search path of Loaders */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   122
    private final ArrayList<Loader> loaders = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /* Map of each URL opened to its corresponding Loader */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   125
    private final HashMap<String, Loader> lmap = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /* The jar protocol handler to use when creating new URLs */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   128
    private final URLStreamHandler jarHandler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   130
    /* Whether this URLClassLoader has been closed yet */
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   131
    private boolean closed = false;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   132
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   133
    /* The context to be used when loading classes and resources.  If non-null
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   134
     * this is the context that was captured during the creation of the
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   135
     * URLClassLoader. null implies no additional security restrictions. */
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   136
    private final AccessControlContext acc;
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   137
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Creates a new URLClassPath for the given URLs. The URLs will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * searched in the order specified for classes and resources. A URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * ending with a '/' is assumed to refer to a directory. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * the URL is assumed to refer to a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param urls the directory and JAR file URLs to search for classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *        and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param factory the URLStreamHandlerFactory to use when creating new URLs
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   147
     * @param acc the context to be used when loading classes and resources, may
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   148
     *            be null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   150
    public URLClassPath(URL[] urls,
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   151
                        URLStreamHandlerFactory factory,
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   152
                        AccessControlContext acc) {
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   153
        ArrayList<URL> path = new ArrayList<>(urls.length);
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   154
        ArrayDeque<URL> unopenedUrls = new ArrayDeque<>(urls.length);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   155
        for (URL url : urls) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   156
            path.add(url);
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   157
            unopenedUrls.add(url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   159
        this.path = path;
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   160
        this.unopenedUrls = unopenedUrls;
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   161
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            jarHandler = factory.createURLStreamHandler("jar");
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   164
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   165
            jarHandler = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   167
        if (DISABLE_ACC_CHECKING)
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   168
            this.acc = null;
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   169
        else
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   170
            this.acc = acc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   173
    public URLClassPath(URL[] urls, AccessControlContext acc) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   174
        this(urls, null, acc);
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   175
    }
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   176
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   177
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   178
     * Constructs a URLClassPath from a class path string.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   179
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   180
     * @param cp the class path string
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   181
     * @param skipEmptyElements indicates if empty elements are ignored or
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   182
     *        treated as the current working directory
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   183
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   184
     * @apiNote Used to create the application class path.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   185
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   186
    URLClassPath(String cp, boolean skipEmptyElements) {
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   187
        ArrayList<URL> path = new ArrayList<>();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   188
        if (cp != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   189
            // map each element of class path to a file URL
48998
43bf8cca0ecc 8198485: Simplify a URLClassPath constructor
martin
parents: 48997
diff changeset
   190
            int off = 0, next;
43bf8cca0ecc 8198485: Simplify a URLClassPath constructor
martin
parents: 48997
diff changeset
   191
            do {
43bf8cca0ecc 8198485: Simplify a URLClassPath constructor
martin
parents: 48997
diff changeset
   192
                next = cp.indexOf(File.pathSeparator, off);
43bf8cca0ecc 8198485: Simplify a URLClassPath constructor
martin
parents: 48997
diff changeset
   193
                String element = (next == -1)
43bf8cca0ecc 8198485: Simplify a URLClassPath constructor
martin
parents: 48997
diff changeset
   194
                    ? cp.substring(off)
43bf8cca0ecc 8198485: Simplify a URLClassPath constructor
martin
parents: 48997
diff changeset
   195
                    : cp.substring(off, next);
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52915
diff changeset
   196
                if (!element.isEmpty() || !skipEmptyElements) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   197
                    URL url = toFileURL(element);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   198
                    if (url != null) path.add(url);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   199
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   200
                off = next + 1;
48998
43bf8cca0ecc 8198485: Simplify a URLClassPath constructor
martin
parents: 48997
diff changeset
   201
            } while (next != -1);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   202
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   203
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   204
        // can't use ArrayDeque#addAll or new ArrayDeque(Collection);
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   205
        // it's too early in the bootstrap to trigger use of lambdas
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   206
        int size = path.size();
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   207
        ArrayDeque<URL> unopenedUrls = new ArrayDeque<>(size);
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   208
        for (int i = 0; i < size; i++)
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   209
            unopenedUrls.add(path.get(i));
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   210
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   211
        this.unopenedUrls = unopenedUrls;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   212
        this.path = path;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   213
        this.jarHandler = null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   214
        this.acc = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   217
    public synchronized List<IOException> closeLoaders() {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   218
        if (closed) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   219
            return Collections.emptyList();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   220
        }
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   221
        List<IOException> result = new LinkedList<>();
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   222
        for (Loader loader : loaders) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   223
            try {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   224
                loader.close();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   225
            } catch (IOException e) {
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   226
                result.add(e);
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   227
            }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   228
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   229
        closed = true;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   230
        return result;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   231
    }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   232
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Appends the specified URL to the search path of directory and JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * file URLs from which to load classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * If the URL specified is null or is already in the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * URLs, then invoking this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
3073
5e93bd389012 6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents: 1940
diff changeset
   240
    public synchronized void addURL(URL url) {
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   241
        if (closed || url == null)
3073
5e93bd389012 6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents: 1940
diff changeset
   242
            return;
48996
1c28a76198da 8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents: 48995
diff changeset
   243
        synchronized (unopenedUrls) {
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   244
            if (! path.contains(url)) {
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   245
                unopenedUrls.addLast(url);
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   246
                path.add(url);
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   247
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   252
     * Appends the specified file path as a file URL to the search path.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   253
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   254
    public void addFile(String s) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   255
        URL url = toFileURL(s);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   256
        if (url != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   257
            addURL(url);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   258
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   259
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   260
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   261
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   262
     * Returns a file URL for the given file path.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   263
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   264
    private static URL toFileURL(String s) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   265
        try {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   266
            File f = new File(s).getCanonicalFile();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   267
            return ParseUtil.fileToEncodedURL(f);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   268
        } catch (IOException e) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   269
            return null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   270
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   271
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   272
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 43800
diff changeset
   273
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Returns the original search path of URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public URL[] getURLs() {
48996
1c28a76198da 8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents: 48995
diff changeset
   277
        synchronized (unopenedUrls) {
1c28a76198da 8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents: 48995
diff changeset
   278
            return path.toArray(new URL[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Finds the resource with the specified name on the URL search path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * or null if not found or security check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param name      the name of the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param check     whether to perform a security check
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   288
     * @return a {@code URL} for the resource, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * if the resource could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public URL findResource(String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        for (int i = 0; (loader = getLoader(i)) != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            URL url = loader.findResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Finds the first Resource on the URL search path which has the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * name. Returns null if no Resource could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param name the name of the Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param check     whether to perform a security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return the Resource, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public Resource getResource(String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            System.err.println("URLClassPath.getResource(\"" + name + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        for (int i = 0; (loader = getLoader(i)) != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            Resource res = loader.getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Finds all resources on the URL search path with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns an enumeration of the URL objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return an Enumeration of all the urls having the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   332
    public Enumeration<URL> findResources(final String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                     final boolean check) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   334
        return new Enumeration<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            private int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            private URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    while ((loader = getLoader(index++)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        url = loader.findResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   357
            public URL nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                URL u = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public Resource getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return getResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Finds all resources on the URL search path with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Returns an enumeration of the Resource objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return an Enumeration of all the resources having the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   379
    public Enumeration<Resource> getResources(final String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                    final boolean check) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   381
        return new Enumeration<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            private int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            private Resource res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    while ((loader = getLoader(index++)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        res = loader.getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   404
            public Resource nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                Resource r = res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   415
    public Enumeration<Resource> getResources(final String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return getResources(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Returns the Loader at the specified position in the URL search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * path. The URLs are opened and expanded as needed. Returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * if the specified index is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   424
    private synchronized Loader getLoader(int index) {
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   425
        if (closed) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   426
            return null;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   427
        }
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   428
        // Expand URL search path until the request can be satisfied
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   429
        // or unopenedUrls is exhausted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        while (loaders.size() < index + 1) {
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   431
            final URL url;
48996
1c28a76198da 8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents: 48995
diff changeset
   432
            synchronized (unopenedUrls) {
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   433
                url = unopenedUrls.pollFirst();
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   434
                if (url == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            // Skip this URL if it already has a Loader. (Loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            // may be null in the case where URL has not been opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            // but is referenced by a JAR index.)
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   440
            String urlNoFragString = URLUtil.urlNoFragString(url);
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   441
            if (lmap.containsKey(urlNoFragString)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // Otherwise, create a new Loader for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                loader = getLoader(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                // If the loader defines a local class path then add the
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   449
                // URLs as the next URLs to be opened.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                URL[] urls = loader.getClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (urls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    push(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                // Silently ignore for now...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                continue;
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   457
            } catch (SecurityException se) {
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   458
                // Always silently ignore. The context, if there is one, that
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   459
                // this URLClassPath was given during construction will never
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   460
                // have permission to access the URL.
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   461
                if (DEBUG) {
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   462
                    System.err.println("Failed to access " + url + ", " + se );
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   463
                }
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   464
                continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // Finally, add the Loader to the search path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            loaders.add(loader);
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   468
            lmap.put(urlNoFragString, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   470
        return loaders.get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Returns the Loader for the specified base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private Loader getLoader(final URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        try {
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   478
            return AccessController.doPrivileged(
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   479
                    new PrivilegedExceptionAction<>() {
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   480
                        public Loader run() throws IOException {
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   481
                            String protocol = url.getProtocol();  // lower cased in URL
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   482
                            String file = url.getFile();
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   483
                            if (file != null && file.endsWith("/")) {
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   484
                                if ("file".equals(protocol)) {
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   485
                                    return new FileLoader(url);
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   486
                                } else if ("jar".equals(protocol) &&
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   487
                                        isDefaultJarHandler(url) &&
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   488
                                        file.endsWith("!/")) {
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   489
                                    // extract the nested URL
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   490
                                    URL nestedUrl = new URL(file.substring(0, file.length() - 2));
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   491
                                    return new JarLoader(nestedUrl, jarHandler, lmap, acc);
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   492
                                } else {
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   493
                                    return new Loader(url);
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   494
                                }
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   495
                            } else {
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   496
                                return new JarLoader(url, jarHandler, lmap, acc);
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   497
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                        }
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   499
                    }, acc);
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   500
        } catch (PrivilegedActionException pae) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw (IOException)pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   505
    private static final JavaNetURLAccess JNUA
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   506
            = SharedSecrets.getJavaNetURLAccess();
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   507
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   508
    private static boolean isDefaultJarHandler(URL u) {
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   509
        URLStreamHandler h = JNUA.getHandler(u);
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   510
        return h instanceof sun.net.www.protocol.jar.Handler;
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   511
    }
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   512
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /*
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   514
     * Pushes the specified URLs onto the head of unopened URLs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
48996
1c28a76198da 8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents: 48995
diff changeset
   516
    private void push(URL[] urls) {
1c28a76198da 8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents: 48995
diff changeset
   517
        synchronized (unopenedUrls) {
1c28a76198da 8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents: 48995
diff changeset
   518
            for (int i = urls.length - 1; i >= 0; --i) {
48997
12c61ec06d2a 8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents: 48996
diff changeset
   519
                unopenedUrls.addFirst(urls[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /*
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   525
     * Checks whether the resource URL should be returned.
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   526
     * Returns null on security check failure.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Called by java.net.URLClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   529
    public static URL checkURL(URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   530
        if (url != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   531
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   532
                check(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   533
            } catch (Exception e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   534
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   535
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /*
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   541
     * Checks whether the resource URL should be returned.
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   542
     * Throws exception on failure.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Called internally within this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   545
    public static void check(URL url) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            URLConnection urlConnection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            Permission perm = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (perm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    security.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    // fallback to checkRead/checkConnect for pre 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    // security managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    if ((perm instanceof java.io.FilePermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        perm.getActions().indexOf("read") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        security.checkRead(perm.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    } else if ((perm instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        java.net.SocketPermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        perm.getActions().indexOf("connect") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        URL locUrl = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        security.checkConnect(locUrl.getHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                              locUrl.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   577
     * Nested class used to represent a loader of resources and classes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * from a base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   580
    private static class Loader implements Closeable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        private final URL base;
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   582
        private JarFile jarfile; // if this points to a jar file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * Creates a new Loader for the specified URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        Loader(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            base = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * Returns the base URL for this Loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        URL getBaseURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                 * For a HTTP connection we use the HEAD method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                 * check if the resource exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                URLConnection uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                if (uc instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    HttpURLConnection hconn = (HttpURLConnection)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    hconn.setRequestMethod("HEAD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    if (hconn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    // our best guess for the other cases
15305
45a0eb6ab4f7 7183373: URLClassloader.close() does not close JAR files whose resources have been loaded via getResource()
dingxmin
parents: 11131
diff changeset
   624
                    uc.setUseCaches(false);
45a0eb6ab4f7 7183373: URLClassloader.close() does not close JAR files whose resources have been loaded via getResource()
dingxmin
parents: 11131
diff changeset
   625
                    InputStream is = uc.getInputStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            final URLConnection uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                InputStream in = uc.getInputStream();
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   648
                if (uc instanceof JarURLConnection) {
9823
2bc6b0d4490e 7050028: ISE "zip file closed" from JarURLConnection.getInputStream on JDK 7 when !useCaches
michaelm
parents: 9035
diff changeset
   649
                    /* Need to remember the jar file so it can be closed
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   650
                     * in a hurry.
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   651
                     */
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   652
                    JarURLConnection juc = (JarURLConnection)uc;
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   653
                    jarfile = JarLoader.checkJar(juc.getJarFile());
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   654
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                public String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                public URL getURL() { return url; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                public URL getCodeSourceURL() { return base; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    return uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                public int getContentLength() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    return uc.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * Returns the Resource for the specified name, or null if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * found or the caller does not have the permission to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        Resource getResource(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return getResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        /*
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   681
         * Closes this loader and release all resources.
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   682
         * Method overridden in sub-classes.
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   683
         */
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   684
        @Override
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   685
        public void close() throws IOException {
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   686
            if (jarfile != null) {
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   687
                jarfile.close();
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   688
            }
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   689
        }
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   690
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   691
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * Returns the local class path for this loader, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        URL[] getClassPath() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    /*
47866
39db80b32b69 8191632: Typos in comments due to duplicating words
igerasim
parents: 47216
diff changeset
   700
     * Nested class used to represent a Loader of resources from a JAR URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
57819
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
   702
    private static class JarLoader extends Loader {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        private JarFile jar;
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   704
        private final URL csu;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        private JarIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        private URLStreamHandler handler;
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   707
        private final HashMap<String, Loader> lmap;
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   708
        private final AccessControlContext acc;
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   709
        private boolean closed = false;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 29986
diff changeset
   710
        private static final JavaUtilZipFileAccess zipAccess =
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 29986
diff changeset
   711
                SharedSecrets.getJavaUtilZipFileAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
         * Creates a new JarLoader for the specified URL referring to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         * a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         */
57819
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
   717
        private JarLoader(URL url, URLStreamHandler jarHandler,
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
   718
                          HashMap<String, Loader> loaderMap,
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
   719
                          AccessControlContext acc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            super(new URL("jar", "", -1, url + "!/", jarHandler));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            csu = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            handler = jarHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            lmap = loaderMap;
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   726
            this.acc = acc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
36000
86257b272c96 8068686: Remove meta-index support
chegar
parents: 32834
diff changeset
   728
            ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   731
        @Override
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   732
        public void close () throws IOException {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   733
            // closing is synchronized at higher level
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   734
            if (!closed) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   735
                closed = true;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   736
                // in case not already open.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   737
                ensureOpen();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   738
                jar.close();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   739
            }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   740
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   741
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        JarFile getJarFile () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            return jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        private boolean isOptimizable(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            return "file".equals(url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            if (jar == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                try {
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   753
                    AccessController.doPrivileged(
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   754
                        new PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   755
                            public Void run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                    System.err.println("Opening " + csu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                    Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                jar = getJarFile(csu);
36000
86257b272c96 8068686: Remove meta-index support
chegar
parents: 32834
diff changeset
   762
                                index = JarIndex.getJarIndex(jar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                                if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                                    String[] jarfiles = index.getJarFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                                // Add all the dependent URLs to the lmap so that loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                // will not be created for them by URLClassPath.getLoader(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                // if the same URL occurs later on the main class path.  We set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                                // Loader to null here to avoid creating a Loader for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                                // URL until we actually need to try to load something from them.
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   770
                                    for (int i = 0; i < jarfiles.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                            URL jarURL = new URL(csu, jarfiles[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                            // If a non-null loader already exists, leave it alone.
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   774
                                            String urlNoFragString = URLUtil.urlNoFragString(jarURL);
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   775
                                            if (!lmap.containsKey(urlNoFragString)) {
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   776
                                                lmap.put(urlNoFragString, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                            }
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   785
                        }, acc);
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   786
                } catch (PrivilegedActionException pae) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    throw (IOException)pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   792
        /* Throws if the given jar file is does not start with the correct LOC */
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   793
        static JarFile checkJar(JarFile jar) throws IOException {
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   794
            if (System.getSecurityManager() != null && !DISABLE_JAR_CHECKING
18273
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   795
                && !zipAccess.startsWithLocHeader(jar)) {
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   796
                IOException x = new IOException("Invalid Jar file");
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   797
                try {
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   798
                    jar.close();
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   799
                } catch (IOException ex) {
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   800
                    x.addSuppressed(ex);
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   801
                }
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   802
                throw x;
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   803
            }
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   804
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   805
            return jar;
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   806
        }
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   807
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        private JarFile getJarFile(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            // Optimize case where url refers to a local jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (isOptimizable(url)) {
52915
3addaaf7eaea 8215048: Some classloader typos
martin
parents: 52902
diff changeset
   811
                FileURLMapper p = new FileURLMapper(url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                if (!p.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    throw new FileNotFoundException(p.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 36000
diff changeset
   815
                return checkJar(new JarFile(new File(p.getPath()), true, ZipFile.OPEN_READ,
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 37785
diff changeset
   816
                        JarFile.runtimeVersion()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 36000
diff changeset
   818
            URLConnection uc = (new URL(getBaseURL(), "#runtime")).openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION);
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   820
            JarFile jarFile = ((JarURLConnection)uc).getJarFile();
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   821
            return checkJar(jarFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * Returns the index of this JarLoader if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        JarIndex getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            } catch (IOException e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 9823
diff changeset
   831
                throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * Creates the resource and if the check flag is set to true, checks if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * is its okay to return the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        Resource checkResource(final String name, boolean check,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            final JarEntry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            try {
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   845
                String nm;
36251
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   846
                if (jar.isMultiRelease()) {
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47866
diff changeset
   847
                    nm = entry.getRealName();
36251
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   848
                } else {
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   849
                    nm = name;
36251
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   850
                }
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   851
                url = new URL(getBaseURL(), ParseUtil.encodePath(nm, false));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                // throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            } catch (AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                public String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                public URL getURL() { return url; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                public URL getCodeSourceURL() { return csu; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                public InputStream getInputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    { return jar.getInputStream(entry); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                public int getContentLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    { return (int)entry.getSize(); }
52171
0da586f1ed05 8208754: The fix for JDK-8194534 needs updates
weijun
parents: 52164
diff changeset
   872
                public Manifest getManifest() throws IOException {
0da586f1ed05 8208754: The fix for JDK-8194534 needs updates
weijun
parents: 52164
diff changeset
   873
                    SharedSecrets.javaUtilJarAccess().ensureInitialization(jar);
0da586f1ed05 8208754: The fix for JDK-8194534 needs updates
weijun
parents: 52164
diff changeset
   874
                    return jar.getManifest();
0da586f1ed05 8208754: The fix for JDK-8194534 needs updates
weijun
parents: 52164
diff changeset
   875
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                public Certificate[] getCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    { return entry.getCertificates(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                public CodeSigner[] getCodeSigners()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    { return entry.getCodeSigners(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        /*
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   885
         * Returns true iff at least one resource in the jar file has the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         * package name as that of the specified resource name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        boolean validIndex(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            String packageName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            int pos;
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   891
            if ((pos = name.lastIndexOf('/')) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                packageName = name.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            String entryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            ZipEntry entry;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   897
            Enumeration<JarEntry> enum_ = jar.entries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            while (enum_.hasMoreElements()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   899
                entry = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                entryName = entry.getName();
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   901
                if ((pos = entryName.lastIndexOf('/')) != -1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    entryName = entryName.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                if (entryName.equals(packageName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
         * Returns the URL for a resource with the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
         */
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   913
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            Resource rsc = getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            if (rsc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                return rsc.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
         * Returns the JAR Resource for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         */
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
   925
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            } catch (IOException e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 9823
diff changeset
   930
                throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            final JarEntry entry = jar.getJarEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            if (entry != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                return checkResource(name, check, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            if (index == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   939
            HashSet<String> visited = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            return getResource(name, check, visited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * Version of getResource() that tracks the jar files that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         * visited by linking through the index files. This helper method uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         * a HashSet to store the URLs of jar files that have been searched and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         * uses it to avoid going into an infinite loop, looking for a
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   948
         * non-existent resource.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        Resource getResource(final String name, boolean check,
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   951
                             Set<String> visited) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            Resource res;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   953
            String[] jarFiles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            int count = 0;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   955
            LinkedList<String> jarFilesList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            /* If there no jar files in the index that can potential contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
             * this resource then return immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
             */
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   960
            if ((jarFilesList = index.get(name)) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                int size = jarFilesList.size();
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   965
                jarFiles = jarFilesList.toArray(new String[size]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                /* loop through the mapped jar file list */
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   967
                while (count < size) {
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   968
                    String jarName = jarFiles[count++];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    JarLoader newLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                        url = new URL(csu, jarName);
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   974
                        String urlNoFragString = URLUtil.urlNoFragString(url);
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   975
                        if ((newLoader = (JarLoader)lmap.get(urlNoFragString)) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                            /* no loader has been set up for this jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                             * before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                             */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   979
                            newLoader = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   980
                                new PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   981
                                    public JarLoader run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                                        return new JarLoader(url, handler,
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   983
                                            lmap, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                    }
43196
2064adad356a 8151934: Resolve class resolution
chegar
parents: 39646
diff changeset
   985
                                }, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                            /* this newly opened jar file has its own index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                             * merge it into the parent's index, taking into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                             * account the relative path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                            JarIndex newIndex = newLoader.getIndex();
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
   992
                            if (newIndex != null) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   993
                                int pos = jarName.lastIndexOf('/');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                                newIndex.merge(this.index, (pos == -1 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                                    null : jarName.substring(0, pos + 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                            /* put it in the global hashtable */
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   999
                            lmap.put(urlNoFragString, newLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                        }
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
  1001
                    } catch (PrivilegedActionException pae) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    /* Note that the addition of the url to the list of visited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                     * jars incorporates a check for presence in the hashmap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                     */
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
  1010
                    boolean visitedURL = !visited.add(URLUtil.urlNoFragString(url));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    if (!visitedURL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                            newLoader.ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                        } catch (IOException e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 9823
diff changeset
  1015
                            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                        final JarEntry entry = newLoader.jar.getJarEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                            return newLoader.checkResource(name, check, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                        /* Verify that at least one other resource with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                         * same package name as the lookedup resource is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                         * present in the new jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                        if (!newLoader.validIndex(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                            /* the mapping is wrong */
36745
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36674
diff changeset
  1028
                            throw new InvalidJarIndexError("Invalid index");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    /* If newLoader is the current loader or if it is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                     * loader that has already been searched or if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                     * loader does not have an index then skip it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                     * and move on to the next loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    if (visitedURL || newLoader == this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                            newLoader.getIndex() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    /* Process the index of the new loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                     */
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
  1044
                    if ((res = newLoader.getResource(name, check, visited))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                            != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                // Get the list of jar files again as the list could have grown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                // due to merging of index files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                jarFilesList = index.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            // If the count is unchanged, we are done.
48995
58bec53828ba 8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents: 47987
diff changeset
  1054
            } while (count < jarFilesList.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
         * Returns the JAR file local class path, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
         */
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
  1062
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        URL[] getClassPath() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            ensureOpen();
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 11131
diff changeset
  1069
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
  1070
            // Only get manifest when necessary
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
  1071
            if (SharedSecrets.javaUtilJarAccess().jarFileHasClassPathAttribute(jar)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                Manifest man = jar.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    Attributes attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                        String value = attr.getValue(Name.CLASS_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                            return parseClassPath(csu, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         * Parses value of the Class-Path manifest attribute and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
         * an array of URLs relative to the specified base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
         */
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
  1090
        private static URL[] parseClassPath(URL base, String value)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            StringTokenizer st = new StringTokenizer(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            URL[] urls = new URL[st.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                String path = st.nextToken();
53277
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1098
                URL url = DISABLE_CP_URL_CHECK ? new URL(base, path) : tryResolve(base, path);
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1099
                if (url != null) {
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1100
                    urls[i] = url;
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1101
                    i++;
53277
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1102
                } else {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1103
                    if (DEBUG_CP_URL_CHECK) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1104
                        System.err.println("Class-Path entry: \"" + path
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1105
                                           + "\" ignored in JAR file " + base);
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1106
                    }
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1107
                }
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1108
            }
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1109
            if (i == 0) {
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1110
                urls = null;
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1111
            } else if (i != urls.length) {
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1112
                // Truncate nulls from end of array
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1113
                urls = Arrays.copyOf(urls, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        }
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1117
53277
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1118
        static URL tryResolve(URL base, String input) throws MalformedURLException {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1119
            if ("file".equalsIgnoreCase(base.getProtocol())) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1120
                return tryResolveFile(base, input);
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1121
            } else {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1122
                return tryResolveNonFile(base, input);
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1123
            }
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1124
        }
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1125
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1126
        /**
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1127
         * Attempt to return a file URL by resolving input against a base file
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1128
         * URL. The input is an absolute or relative file URL that encodes a
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1129
         * file path.
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1130
         *
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1131
         * @apiNote Nonsensical input such as a Windows file path with a drive
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1132
         * letter cannot be disambiguated from an absolute URL so will be rejected
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1133
         * (by returning null) by this method.
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1134
         *
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1135
         * @return the resolved URL or null if the input is an absolute URL with
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1136
         *         a scheme other than file (ignoring case)
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1137
         * @throws MalformedURLException
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1138
         */
53277
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1139
        static URL tryResolveFile(URL base, String input) throws MalformedURLException {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1140
            int index = input.indexOf(':');
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1141
            boolean isFile;
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1142
            if (index >= 0) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1143
                String scheme = input.substring(0, index);
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1144
                isFile = "file".equalsIgnoreCase(scheme);
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1145
            } else {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1146
                isFile = true;
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1147
            }
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1148
            return (isFile) ? new URL(base, input) : null;
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1149
        }
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1150
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1151
        /**
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1152
         * Attempt to return a URL by resolving input against a base URL. Returns
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1153
         * null if the resolved URL is not contained by the base URL.
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1154
         *
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1155
         * @return the resolved URL or null
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1156
         * @throws MalformedURLException
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1157
         */
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1158
        static URL tryResolveNonFile(URL base, String input) throws MalformedURLException {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1159
            String child = input.replace(File.separatorChar, '/');
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1160
            if (isRelative(child)) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1161
                URL url = new URL(base, child);
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1162
                String bp = base.getPath();
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1163
                String urlp = url.getPath();
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1164
                int pos = bp.lastIndexOf('/');
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1165
                if (pos == -1) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1166
                    pos = bp.length() - 1;
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1167
                }
53277
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1168
                if (urlp.regionMatches(0, bp, 0, pos + 1)
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1169
                        && urlp.indexOf("..", pos) == -1) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1170
                    return url;
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1171
                }
52164
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1172
            }
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1173
            return null;
27135de165ac 8195874: Improve jar specification adherence
bchristi
parents: 48998
diff changeset
  1174
        }
53277
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1175
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1176
        /**
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1177
         * Returns true if the given input is a relative URI.
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1178
         */
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1179
        static boolean isRelative(String child) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1180
            try {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1181
                return !URI.create(child).isAbsolute();
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1182
            } catch (IllegalArgumentException e) {
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1183
                return false;
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1184
            }
b002e4ee60b0 8216401: Allow "file:" URLs in Class-Path of local JARs
bchristi
parents: 53018
diff changeset
  1185
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /*
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
  1189
     * Nested class used to represent a loader of classes and resources
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * from a file URL that refers to a directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    private static class FileLoader extends Loader {
495
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1193
        /* Canonicalized File */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        private File dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
57819
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
  1196
        /*
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
  1197
         * Creates a new FileLoader for the specified URL with a file protocol.
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
  1198
         */
40006c0ada91 8139820: URLClassPath.FileLoader constructor redundantly checks protocol
godin
parents: 54218
diff changeset
  1199
        private FileLoader(URL url) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            super(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            String path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            path = ParseUtil.decode(path);
495
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1203
            dir = (new File(path)).getCanonicalFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
         * Returns the URL for a resource with the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         */
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
  1209
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            Resource rsc = getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            if (rsc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                return rsc.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
43800
1dc22b50717a 8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents: 43196
diff changeset
  1218
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                URL normalizedBase = new URL(getBaseURL(), ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                url = new URL(getBaseURL(), ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                if (url.getFile().startsWith(normalizedBase.getFile()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    // requested resource had ../..'s in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                if (check)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    URLClassPath.check(url);
495
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1232
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1233
                final File file;
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1234
                if (name.indexOf("..") != -1) {
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1235
                    file = (new File(dir, name.replace('/', File.separatorChar)))
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1236
                          .getCanonicalFile();
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1237
                    if ( !((file.getPath()).startsWith(dir.getPath())) ) {
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1238
                        /* outside of base dir */
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1239
                        return null;
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1240
                    }
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1241
                } else {
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1242
                    file = new File(dir, name.replace('/', File.separatorChar));
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1243
                }
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1244
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                    return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                        public String getName() { return name; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                        public URL getURL() { return url; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                        public URL getCodeSourceURL() { return getBaseURL(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                        public InputStream getInputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                            { return new FileInputStream(file); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                        public int getContentLength() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                            { return (int)file.length(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
}