jdk/src/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java
author chegar
Wed, 20 Mar 2013 14:39:20 +0000
changeset 16491 272ad530ce3a
parent 10596 39b3a979e600
child 23010 6dadb192ad81
permissions -rw-r--r--
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe Reviewed-by: khazra, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, 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: 3437
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: 3437
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: 3437
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3437
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3437
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www.protocol.jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    28
import java.io.IOException;
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    29
import java.io.FileNotFoundException;
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    30
import java.net.URL;
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    31
import java.net.URLConnection;
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    32
import java.util.HashMap;
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    33
import java.util.jar.JarFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Permission;
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    35
import sun.net.util.URLUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/* A factory for cached JAR file. This class is used to both retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and cache Jar files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since JDK1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class JarFileFactory implements URLJarFile.URLJarFileCloseController {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /* the url to file cache */
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    46
    private static final HashMap<String, JarFile> fileCache = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* the file to url cache */
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    49
    private static final HashMap<JarFile, URL> urlCache = new HashMap<>();
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    50
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    51
    private static final JarFileFactory instance = new JarFileFactory();
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    52
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    53
    private JarFileFactory() { }
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    54
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    55
    public static JarFileFactory getInstance() {
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    56
        return instance;
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    57
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    URLConnection getConnection(JarFile jarFile) throws IOException {
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    60
        URL u;
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    61
        synchronized (instance) {
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    62
            u = urlCache.get(jarFile);
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    63
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (u != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return u.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public JarFile get(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return get(url, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    JarFile get(URL url, boolean useCaches) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (url.getProtocol().equalsIgnoreCase("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // Deal with UNC pathnames specially. See 4180841
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (host != null && !host.equals("") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                !host.equalsIgnoreCase("localhost")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                url = new URL("file", "", "//" + host + url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    86
        JarFile result;
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    87
        JarFile local_result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (useCaches) {
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    90
            synchronized (instance) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                result = getCachedJarFile(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                local_result = URLJarFile.getJarFile(url, this);
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
    95
                synchronized (instance) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    result = getCachedJarFile(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    if (result == null) {
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    98
                        fileCache.put(URLUtil.urlNoFragString(url), local_result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        urlCache.put(local_result, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        result = local_result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        if (local_result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                            local_result.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            result = URLJarFile.getJarFile(url, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new FileNotFoundException(url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Callback method of the URLJarFileCloseController to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * indicate that the JarFile is close. This way we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * remove the JarFile from the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void close(JarFile jarFile) {
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
   123
        synchronized (instance) {
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
   124
            URL urlRemoved = urlCache.remove(jarFile);
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
   125
            if (urlRemoved != null)
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
   126
                fileCache.remove(URLUtil.urlNoFragString(urlRemoved));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private JarFile getCachedJarFile(URL url) {
16491
272ad530ce3a 8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents: 10596
diff changeset
   131
        assert Thread.holdsLock(instance);
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
   132
        JarFile result = fileCache.get(URLUtil.urlNoFragString(url));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /* if the JAR file is cached, the permission will always be there */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            Permission perm = getPermission(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (perm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        // fallback to checkRead/checkConnect for pre 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        // security managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        if ((perm instanceof java.io.FilePermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            perm.getActions().indexOf("read") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            sm.checkRead(perm.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        } else if ((perm instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                            java.net.SocketPermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                            perm.getActions().indexOf("connect") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            sm.checkConnect(url.getHost(), url.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                            throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private Permission getPermission(JarFile jarFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   164
            URLConnection uc = getConnection(jarFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (uc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return uc.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // gulp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}