jdk/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 36745 51effd3e92d0
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 19409
diff changeset
     2
 * Copyright (c) 1997, 2013, 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;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    37
import java.net.URL;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    38
import java.net.URLConnection;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    39
import java.net.URLStreamHandler;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    40
import java.net.URLStreamHandlerFactory;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    41
import java.security.AccessControlException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    42
import java.security.AccessController;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    43
import java.security.CodeSigner;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    44
import java.security.Permission;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    45
import java.security.PrivilegedExceptionAction;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    46
import java.security.cert.Certificate;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    47
import java.util.ArrayList;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    48
import java.util.Collections;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    49
import java.util.Enumeration;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    50
import java.util.HashMap;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    51
import java.util.HashSet;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    52
import java.util.LinkedList;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    53
import java.util.List;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    54
import java.util.NoSuchElementException;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    55
import java.util.Properties;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    56
import java.util.Set;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    57
import java.util.Stack;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    58
import java.util.StringTokenizer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.zip.ZipEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.util.jar.JarEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.util.jar.Attributes.Name;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 36000
diff changeset
    65
import java.util.zip.ZipFile;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    66
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 29986
diff changeset
    67
import jdk.internal.misc.JavaUtilZipFileAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 29986
diff changeset
    68
import jdk.internal.misc.SharedSecrets;
36745
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36674
diff changeset
    69
import jdk.internal.util.jar.InvalidJarIndexError;
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36674
diff changeset
    70
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
    71
import sun.net.util.URLUtil;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    72
import sun.net.www.ParseUtil;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    73
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * This class is used to maintain a search path of URLs for loading classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * and resources from both JAR files and directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class URLClassPath {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    82
    private static final String USER_AGENT_JAVA_VERSION = "UA-Java-Version";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    83
    private static final String JAVA_VERSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final boolean DEBUG;
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
    85
    private static final boolean DISABLE_JAR_CHECKING;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    88
        Properties props = GetPropertyAction.getProperties();
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    89
        JAVA_VERSION = props.getProperty("java.version");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    90
        DEBUG = (props.getProperty("sun.misc.URLClassPath.debug") != null);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36745
diff changeset
    91
        String p = props.getProperty("sun.misc.URLClassPath.disableJarChecking");
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
    92
        DISABLE_JAR_CHECKING = p != null ? p.equals("true") || p.equals("") : false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /* The original search path of URLs. */
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
    96
    private ArrayList<URL> path = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* The stack of unopened URLs */
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
    99
    Stack<URL> urls = new Stack<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* The resulting search path of Loaders */
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   102
    ArrayList<Loader> loaders = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* Map of each URL opened to its corresponding Loader */
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   105
    HashMap<String, Loader> lmap = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /* The jar protocol handler to use when creating new URLs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private URLStreamHandler jarHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   110
    /* Whether this URLClassLoader has been closed yet */
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   111
    private boolean closed = false;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   112
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Creates a new URLClassPath for the given URLs. The URLs will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * searched in the order specified for classes and resources. A URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * ending with a '/' is assumed to refer to a directory. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * the URL is assumed to refer to a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param urls the directory and JAR file URLs to search for classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *        and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param factory the URLStreamHandlerFactory to use when creating new URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public URLClassPath(URL[] urls, URLStreamHandlerFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            path.add(urls[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        push(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            jarHandler = factory.createURLStreamHandler("jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public URLClassPath(URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this(urls, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   137
    public synchronized List<IOException> closeLoaders() {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   138
        if (closed) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   139
            return Collections.emptyList();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   140
        }
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   141
        List<IOException> result = new LinkedList<>();
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   142
        for (Loader loader : loaders) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   143
            try {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   144
                loader.close();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   145
            } catch (IOException e) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   146
                result.add (e);
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   147
            }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   148
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   149
        closed = true;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   150
        return result;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   151
    }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   152
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Appends the specified URL to the search path of directory and JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * file URLs from which to load classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * If the URL specified is null or is already in the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * URLs, then invoking this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
3073
5e93bd389012 6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents: 1940
diff changeset
   160
    public synchronized void addURL(URL url) {
5e93bd389012 6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents: 1940
diff changeset
   161
        if (closed)
5e93bd389012 6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents: 1940
diff changeset
   162
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        synchronized (urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (url == null || path.contains(url))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            urls.add(0, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            path.add(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns the original search path of URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public URL[] getURLs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        synchronized (urls) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   177
            return path.toArray(new URL[path.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Finds the resource with the specified name on the URL search path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * or null if not found or security check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param name      the name of the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param check     whether to perform a security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return a <code>URL</code> for the resource, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * if the resource could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public URL findResource(String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        for (int i = 0; (loader = getLoader(i)) != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            URL url = loader.findResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Finds the first Resource on the URL search path which has the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * name. Returns null if no Resource could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param name the name of the Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param check     whether to perform a security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the Resource, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public Resource getResource(String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.err.println("URLClassPath.getResource(\"" + name + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (int i = 0; (loader = getLoader(i)) != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            Resource res = loader.getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Finds all resources on the URL search path with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns an enumeration of the URL objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return an Enumeration of all the urls having the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   231
    public Enumeration<URL> findResources(final String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                     final boolean check) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   233
        return new Enumeration<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            private int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            private URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    while ((loader = getLoader(index++)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        url = loader.findResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   256
            public URL nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                URL u = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public Resource getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return getResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Finds all resources on the URL search path with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns an enumeration of the Resource objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return an Enumeration of all the resources having the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   278
    public Enumeration<Resource> getResources(final String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                    final boolean check) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   280
        return new Enumeration<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            private int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            private Resource res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    while ((loader = getLoader(index++)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        res = loader.getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   303
            public Resource nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                Resource r = res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   314
    public Enumeration<Resource> getResources(final String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return getResources(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Returns the Loader at the specified position in the URL search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * path. The URLs are opened and expanded as needed. Returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * if the specified index is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     private synchronized Loader getLoader(int index) {
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   324
        if (closed) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   325
            return null;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   326
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         // Expand URL search path until the request can be satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         // or the URL stack is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        while (loaders.size() < index + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // Pop the next URL from the URL stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            synchronized (urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (urls.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   336
                    url = urls.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // Skip this URL if it already has a Loader. (Loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // may be null in the case where URL has not been opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // 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
   342
            String urlNoFragString = URLUtil.urlNoFragString(url);
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   343
            if (lmap.containsKey(urlNoFragString)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // Otherwise, create a new Loader for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                loader = getLoader(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                // If the loader defines a local class path then add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                // URLs to the list of URLs to be opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                URL[] urls = loader.getClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (urls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    push(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                // Silently ignore for now...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // Finally, add the Loader to the search path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            loaders.add(loader);
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   362
            lmap.put(urlNoFragString, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   364
        return loaders.get(index);
2
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
     * Returns the Loader for the specified base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private Loader getLoader(final URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   372
            return java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   373
                new java.security.PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   374
                public Loader run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    String file = url.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    if (file != null && file.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        if ("file".equals(url.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                            return new FileLoader(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                            return new Loader(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        return new JarLoader(url, jarHandler, lmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw (IOException)pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Pushes the specified URLs onto the list of unopened URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private void push(URL[] us) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        synchronized (urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            for (int i = us.length - 1; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                urls.push(us[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Check whether the resource URL should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Return null on security check failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Called by java.net.URLClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   408
    public static URL checkURL(URL url) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   409
        if (url != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   410
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   411
                check(url);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   412
            } catch (Exception e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   413
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   414
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return url;
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
     * Check whether the resource URL should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Throw exception on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Called internally within this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36251
diff changeset
   424
    public static void check(URL url) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            URLConnection urlConnection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            Permission perm = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if (perm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    security.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    // fallback to checkRead/checkConnect for pre 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    // security managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    if ((perm instanceof java.io.FilePermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        perm.getActions().indexOf("read") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        security.checkRead(perm.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    } else if ((perm instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        java.net.SocketPermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        perm.getActions().indexOf("connect") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        URL locUrl = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        security.checkConnect(locUrl.getHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                              locUrl.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Inner class used to represent a loader of resources and classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * from a base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   459
    private static class Loader implements Closeable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        private final URL base;
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   461
        private JarFile jarfile; // if this points to a jar file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * Creates a new Loader for the specified URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        Loader(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            base = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * Returns the base URL for this Loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        URL getBaseURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                 * For a HTTP connection we use the HEAD method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                 * check if the resource exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                URLConnection uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                if (uc instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    HttpURLConnection hconn = (HttpURLConnection)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    hconn.setRequestMethod("HEAD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    if (hconn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    // 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
   503
                    uc.setUseCaches(false);
45a0eb6ab4f7 7183373: URLClassloader.close() does not close JAR files whose resources have been loaded via getResource()
dingxmin
parents: 11131
diff changeset
   504
                    InputStream is = uc.getInputStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            final URLConnection uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                InputStream in = uc.getInputStream();
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   527
                if (uc instanceof JarURLConnection) {
9823
2bc6b0d4490e 7050028: ISE "zip file closed" from JarURLConnection.getInputStream on JDK 7 when !useCaches
michaelm
parents: 9035
diff changeset
   528
                    /* 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
   529
                     * in a hurry.
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   530
                     */
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   531
                    JarURLConnection juc = (JarURLConnection)uc;
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   532
                    jarfile = JarLoader.checkJar(juc.getJarFile());
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   533
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                public String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                public URL getURL() { return url; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                public URL getCodeSourceURL() { return base; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    return uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                public int getContentLength() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    return uc.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         * Returns the Resource for the specified name, or null if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         * found or the caller does not have the permission to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         * resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        Resource getResource(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return getResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        /*
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   560
         * close this loader and release all resources
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   561
         * method overridden in sub-classes
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   562
         */
7980
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   563
        public void close () throws IOException {
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   564
            if (jarfile != null) {
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   565
                jarfile.close();
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   566
            }
3af394bb6f59 6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents: 5506
diff changeset
   567
        }
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   568
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   569
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         * Returns the local class path for this loader, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        URL[] getClassPath() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Inner class used to represent a Loader of resources from a JAR URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    static class JarLoader extends Loader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        private JarFile jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        private URL csu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        private JarIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        private URLStreamHandler handler;
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   585
        private HashMap<String, Loader> lmap;
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   586
        private boolean closed = false;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 29986
diff changeset
   587
        private static final JavaUtilZipFileAccess zipAccess =
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 29986
diff changeset
   588
                SharedSecrets.getJavaUtilZipFileAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * Creates a new JarLoader for the specified URL referring to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   594
        JarLoader(URL url, URLStreamHandler jarHandler,
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   595
                  HashMap<String, Loader> loaderMap)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            super(new URL("jar", "", -1, url + "!/", jarHandler));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            csu = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            handler = jarHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            lmap = loaderMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
36000
86257b272c96 8068686: Remove meta-index support
chegar
parents: 32834
diff changeset
   603
            ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   606
        @Override
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   607
        public void close () throws IOException {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   608
            // closing is synchronized at higher level
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   609
            if (!closed) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   610
                closed = true;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   611
                // in case not already open.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   612
                ensureOpen();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   613
                jar.close();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   614
            }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   615
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   616
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        JarFile getJarFile () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        private boolean isOptimizable(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            return "file".equals(url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (jar == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   629
                        new java.security.PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   630
                            public Void run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                    System.err.println("Opening " + csu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                    Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                jar = getJarFile(csu);
36000
86257b272c96 8068686: Remove meta-index support
chegar
parents: 32834
diff changeset
   637
                                index = JarIndex.getJarIndex(jar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                                if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                    String[] jarfiles = index.getJarFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                // Add all the dependent URLs to the lmap so that loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                // will not be created for them by URLClassPath.getLoader(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                // if the same URL occurs later on the main class path.  We set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                // Loader to null here to avoid creating a Loader for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                // URL until we actually need to try to load something from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                    for(int i = 0; i < jarfiles.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                            URL jarURL = new URL(csu, jarfiles[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                                            // 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
   649
                                            String urlNoFragString = URLUtil.urlNoFragString(jarURL);
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   650
                                            if (!lmap.containsKey(urlNoFragString)) {
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   651
                                                lmap.put(urlNoFragString, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    throw (IOException)pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   668
        /* 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
   669
        static JarFile checkJar(JarFile jar) throws IOException {
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   670
            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
   671
                && !zipAccess.startsWithLocHeader(jar)) {
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   672
                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
   673
                try {
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   674
                    jar.close();
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   675
                } catch (IOException ex) {
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   676
                    x.addSuppressed(ex);
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   677
                }
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   678
                throw x;
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   679
            }
407ff7067681 8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents: 18223
diff changeset
   680
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   681
            return jar;
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   682
        }
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   683
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        private JarFile getJarFile(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            // Optimize case where url refers to a local jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (isOptimizable(url)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                FileURLMapper p = new FileURLMapper (url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                if (!p.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    throw new FileNotFoundException(p.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 36000
diff changeset
   691
                return checkJar(new JarFile(new File(p.getPath()), true, ZipFile.OPEN_READ,
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 36000
diff changeset
   692
                        JarFile.Release.RUNTIME));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 36000
diff changeset
   694
            URLConnection uc = (new URL(getBaseURL(), "#runtime")).openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION);
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   696
            JarFile jarFile = ((JarURLConnection)uc).getJarFile();
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 16023
diff changeset
   697
            return checkJar(jarFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * Returns the index of this JarLoader if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        JarIndex getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            } catch (IOException e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 9823
diff changeset
   707
                throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         * Creates the resource and if the check flag is set to true, checks if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
         * is its okay to return the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        Resource checkResource(final String name, boolean check,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            final JarEntry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            try {
36251
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   721
                if (jar.isMultiRelease()) {
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   722
                    // add #runtime fragment to tell JarURLConnection to use
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   723
                    // runtime versioning if the underlying jar file is multi-release
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   724
                    url = new URL(getBaseURL(), ParseUtil.encodePath(name, false) + "#runtime");
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   725
                } else {
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   726
                    url = new URL(getBaseURL(), ParseUtil.encodePath(name, false));
e909478d3e1d 8151339: Adding fragment to JAR URLs breaks ant
sdrach
parents: 36129
diff changeset
   727
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                // throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            } catch (AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                public String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                public URL getURL() { return url; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                public URL getCodeSourceURL() { return csu; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                public InputStream getInputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    { return jar.getInputStream(entry); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                public int getContentLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    { return (int)entry.getSize(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                public Manifest getManifest() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    { return jar.getManifest(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                public Certificate[] getCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    { return entry.getCertificates(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                public CodeSigner[] getCodeSigners()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    { return entry.getCodeSigners(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * Returns true iff atleast one resource in the jar file has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         * package name as that of the specified resource name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        boolean validIndex(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            String packageName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            int pos;
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   765
            if((pos = name.lastIndexOf('/')) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                packageName = name.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            String entryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            ZipEntry entry;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   771
            Enumeration<JarEntry> enum_ = jar.entries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            while (enum_.hasMoreElements()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   773
                entry = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                entryName = entry.getName();
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   775
                if((pos = entryName.lastIndexOf('/')) != -1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    entryName = entryName.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                if (entryName.equals(packageName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         * Returns the URL for a resource with the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            Resource rsc = getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (rsc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                return rsc.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * Returns the JAR Resource for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            } catch (IOException e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 9823
diff changeset
   802
                throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            final JarEntry entry = jar.getJarEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            if (entry != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                return checkResource(name, check, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if (index == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   811
            HashSet<String> visited = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            return getResource(name, check, visited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * Version of getResource() that tracks the jar files that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * visited by linking through the index files. This helper method uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         * a HashSet to store the URLs of jar files that have been searched and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * uses it to avoid going into an infinite loop, looking for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         * non-existent resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        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
   823
                             Set<String> visited) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            Resource res;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   826
            String[] jarFiles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            int count = 0;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   828
            LinkedList<String> jarFilesList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            /* If there no jar files in the index that can potential contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
             * this resource then return immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if((jarFilesList = index.get(name)) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                int size = jarFilesList.size();
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   838
                jarFiles = jarFilesList.toArray(new String[size]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                /* loop through the mapped jar file list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                while(count < size) {
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10355
diff changeset
   841
                    String jarName = jarFiles[count++];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    JarLoader newLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        url = new URL(csu, jarName);
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   847
                        String urlNoFragString = URLUtil.urlNoFragString(url);
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   848
                        if ((newLoader = (JarLoader)lmap.get(urlNoFragString)) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                            /* no loader has been set up for this jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                             * before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                             */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   852
                            newLoader = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   853
                                new PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   854
                                    public JarLoader run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                        return new JarLoader(url, handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                            lmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                            /* this newly opened jar file has its own index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                             * merge it into the parent's index, taking into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                             * account the relative path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                            JarIndex newIndex = newLoader.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                            if(newIndex != null) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   866
                                int pos = jarName.lastIndexOf('/');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                newIndex.merge(this.index, (pos == -1 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                    null : jarName.substring(0, pos + 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                            /* 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
   872
                            lmap.put(urlNoFragString, newLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    /* Note that the addition of the url to the list of visited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                     * jars incorporates a check for presence in the hashmap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                     */
3436
c7d276696c5b 6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents: 3073
diff changeset
   884
                    boolean visitedURL = !visited.add(URLUtil.urlNoFragString(url));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    if (!visitedURL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                            newLoader.ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                        } catch (IOException e) {
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 9823
diff changeset
   889
                            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                        final JarEntry entry = newLoader.jar.getJarEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                            return newLoader.checkResource(name, check, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        /* Verify that at least one other resource with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                         * same package name as the lookedup resource is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                         * present in the new jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                        if (!newLoader.validIndex(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                            /* the mapping is wrong */
36745
51effd3e92d0 8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents: 36674
diff changeset
   902
                            throw new InvalidJarIndexError("Invalid index");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    /* If newLoader is the current loader or if it is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                     * loader that has already been searched or if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                     * loader does not have an index then skip it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                     * and move on to the next loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    if (visitedURL || newLoader == this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                            newLoader.getIndex() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    /* Process the index of the new loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    if((res = newLoader.getResource(name, check, visited))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                            != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                // Get the list of jar files again as the list could have grown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                // due to merging of index files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                jarFilesList = index.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            // If the count is unchanged, we are done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            } while(count < jarFilesList.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         * Returns the JAR file local class path, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        URL[] getClassPath() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            ensureOpen();
15682
793a36de151d 8003255: (profiles) Update JAR file specification to support profiles
alanb
parents: 11131
diff changeset
   942
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (SharedSecrets.javaUtilJarAccess().jarFileHasClassPathAttribute(jar)) { // Only get manifest when necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                Manifest man = jar.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    Attributes attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                        String value = attr.getValue(Name.CLASS_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                            return parseClassPath(csu, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
         * Parses value of the Class-Path manifest attribute and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
         * an array of URLs relative to the specified base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        private URL[] parseClassPath(URL base, String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            StringTokenizer st = new StringTokenizer(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            URL[] urls = new URL[st.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                String path = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                urls[i] = new URL(base, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Inner class used to represent a loader of classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * from a file URL that refers to a directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    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
   982
        /* Canonicalized File */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        private File dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        FileLoader(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            super(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            if (!"file".equals(url.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                throw new IllegalArgumentException("url");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            String path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            path = ParseUtil.decode(path);
495
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
   992
            dir = (new File(path)).getCanonicalFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
         * Returns the URL for a resource with the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            Resource rsc = getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            if (rsc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                return rsc.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                URL normalizedBase = new URL(getBaseURL(), ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                url = new URL(getBaseURL(), ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                if (url.getFile().startsWith(normalizedBase.getFile()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    // requested resource had ../..'s in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                if (check)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    URLClassPath.check(url);
495
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1019
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1020
                final File file;
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1021
                if (name.indexOf("..") != -1) {
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1022
                    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
  1023
                          .getCanonicalFile();
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1024
                    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
  1025
                        /* outside of base dir */
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1026
                        return null;
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1027
                    }
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1028
                } else {
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1029
                    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
  1030
                }
d612e90c3ebc 6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents: 51
diff changeset
  1031
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                    return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                        public String getName() { return name; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        public URL getURL() { return url; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                        public URL getCodeSourceURL() { return getBaseURL(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                        public InputStream getInputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                            { return new FileInputStream(file); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        public int getContentLength() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                            { return (int)file.length(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
}