001: /*
002: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.net.www.protocol.jar;
027:
028: import java.io.*;
029: import java.net.*;
030: import java.util.*;
031: import java.util.jar.*;
032: import java.util.zip.ZipFile;
033: import java.security.Permission;
034:
035: /* A factory for cached JAR file. This class is used to both retrieve
036: * and cache Jar files.
037: *
038: * @author Benjamin Renaud
039: * @since JDK1.2
040: */
041: class JarFileFactory implements URLJarFile.URLJarFileCloseController {
042:
043: /* the url to file cache */
044: private static HashMap fileCache = new HashMap();
045:
046: /* the file to url cache */
047: private static HashMap urlCache = new HashMap();
048:
049: URLConnection getConnection(JarFile jarFile) throws IOException {
050: URL u = (URL) urlCache.get(jarFile);
051: if (u != null)
052: return u.openConnection();
053:
054: return null;
055: }
056:
057: public JarFile get(URL url) throws IOException {
058: return get(url, true);
059: }
060:
061: JarFile get(URL url, boolean useCaches) throws IOException {
062: if (url.getProtocol().equalsIgnoreCase("file")) {
063: // Deal with UNC pathnames specially. See 4180841
064:
065: String host = url.getHost();
066: if (host != null && !host.equals("")
067: && !host.equalsIgnoreCase("localhost")) {
068:
069: url = new URL("file", "", "//" + host + url.getPath());
070: }
071: }
072:
073: JarFile result = null;
074: JarFile local_result = null;
075:
076: if (useCaches) {
077: synchronized (this ) {
078: result = getCachedJarFile(url);
079: }
080: if (result == null) {
081: local_result = URLJarFile.getJarFile(url, this );
082: synchronized (this ) {
083: result = getCachedJarFile(url);
084: if (result == null) {
085: fileCache.put(url, local_result);
086: urlCache.put(local_result, url);
087: result = local_result;
088: } else {
089: if (local_result != null) {
090: local_result.close();
091: }
092: }
093: }
094: }
095: } else {
096: result = URLJarFile.getJarFile(url, this );
097: }
098: if (result == null)
099: throw new FileNotFoundException(url.toString());
100:
101: return result;
102: }
103:
104: /**
105: * Callback method of the URLJarFileCloseController to
106: * indicate that the JarFile is close. This way we can
107: * remove the JarFile from the cache
108: */
109: public void close(JarFile jarFile) {
110: URL urlRemoved = (URL) urlCache.remove(jarFile);
111: if (urlRemoved != null) {
112: fileCache.remove(urlRemoved);
113: }
114: }
115:
116: private JarFile getCachedJarFile(URL url) {
117: JarFile result = (JarFile) fileCache.get(url);
118:
119: /* if the JAR file is cached, the permission will always be there */
120: if (result != null) {
121: Permission perm = getPermission(result);
122: if (perm != null) {
123: SecurityManager sm = System.getSecurityManager();
124: if (sm != null) {
125: try {
126: sm.checkPermission(perm);
127: } catch (SecurityException se) {
128: // fallback to checkRead/checkConnect for pre 1.2
129: // security managers
130: if ((perm instanceof java.io.FilePermission)
131: && perm.getActions().indexOf("read") != -1) {
132: sm.checkRead(perm.getName());
133: } else if ((perm instanceof java.net.SocketPermission)
134: && perm.getActions().indexOf("connect") != -1) {
135: sm.checkConnect(url.getHost(), url
136: .getPort());
137: } else {
138: throw se;
139: }
140: }
141: }
142: }
143: }
144: return result;
145: }
146:
147: private Permission getPermission(JarFile jarFile) {
148: try {
149: URLConnection uc = (URLConnection) getConnection(jarFile);
150: if (uc != null)
151: return uc.getPermission();
152: } catch (IOException ioe) {
153: // gulp
154: }
155:
156: return null;
157: }
158: }
|