~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/net/www/protocol/jar/JarFileFactory.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
4
 *
 
5
 * This code is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 2 only, as
 
7
 * published by the Free Software Foundation.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle in the LICENSE file that accompanied this code.
 
10
 *
 
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * version 2 for more details (a copy is included in the LICENSE file that
 
15
 * accompanied this code).
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License version
 
18
 * 2 along with this work; if not, write to the Free Software Foundation,
 
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 
22
 * or visit www.oracle.com if you need additional information or have any
 
23
 * questions.
 
24
 */
 
25
 
 
26
/*IKVM*/
 
27
/*
 
28
 * Modified for IKVM by Jeroen Frijters on May 22, 2007.
 
29
 * 
 
30
 * This is a merged version of the Windows & Solaris platform specific versions.
 
31
 * Since the IKVM class library binary can be used both on Windows and on *nix,
 
32
 * I've merged the platform specific classes into a generic class that at
 
33
 * runtime determines if it runs on Windows or not.
 
34
 * 
 
35
/*IKVM*/
 
36
 
 
37
package sun.net.www.protocol.jar;
 
38
 
 
39
import java.io.IOException;
 
40
import java.io.FileNotFoundException;
 
41
import java.net.URL;
 
42
import java.net.URLConnection;
 
43
import java.util.HashMap;
 
44
import java.util.jar.JarFile;
 
45
import java.security.Permission;
 
46
import sun.net.util.URLUtil;
 
47
 
 
48
/* A factory for cached JAR file. This class is used to both retrieve
 
49
 * and cache Jar files.
 
50
 *
 
51
 * @author Benjamin Renaud
 
52
 * @since JDK1.2
 
53
 */
 
54
class JarFileFactory implements URLJarFile.URLJarFileCloseController {
 
55
 
 
56
    /* the url to file cache */
 
57
    private static HashMap<String, JarFile> fileCache = new HashMap<String, JarFile>();
 
58
 
 
59
    /* the file to url cache */
 
60
    private static HashMap<JarFile, URL> urlCache = new HashMap<JarFile, URL>();
 
61
 
 
62
    URLConnection getConnection(JarFile jarFile) throws IOException {
 
63
        URL u = urlCache.get(jarFile);
 
64
        if (u != null)
 
65
            return u.openConnection();
 
66
 
 
67
        return null;
 
68
    }
 
69
 
 
70
    public JarFile get(URL url) throws IOException {
 
71
        return get(url, true);
 
72
    }
 
73
 
 
74
    JarFile get(URL url, boolean useCaches) throws IOException {
 
75
        if (ikvm.internal.Util.WINDOWS && url.getProtocol().equalsIgnoreCase("file")) {
 
76
            // Deal with UNC pathnames specially. See 4180841
 
77
 
 
78
            String host = url.getHost();
 
79
            if (host != null && !host.equals("") &&
 
80
                !host.equalsIgnoreCase("localhost")) {
 
81
 
 
82
                url = new URL("file", "", "//" + host + url.getPath());
 
83
            }
 
84
        }
 
85
 
 
86
        JarFile result = null;
 
87
        JarFile local_result = null;
 
88
 
 
89
        if (useCaches) {
 
90
            synchronized (this) {
 
91
                result = getCachedJarFile(url);
 
92
            }
 
93
            if (result == null) {
 
94
                local_result = URLJarFile.getJarFile(url, this);
 
95
                synchronized (this) {
 
96
                    result = getCachedJarFile(url);
 
97
                    if (result == null) {
 
98
                        fileCache.put(URLUtil.urlNoFragString(url), local_result);
 
99
                        urlCache.put(local_result, url);
 
100
                        result = local_result;
 
101
                    } else {
 
102
                        if (local_result != null) {
 
103
                            local_result.close();
 
104
                        }
 
105
                    }
 
106
                }
 
107
            }
 
108
        } else {
 
109
            result = URLJarFile.getJarFile(url, this);
 
110
        }
 
111
        if (result == null)
 
112
            throw new FileNotFoundException(url.toString());
 
113
 
 
114
        return result;
 
115
    }
 
116
 
 
117
    /**
 
118
     * Callback method of the URLJarFileCloseController to
 
119
     * indicate that the JarFile is close. This way we can
 
120
     * remove the JarFile from the cache
 
121
     */
 
122
    public void close(JarFile jarFile) {
 
123
        URL urlRemoved = urlCache.remove(jarFile);
 
124
        if( urlRemoved != null) {
 
125
                fileCache.remove(URLUtil.urlNoFragString(urlRemoved));
 
126
        }
 
127
    }
 
128
 
 
129
    private JarFile getCachedJarFile(URL url) {
 
130
        JarFile result = fileCache.get(URLUtil.urlNoFragString(url));
 
131
 
 
132
        /* if the JAR file is cached, the permission will always be there */
 
133
        if (result != null) {
 
134
            Permission perm = getPermission(result);
 
135
            if (perm != null) {
 
136
                SecurityManager sm = System.getSecurityManager();
 
137
                if (sm != null) {
 
138
                    try {
 
139
                        sm.checkPermission(perm);
 
140
                    } catch (SecurityException se) {
 
141
                        // fallback to checkRead/checkConnect for pre 1.2
 
142
                        // security managers
 
143
                        if ((perm instanceof java.io.FilePermission) &&
 
144
                            perm.getActions().indexOf("read") != -1) {
 
145
                            sm.checkRead(perm.getName());
 
146
                        } else if ((perm instanceof
 
147
                            java.net.SocketPermission) &&
 
148
                            perm.getActions().indexOf("connect") != -1) {
 
149
                            sm.checkConnect(url.getHost(), url.getPort());
 
150
                        } else {
 
151
                            throw se;
 
152
                        }
 
153
                    }
 
154
                }
 
155
            }
 
156
        }
 
157
        return result;
 
158
    }
 
159
 
 
160
    private Permission getPermission(JarFile jarFile) {
 
161
        try {
 
162
            URLConnection uc = (URLConnection)getConnection(jarFile);
 
163
            if (uc != null)
 
164
                return uc.getPermission();
 
165
        } catch (IOException ioe) {
 
166
            // gulp
 
167
        }
 
168
 
 
169
        return null;
 
170
    }
 
171
}