jdk/src/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 5506 202f599c92aa
child 16491 272ad530ce3a
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
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 */
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    46
    private static HashMap<String, JarFile> fileCache = new HashMap<String, JarFile>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* the file to url cache */
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    49
    private static HashMap<JarFile, URL> urlCache = new HashMap<JarFile, URL>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    URLConnection getConnection(JarFile jarFile) throws IOException {
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    52
        URL u = urlCache.get(jarFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (u != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            return u.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public JarFile get(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return get(url, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    JarFile get(URL url, boolean useCaches) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (url.getProtocol().equalsIgnoreCase("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // Deal with UNC pathnames specially. See 4180841
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (host != null && !host.equals("") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                !host.equalsIgnoreCase("localhost")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                url = new URL("file", "", "//" + host + url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        JarFile result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        JarFile local_result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                result = getCachedJarFile(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                local_result = URLJarFile.getJarFile(url, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    result = getCachedJarFile(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    if (result == null) {
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
    87
                        fileCache.put(URLUtil.urlNoFragString(url), local_result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        urlCache.put(local_result, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        result = local_result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        if (local_result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                            local_result.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            result = URLJarFile.getJarFile(url, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new FileNotFoundException(url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return result;
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
     * Callback method of the URLJarFileCloseController to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * indicate that the JarFile is close. This way we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * remove the JarFile from the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void close(JarFile jarFile) {
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
   112
        URL urlRemoved = urlCache.remove(jarFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if( urlRemoved != null) {
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
   114
                fileCache.remove(URLUtil.urlNoFragString(urlRemoved));
2
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
    private JarFile getCachedJarFile(URL url) {
3437
5284a93427b6 6826801: JarFileFactory should not use HashMap<URL>
chegar
parents: 2
diff changeset
   119
        JarFile result = fileCache.get(URLUtil.urlNoFragString(url));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        /* if the JAR file is cached, the permission will always be there */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Permission perm = getPermission(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (perm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        // fallback to checkRead/checkConnect for pre 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        // security managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        if ((perm instanceof java.io.FilePermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            perm.getActions().indexOf("read") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            sm.checkRead(perm.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        } else if ((perm instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                            java.net.SocketPermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            perm.getActions().indexOf("connect") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                            sm.checkConnect(url.getHost(), url.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                            throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private Permission getPermission(JarFile jarFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        try {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   151
            URLConnection uc = getConnection(jarFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (uc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                return uc.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // gulp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}