001: /*
002: * Copyright 1997-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:
063: JarFile result = null;
064: JarFile local_result = null;
065:
066: if (useCaches) {
067: synchronized (this ) {
068: result = getCachedJarFile(url);
069: }
070: if (result == null) {
071: local_result = URLJarFile.getJarFile(url, this );
072: synchronized (this ) {
073: result = getCachedJarFile(url);
074: if (result == null) {
075: fileCache.put(url, local_result);
076: urlCache.put(local_result, url);
077: result = local_result;
078: } else {
079: if (local_result != null) {
080: local_result.close();
081: }
082: }
083: }
084: }
085: } else {
086: result = URLJarFile.getJarFile(url, this );
087: }
088: if (result == null)
089: throw new FileNotFoundException(url.toString());
090:
091: return result;
092: }
093:
094: /**
095: * Callback method of the URLJarFileCloseController to
096: * indicate that the JarFile is close. This way we can
097: * remove the JarFile from the cache
098: */
099: public void close(JarFile jarFile) {
100: URL urlRemoved = (URL) urlCache.remove(jarFile);
101: if (urlRemoved != null) {
102: fileCache.remove(urlRemoved);
103: }
104: }
105:
106: private JarFile getCachedJarFile(URL url) {
107: JarFile result = (JarFile) fileCache.get(url);
108:
109: /* if the JAR file is cached, the permission will always be there */
110: if (result != null) {
111: Permission perm = getPermission(result);
112: if (perm != null) {
113: SecurityManager sm = System.getSecurityManager();
114: if (sm != null) {
115: try {
116: sm.checkPermission(perm);
117: } catch (SecurityException se) {
118: // fallback to checkRead/checkConnect for pre 1.2
119: // security managers
120: if ((perm instanceof java.io.FilePermission)
121: && perm.getActions().indexOf("read") != -1) {
122: sm.checkRead(perm.getName());
123: } else if ((perm instanceof java.net.SocketPermission)
124: && perm.getActions().indexOf("connect") != -1) {
125: sm.checkConnect(url.getHost(), url
126: .getPort());
127: } else {
128: throw se;
129: }
130: }
131: }
132: }
133: }
134: return result;
135: }
136:
137: private Permission getPermission(JarFile jarFile) {
138: try {
139: URLConnection uc = (URLConnection) getConnection(jarFile);
140: if (uc != null)
141: return uc.getPermission();
142: } catch (IOException ioe) {
143: // gulp
144: }
145:
146: return null;
147: }
148: }
|