diff --git a/make/sun/net/FILES_java.gmk b/make/sun/net/FILES_java.gmk index 3d729947667627dadd9fbc8521fdd219685e5f77..5082c3d370f152b5dca162ab8e1c724152a57ab6 100644 --- a/make/sun/net/FILES_java.gmk +++ b/make/sun/net/FILES_java.gmk @@ -41,6 +41,7 @@ FILES_java = \ sun/net/NetProperties.java \ sun/net/NetHooks.java \ sun/net/util/IPAddressUtil.java \ + sun/net/util/URLUtil.java \ sun/net/dns/ResolverConfiguration.java \ sun/net/dns/ResolverConfigurationImpl.java \ sun/net/ftp/FtpClient.java \ diff --git a/src/share/classes/sun/misc/URLClassPath.java b/src/share/classes/sun/misc/URLClassPath.java index d9b1035ba6daaf01654da9ad8675cba893cfa403..5c60e2fe81d390be4405ab363e346a78b393e5e3 100644 --- a/src/share/classes/sun/misc/URLClassPath.java +++ b/src/share/classes/sun/misc/URLClassPath.java @@ -51,6 +51,7 @@ import java.security.PrivilegedAction; import java.security.PrivilegedExceptionAction; import java.security.cert.Certificate; import sun.misc.FileURLMapper; +import sun.net.util.URLUtil; /** * This class is used to maintain a search path of URLs for loading classes @@ -80,7 +81,7 @@ public class URLClassPath { ArrayList loaders = new ArrayList(); /* Map of each URL opened to its corresponding Loader */ - HashMap lmap = new HashMap(); + HashMap lmap = new HashMap(); /* The jar protocol handler to use when creating new URLs */ private URLStreamHandler jarHandler; @@ -317,7 +318,8 @@ public class URLClassPath { // Skip this URL if it already has a Loader. (Loader // may be null in the case where URL has not been opened // but is referenced by a JAR index.) - if (lmap.containsKey(url)) { + String urlNoFragString = URLUtil.urlNoFragString(url); + if (lmap.containsKey(urlNoFragString)) { continue; } // Otherwise, create a new Loader for the URL. @@ -336,7 +338,7 @@ public class URLClassPath { } // Finally, add the Loader to the search path. loaders.add(loader); - lmap.put(url, loader); + lmap.put(urlNoFragString, loader); } return loaders.get(index); } @@ -576,7 +578,7 @@ public class URLClassPath { private JarIndex index; private MetaIndex metaIndex; private URLStreamHandler handler; - private HashMap lmap; + private HashMap lmap; private boolean closed = false; /* @@ -584,7 +586,7 @@ public class URLClassPath { * a JAR file. */ JarLoader(URL url, URLStreamHandler jarHandler, - HashMap loaderMap) + HashMap loaderMap) throws IOException { super(new URL("jar", "", -1, url + "!/", jarHandler)); @@ -663,8 +665,9 @@ public class URLClassPath { try { URL jarURL = new URL(csu, jarfiles[i]); // If a non-null loader already exists, leave it alone. - if (!lmap.containsKey(jarURL)) { - lmap.put(jarURL, null); + String urlNoFragString = URLUtil.urlNoFragString(jarURL); + if (!lmap.containsKey(urlNoFragString)) { + lmap.put(urlNoFragString, null); } } catch (MalformedURLException e) { continue; @@ -806,7 +809,7 @@ public class URLClassPath { if (index == null) return null; - HashSet visited = new HashSet(); + HashSet visited = new HashSet(); return getResource(name, check, visited); } @@ -818,7 +821,7 @@ public class URLClassPath { * non-existent resource */ Resource getResource(final String name, boolean check, - Set visited) { + Set visited) { Resource res; Object[] jarFiles; @@ -843,7 +846,8 @@ public class URLClassPath { try{ url = new URL(csu, jarName); - if ((newLoader = (JarLoader)lmap.get(url)) == null) { + String urlNoFragString = URLUtil.urlNoFragString(url); + if ((newLoader = (JarLoader)lmap.get(urlNoFragString)) == null) { /* no loader has been set up for this jar file * before */ @@ -867,7 +871,7 @@ public class URLClassPath { } /* put it in the global hashtable */ - lmap.put(url, newLoader); + lmap.put(urlNoFragString, newLoader); } } catch (java.security.PrivilegedActionException pae) { continue; @@ -879,7 +883,7 @@ public class URLClassPath { /* Note that the addition of the url to the list of visited * jars incorporates a check for presence in the hashmap */ - boolean visitedURL = !visited.add(url); + boolean visitedURL = !visited.add(URLUtil.urlNoFragString(url)); if (!visitedURL) { try { newLoader.ensureOpen(); diff --git a/src/share/classes/sun/net/util/URLUtil.java b/src/share/classes/sun/net/util/URLUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..047df3ebe9ae8e4307ae6ef21934e96762793795 --- /dev/null +++ b/src/share/classes/sun/net/util/URLUtil.java @@ -0,0 +1,80 @@ +/* + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package sun.net.util; + +import java.net.URL; + +/** + * URL Utility class. + */ +public class URLUtil { + /** + * Returns a string form of the url suitable for use as a key in HashMap/Sets. + * + * The string form should be behave in the same manner as the URL when + * compared for equality in a HashMap/Set, except that no nameservice + * lookup is done on the hostname (only string comparison), and the fragment + * is not considered. + * + * @see java.net.URLStreamHandler.sameFile(java.net.URL) + */ + public static String urlNoFragString(URL url) { + StringBuilder strForm = new StringBuilder(); + + String protocol = url.getProtocol(); + if (protocol != null) { + /* protocol is compared case-insensitive, so convert to lowercase */ + protocol = protocol.toLowerCase(); + strForm.append(protocol); + strForm.append("://"); + } + + String host = url.getHost(); + if (host != null) { + /* host is compared case-insensitive, so convert to lowercase */ + host = host.toLowerCase(); + strForm.append(host); + + int port = url.getPort(); + if (port == -1) { + /* if no port is specificed then use the protocols + * default, if there is one */ + port = url.getDefaultPort(); + } + if (port != -1) { + strForm.append(":").append(port); + } + } + + String file = url.getFile(); + if (file != null) { + strForm.append(file); + } + + return strForm.toString(); + } +} + diff --git a/src/solaris/classes/sun/net/www/protocol/jar/JarFileFactory.java b/src/solaris/classes/sun/net/www/protocol/jar/JarFileFactory.java index c51f15eaec90e9e65fd625f0dd11025fff037b2d..0d1b3099836fe82c4a6b1fe25b1d384eaf58d5a8 100644 --- a/src/solaris/classes/sun/net/www/protocol/jar/JarFileFactory.java +++ b/src/solaris/classes/sun/net/www/protocol/jar/JarFileFactory.java @@ -25,12 +25,14 @@ package sun.net.www.protocol.jar; -import java.io.*; -import java.net.*; -import java.util.*; -import java.util.jar.*; -import java.util.zip.ZipFile; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.net.URL; +import java.net.URLConnection; +import java.util.HashMap; +import java.util.jar.JarFile; import java.security.Permission; +import sun.net.util.URLUtil; /* A factory for cached JAR file. This class is used to both retrieve * and cache Jar files. @@ -41,13 +43,13 @@ import java.security.Permission; class JarFileFactory implements URLJarFile.URLJarFileCloseController { /* the url to file cache */ - private static HashMap fileCache = new HashMap(); + private static HashMap fileCache = new HashMap(); /* the file to url cache */ - private static HashMap urlCache = new HashMap(); + private static HashMap urlCache = new HashMap(); URLConnection getConnection(JarFile jarFile) throws IOException { - URL u = (URL)urlCache.get(jarFile); + URL u = urlCache.get(jarFile); if (u != null) return u.openConnection(); @@ -72,7 +74,7 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController { synchronized (this) { result = getCachedJarFile(url); if (result == null) { - fileCache.put(url, local_result); + fileCache.put(URLUtil.urlNoFragString(url), local_result); urlCache.put(local_result, url); result = local_result; } else { @@ -97,15 +99,15 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController { * remove the JarFile from the cache */ public void close(JarFile jarFile) { - URL urlRemoved = (URL) urlCache.remove(jarFile); + URL urlRemoved = urlCache.remove(jarFile); if( urlRemoved != null) { - fileCache.remove(urlRemoved); + fileCache.remove(URLUtil.urlNoFragString(urlRemoved)); } } private JarFile getCachedJarFile(URL url) { - JarFile result = (JarFile)fileCache.get(url); + JarFile result = fileCache.get(URLUtil.urlNoFragString(url)); /* if the JAR file is cached, the permission will always be there */ if (result != null) { diff --git a/src/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java b/src/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java index 3192c52d030efd84800c6442be6507e3e7741f6d..9df86326e40c4960e5befeb029edb8c8e57b9f09 100644 --- a/src/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java +++ b/src/windows/classes/sun/net/www/protocol/jar/JarFileFactory.java @@ -25,12 +25,14 @@ package sun.net.www.protocol.jar; -import java.io.*; -import java.net.*; -import java.util.*; -import java.util.jar.*; -import java.util.zip.ZipFile; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.net.URL; +import java.net.URLConnection; +import java.util.HashMap; +import java.util.jar.JarFile; import java.security.Permission; +import sun.net.util.URLUtil; /* A factory for cached JAR file. This class is used to both retrieve * and cache Jar files. @@ -41,13 +43,13 @@ import java.security.Permission; class JarFileFactory implements URLJarFile.URLJarFileCloseController { /* the url to file cache */ - private static HashMap fileCache = new HashMap(); + private static HashMap fileCache = new HashMap(); /* the file to url cache */ - private static HashMap urlCache = new HashMap(); + private static HashMap urlCache = new HashMap(); URLConnection getConnection(JarFile jarFile) throws IOException { - URL u = (URL)urlCache.get(jarFile); + URL u = urlCache.get(jarFile); if (u != null) return u.openConnection(); @@ -82,7 +84,7 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController { synchronized (this) { result = getCachedJarFile(url); if (result == null) { - fileCache.put(url, local_result); + fileCache.put(URLUtil.urlNoFragString(url), local_result); urlCache.put(local_result, url); result = local_result; } else { @@ -107,14 +109,14 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController { * remove the JarFile from the cache */ public void close(JarFile jarFile) { - URL urlRemoved = (URL) urlCache.remove(jarFile); + URL urlRemoved = urlCache.remove(jarFile); if( urlRemoved != null) { - fileCache.remove(urlRemoved); + fileCache.remove(URLUtil.urlNoFragString(urlRemoved)); } } private JarFile getCachedJarFile(URL url) { - JarFile result = (JarFile)fileCache.get(url); + JarFile result = fileCache.get(URLUtil.urlNoFragString(url)); /* if the JAR file is cached, the permission will always be there */ if (result != null) {