26 package sun.net.www.protocol.jar; 27 28 import java.io.IOException; 29 import java.io.FileNotFoundException; 30 import java.net.URL; 31 import java.net.URLConnection; 32 import java.util.HashMap; 33 import java.util.jar.JarFile; 34 import java.security.Permission; 35 import sun.net.util.URLUtil; 36 37 /* A factory for cached JAR file. This class is used to both retrieve 38 * and cache Jar files. 39 * 40 * @author Benjamin Renaud 41 * @since JDK1.2 42 */ 43 class JarFileFactory implements URLJarFile.URLJarFileCloseController { 44 45 /* the url to file cache */ 46 private static HashMap<String, JarFile> fileCache = new HashMap<String, JarFile>(); 47 48 /* the file to url cache */ 49 private static HashMap<JarFile, URL> urlCache = new HashMap<JarFile, URL>(); 50 51 URLConnection getConnection(JarFile jarFile) throws IOException { 52 URL u = urlCache.get(jarFile); 53 if (u != null) 54 return u.openConnection(); 55 56 return null; 57 } 58 59 public JarFile get(URL url) throws IOException { 60 return get(url, true); 61 } 62 63 JarFile get(URL url, boolean useCaches) throws IOException { 64 65 JarFile result = null; 66 JarFile local_result = null; 67 68 if (useCaches) { 69 synchronized (this) { 70 result = getCachedJarFile(url); 71 } 72 if (result == null) { 73 local_result = URLJarFile.getJarFile(url, this); 74 synchronized (this) { 75 result = getCachedJarFile(url); 76 if (result == null) { 77 fileCache.put(URLUtil.urlNoFragString(url), local_result); 78 urlCache.put(local_result, url); 79 result = local_result; 80 } else { 81 if (local_result != null) { 82 local_result.close(); 83 } 84 } 85 } 86 } 87 } else { 88 result = URLJarFile.getJarFile(url, this); 89 } 90 if (result == null) 91 throw new FileNotFoundException(url.toString()); 92 93 return result; 94 } 95 96 /** 97 * Callback method of the URLJarFileCloseController to 98 * indicate that the JarFile is close. This way we can 99 * remove the JarFile from the cache 100 */ 101 public void close(JarFile jarFile) { 102 URL urlRemoved = urlCache.remove(jarFile); 103 if( urlRemoved != null) { 104 fileCache.remove(URLUtil.urlNoFragString(urlRemoved)); 105 } 106 } 107 108 109 private JarFile getCachedJarFile(URL url) { 110 JarFile result = fileCache.get(URLUtil.urlNoFragString(url)); 111 112 /* if the JAR file is cached, the permission will always be there */ 113 if (result != null) { 114 Permission perm = getPermission(result); 115 if (perm != null) { 116 SecurityManager sm = System.getSecurityManager(); 117 if (sm != null) { 118 try { 119 sm.checkPermission(perm); 120 } catch (SecurityException se) { 121 // fallback to checkRead/checkConnect for pre 1.2 122 // security managers 123 if ((perm instanceof java.io.FilePermission) && 124 perm.getActions().indexOf("read") != -1) { 125 sm.checkRead(perm.getName()); 126 } else if ((perm instanceof 127 java.net.SocketPermission) && 128 perm.getActions().indexOf("connect") != -1) { | 26 package sun.net.www.protocol.jar; 27 28 import java.io.IOException; 29 import java.io.FileNotFoundException; 30 import java.net.URL; 31 import java.net.URLConnection; 32 import java.util.HashMap; 33 import java.util.jar.JarFile; 34 import java.security.Permission; 35 import sun.net.util.URLUtil; 36 37 /* A factory for cached JAR file. This class is used to both retrieve 38 * and cache Jar files. 39 * 40 * @author Benjamin Renaud 41 * @since JDK1.2 42 */ 43 class JarFileFactory implements URLJarFile.URLJarFileCloseController { 44 45 /* the url to file cache */ 46 private static final HashMap<String, JarFile> fileCache = new HashMap<>(); 47 48 /* the file to url cache */ 49 private static final HashMap<JarFile, URL> urlCache = new HashMap<>(); 50 51 private static final JarFileFactory instance = new JarFileFactory(); 52 53 private JarFileFactory() { } 54 55 public static JarFileFactory getInstance() { 56 return instance; 57 } 58 59 URLConnection getConnection(JarFile jarFile) throws IOException { 60 URL u; 61 synchronized (instance) { 62 u = urlCache.get(jarFile); 63 } 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 76 JarFile result; 77 JarFile local_result; 78 79 if (useCaches) { 80 synchronized (instance) { 81 result = getCachedJarFile(url); 82 } 83 if (result == null) { 84 local_result = URLJarFile.getJarFile(url, this); 85 synchronized (instance) { 86 result = getCachedJarFile(url); 87 if (result == null) { 88 fileCache.put(URLUtil.urlNoFragString(url), local_result); 89 urlCache.put(local_result, url); 90 result = local_result; 91 } else { 92 if (local_result != null) { 93 local_result.close(); 94 } 95 } 96 } 97 } 98 } else { 99 result = URLJarFile.getJarFile(url, this); 100 } 101 if (result == null) 102 throw new FileNotFoundException(url.toString()); 103 104 return result; 105 } 106 107 /** 108 * Callback method of the URLJarFileCloseController to 109 * indicate that the JarFile is close. This way we can 110 * remove the JarFile from the cache 111 */ 112 public void close(JarFile jarFile) { 113 synchronized (instance) { 114 URL urlRemoved = urlCache.remove(jarFile); 115 if( urlRemoved != null) 116 fileCache.remove(URLUtil.urlNoFragString(urlRemoved)); 117 } 118 } 119 120 // must be called while holding the 'instance' lock 121 private JarFile getCachedJarFile(URL url) { 122 JarFile result = fileCache.get(URLUtil.urlNoFragString(url)); 123 124 /* if the JAR file is cached, the permission will always be there */ 125 if (result != null) { 126 Permission perm = getPermission(result); 127 if (perm != null) { 128 SecurityManager sm = System.getSecurityManager(); 129 if (sm != null) { 130 try { 131 sm.checkPermission(perm); 132 } catch (SecurityException se) { 133 // fallback to checkRead/checkConnect for pre 1.2 134 // security managers 135 if ((perm instanceof java.io.FilePermission) && 136 perm.getActions().indexOf("read") != -1) { 137 sm.checkRead(perm.getName()); 138 } else if ((perm instanceof 139 java.net.SocketPermission) && 140 perm.getActions().indexOf("connect") != -1) { |