J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.net.www.protocol.jar; |
| 27 | |
| 28 | import java.io.*; |
| 29 | import java.net.*; |
| 30 | import java.util.*; |
| 31 | import java.util.jar.*; |
| 32 | import java.util.zip.ZipFile; |
| 33 | import java.security.Permission; |
| 34 | |
| 35 | /* A factory for cached JAR file. This class is used to both retrieve |
| 36 | * and cache Jar files. |
| 37 | * |
| 38 | * @author Benjamin Renaud |
| 39 | * @since JDK1.2 |
| 40 | */ |
| 41 | class JarFileFactory implements URLJarFile.URLJarFileCloseController { |
| 42 | |
| 43 | /* the url to file cache */ |
| 44 | private static HashMap fileCache = new HashMap(); |
| 45 | |
| 46 | /* the file to url cache */ |
| 47 | private static HashMap urlCache = new HashMap(); |
| 48 | |
| 49 | URLConnection getConnection(JarFile jarFile) throws IOException { |
| 50 | URL u = (URL)urlCache.get(jarFile); |
| 51 | if (u != null) |
| 52 | return u.openConnection(); |
| 53 | |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | public JarFile get(URL url) throws IOException { |
| 58 | return get(url, true); |
| 59 | } |
| 60 | |
| 61 | JarFile get(URL url, boolean useCaches) throws IOException { |
| 62 | |
| 63 | JarFile result = null; |
| 64 | JarFile local_result = null; |
| 65 | |
| 66 | if (useCaches) { |
| 67 | synchronized (this) { |
| 68 | result = getCachedJarFile(url); |
| 69 | } |
| 70 | if (result == null) { |
| 71 | local_result = URLJarFile.getJarFile(url, this); |
| 72 | synchronized (this) { |
| 73 | result = getCachedJarFile(url); |
| 74 | if (result == null) { |
| 75 | fileCache.put(url, local_result); |
| 76 | urlCache.put(local_result, url); |
| 77 | result = local_result; |
| 78 | } else { |
| 79 | if (local_result != null) { |
| 80 | local_result.close(); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } else { |
| 86 | result = URLJarFile.getJarFile(url, this); |
| 87 | } |
| 88 | if (result == null) |
| 89 | throw new FileNotFoundException(url.toString()); |
| 90 | |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Callback method of the URLJarFileCloseController to |
| 96 | * indicate that the JarFile is close. This way we can |
| 97 | * remove the JarFile from the cache |
| 98 | */ |
| 99 | public void close(JarFile jarFile) { |
| 100 | URL urlRemoved = (URL) urlCache.remove(jarFile); |
| 101 | if( urlRemoved != null) { |
| 102 | fileCache.remove(urlRemoved); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | private JarFile getCachedJarFile(URL url) { |
| 108 | JarFile result = (JarFile)fileCache.get(url); |
| 109 | |
| 110 | /* if the JAR file is cached, the permission will always be there */ |
| 111 | if (result != null) { |
| 112 | Permission perm = getPermission(result); |
| 113 | if (perm != null) { |
| 114 | SecurityManager sm = System.getSecurityManager(); |
| 115 | if (sm != null) { |
| 116 | try { |
| 117 | sm.checkPermission(perm); |
| 118 | } catch (SecurityException se) { |
| 119 | // fallback to checkRead/checkConnect for pre 1.2 |
| 120 | // security managers |
| 121 | if ((perm instanceof java.io.FilePermission) && |
| 122 | perm.getActions().indexOf("read") != -1) { |
| 123 | sm.checkRead(perm.getName()); |
| 124 | } else if ((perm instanceof |
| 125 | java.net.SocketPermission) && |
| 126 | perm.getActions().indexOf("connect") != -1) { |
| 127 | sm.checkConnect(url.getHost(), url.getPort()); |
| 128 | } else { |
| 129 | throw se; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | private Permission getPermission(JarFile jarFile) { |
| 139 | try { |
| 140 | URLConnection uc = getConnection(jarFile); |
| 141 | if (uc != null) |
| 142 | return uc.getPermission(); |
| 143 | } catch (IOException ioe) { |
| 144 | // gulp |
| 145 | } |
| 146 | |
| 147 | return null; |
| 148 | } |
| 149 | } |