src/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java

Print this page




  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         if (url.getProtocol().equalsIgnoreCase("file")) {
  65             // Deal with UNC pathnames specially. See 4180841
  66 
  67             String host = url.getHost();
  68             if (host != null && !host.equals("") &&
  69                 !host.equalsIgnoreCase("localhost")) {
  70 
  71                 url = new URL("file", "", "//" + host + url.getPath());
  72             }
  73         }
  74 
  75         JarFile result = null;
  76         JarFile local_result = null;
  77 
  78         if (useCaches) {
  79             synchronized (this) {
  80                 result = getCachedJarFile(url);
  81             }
  82             if (result == null) {
  83                 local_result = URLJarFile.getJarFile(url, this);
  84                 synchronized (this) {
  85                     result = getCachedJarFile(url);
  86                     if (result == null) {
  87                         fileCache.put(URLUtil.urlNoFragString(url), local_result);
  88                         urlCache.put(local_result, url);
  89                         result = local_result;
  90                     } else {
  91                         if (local_result != null) {
  92                             local_result.close();
  93                         }
  94                     }
  95                 }
  96             }
  97         } else {
  98             result = URLJarFile.getJarFile(url, this);
  99         }
 100         if (result == null)
 101             throw new FileNotFoundException(url.toString());
 102 
 103         return result;
 104     }
 105 
 106     /**
 107      * Callback method of the URLJarFileCloseController to
 108      * indicate that the JarFile is close. This way we can
 109      * remove the JarFile from the cache
 110      */
 111     public void close(JarFile jarFile) {

 112         URL urlRemoved = urlCache.remove(jarFile);
 113         if( urlRemoved != null) {
 114                 fileCache.remove(URLUtil.urlNoFragString(urlRemoved));
 115         }
 116     }
 117 

 118     private JarFile getCachedJarFile(URL url) {
 119         JarFile result = fileCache.get(URLUtil.urlNoFragString(url));
 120 
 121         /* if the JAR file is cached, the permission will always be there */
 122         if (result != null) {
 123             Permission perm = getPermission(result);
 124             if (perm != null) {
 125                 SecurityManager sm = System.getSecurityManager();
 126                 if (sm != null) {
 127                     try {
 128                         sm.checkPermission(perm);
 129                     } catch (SecurityException se) {
 130                         // fallback to checkRead/checkConnect for pre 1.2
 131                         // security managers
 132                         if ((perm instanceof java.io.FilePermission) &&
 133                             perm.getActions().indexOf("read") != -1) {
 134                             sm.checkRead(perm.getName());
 135                         } else if ((perm instanceof
 136                             java.net.SocketPermission) &&
 137                             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         if (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;
  87         JarFile local_result;
  88 
  89         if (useCaches) {
  90             synchronized (instance) {
  91                 result = getCachedJarFile(url);
  92             }
  93             if (result == null) {
  94                 local_result = URLJarFile.getJarFile(url, this);
  95                 synchronized (instance) {
  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         synchronized (instance) {
 124             URL urlRemoved = urlCache.remove(jarFile);
 125             if( urlRemoved != null)
 126                 fileCache.remove(URLUtil.urlNoFragString(urlRemoved));
 127         }
 128     }
 129 
 130     // must be called while holding the 'instance' lock
 131     private JarFile getCachedJarFile(URL url) {
 132         JarFile result = fileCache.get(URLUtil.urlNoFragString(url));
 133 
 134         /* if the JAR file is cached, the permission will always be there */
 135         if (result != null) {
 136             Permission perm = getPermission(result);
 137             if (perm != null) {
 138                 SecurityManager sm = System.getSecurityManager();
 139                 if (sm != null) {
 140                     try {
 141                         sm.checkPermission(perm);
 142                     } catch (SecurityException se) {
 143                         // fallback to checkRead/checkConnect for pre 1.2
 144                         // security managers
 145                         if ((perm instanceof java.io.FilePermission) &&
 146                             perm.getActions().indexOf("read") != -1) {
 147                             sm.checkRead(perm.getName());
 148                         } else if ((perm instanceof
 149                             java.net.SocketPermission) &&
 150                             perm.getActions().indexOf("connect") != -1) {