0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.net.www.protocol.jar;
0N/A
1490N/Aimport java.io.IOException;
1490N/Aimport java.io.FileNotFoundException;
1490N/Aimport java.net.URL;
1490N/Aimport java.net.URLConnection;
1490N/Aimport java.util.HashMap;
1490N/Aimport java.util.jar.JarFile;
0N/Aimport java.security.Permission;
1490N/Aimport sun.net.util.URLUtil;
0N/A
0N/A/* A factory for cached JAR file. This class is used to both retrieve
0N/A * and cache Jar files.
0N/A *
0N/A * @author Benjamin Renaud
0N/A * @since JDK1.2
0N/A */
0N/Aclass JarFileFactory implements URLJarFile.URLJarFileCloseController {
0N/A
0N/A /* the url to file cache */
5844N/A private static final HashMap<String, JarFile> fileCache = new HashMap<>();
0N/A
0N/A /* the file to url cache */
5844N/A private static final HashMap<JarFile, URL> urlCache = new HashMap<>();
5844N/A
5844N/A private static final JarFileFactory instance = new JarFileFactory();
5844N/A
5844N/A private JarFileFactory() { }
5844N/A
5844N/A public static JarFileFactory getInstance() {
5844N/A return instance;
5844N/A }
0N/A
0N/A URLConnection getConnection(JarFile jarFile) throws IOException {
5844N/A URL u;
5844N/A synchronized (instance) {
5844N/A u = urlCache.get(jarFile);
5844N/A }
0N/A if (u != null)
0N/A return u.openConnection();
0N/A
0N/A return null;
0N/A }
0N/A
0N/A public JarFile get(URL url) throws IOException {
0N/A return get(url, true);
0N/A }
0N/A
0N/A JarFile get(URL url, boolean useCaches) throws IOException {
0N/A
5844N/A JarFile result;
5844N/A JarFile local_result;
0N/A
0N/A if (useCaches) {
5844N/A synchronized (instance) {
0N/A result = getCachedJarFile(url);
0N/A }
0N/A if (result == null) {
0N/A local_result = URLJarFile.getJarFile(url, this);
5844N/A synchronized (instance) {
0N/A result = getCachedJarFile(url);
0N/A if (result == null) {
1490N/A fileCache.put(URLUtil.urlNoFragString(url), local_result);
0N/A urlCache.put(local_result, url);
0N/A result = local_result;
0N/A } else {
0N/A if (local_result != null) {
0N/A local_result.close();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A } else {
0N/A result = URLJarFile.getJarFile(url, this);
0N/A }
0N/A if (result == null)
0N/A throw new FileNotFoundException(url.toString());
0N/A
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Callback method of the URLJarFileCloseController to
0N/A * indicate that the JarFile is close. This way we can
0N/A * remove the JarFile from the cache
0N/A */
0N/A public void close(JarFile jarFile) {
5844N/A synchronized (instance) {
5844N/A URL urlRemoved = urlCache.remove(jarFile);
5844N/A if (urlRemoved != null)
1490N/A fileCache.remove(URLUtil.urlNoFragString(urlRemoved));
0N/A }
0N/A }
0N/A
0N/A private JarFile getCachedJarFile(URL url) {
5844N/A assert Thread.holdsLock(instance);
1490N/A JarFile result = fileCache.get(URLUtil.urlNoFragString(url));
0N/A
0N/A /* if the JAR file is cached, the permission will always be there */
0N/A if (result != null) {
0N/A Permission perm = getPermission(result);
0N/A if (perm != null) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A try {
0N/A sm.checkPermission(perm);
0N/A } catch (SecurityException se) {
0N/A // fallback to checkRead/checkConnect for pre 1.2
0N/A // security managers
0N/A if ((perm instanceof java.io.FilePermission) &&
0N/A perm.getActions().indexOf("read") != -1) {
0N/A sm.checkRead(perm.getName());
0N/A } else if ((perm instanceof
0N/A java.net.SocketPermission) &&
0N/A perm.getActions().indexOf("connect") != -1) {
0N/A sm.checkConnect(url.getHost(), url.getPort());
0N/A } else {
0N/A throw se;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A private Permission getPermission(JarFile jarFile) {
0N/A try {
0N/A URLConnection uc = getConnection(jarFile);
0N/A if (uc != null)
0N/A return uc.getPermission();
0N/A } catch (IOException ioe) {
0N/A // gulp
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A}