URLClassPath.java revision 0
0N/A * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 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 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 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 * 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * This class is used to maintain a search path of URLs for loading classes 0N/A * and resources from both JAR files and directories. 0N/A * @author David Connelly 0N/A /* The original search path of URLs. */ 0N/A /* The stack of unopened URLs */ 0N/A /* The resulting search path of Loaders */ 0N/A /* Map of each URL opened to its corresponding Loader */ 0N/A /* The jar protocol handler to use when creating new URLs */ 0N/A * Creates a new URLClassPath for the given URLs. The URLs will be 0N/A * searched in the order specified for classes and resources. A URL 0N/A * ending with a '/' is assumed to refer to a directory. Otherwise, 0N/A * the URL is assumed to refer to a JAR file. 0N/A * @param urls the directory and JAR file URLs to search for classes 0N/A * @param factory the URLStreamHandlerFactory to use when creating new URLs 0N/A * Appends the specified URL to the search path of directory and JAR 0N/A * file URLs from which to load classes and resources. 0N/A * If the URL specified is null or is already in the list of 0N/A * URLs, then invoking this method has no effect. 0N/A * Returns the original search path of URLs. 0N/A * Finds the resource with the specified name on the URL search path 0N/A * or null if not found or security check fails. 0N/A * @param name the name of the resource 0N/A * @param check whether to perform a security check 0N/A * @return a <code>URL</code> for the resource, or <code>null</code> 0N/A * if the resource could not be found. 0N/A * Finds the first Resource on the URL search path which has the specified 0N/A * name. Returns null if no Resource could be found. 0N/A * @param name the name of the Resource 0N/A * @param check whether to perform a security check 0N/A * @return the Resource, or null if not found 0N/A * Finds all resources on the URL search path with the given name. 0N/A * Returns an enumeration of the URL objects. 0N/A * @param name the resource name 0N/A * @return an Enumeration of all the urls having the specified name 0N/A * Finds all resources on the URL search path with the given name. 0N/A * Returns an enumeration of the Resource objects. 0N/A * @param name the resource name 0N/A * @return an Enumeration of all the resources having the specified name 0N/A * Returns the Loader at the specified position in the URL search 0N/A * path. The URLs are opened and expanded as needed. Returns null 0N/A * if the specified index is out of range. 0N/A // Expand URL search path until the request can be satisfied 0N/A // or the URL stack is empty. 0N/A // Pop the next URL from the URL stack 0N/A // Skip this URL if it already has a Loader. (Loader 0N/A // may be null in the case where URL has not been opened 0N/A // but is referenced by a JAR index.) 0N/A // Otherwise, create a new Loader for the URL. 0N/A // If the loader defines a local class path then add the 0N/A // URLs to the list of URLs to be opened. 0N/A // Silently ignore for now... 0N/A // Finally, add the Loader to the search path. 0N/A * Returns the Loader for the specified base URL. 0N/A * Pushes the specified URLs onto the list of unopened URLs. 0N/A * Convert class path specification into an array of file URLs. 0N/A * The path of the file is encoded before conversion into URL 0N/A * form so that reserved characters can safely appear in the path. 0N/A // use the non-canonicalized filename 0N/A * Check whether the resource URL should be returned. 0N/A * Return null on security check failure. 0N/A * Called by java.net.URLClassLoader. 0N/A * Check whether the resource URL should be returned. 0N/A * Throw exception on failure. 0N/A * Called internally within this file. 0N/A // security managers 0N/A * Inner class used to represent a loader of resources and classes 0N/A * Creates a new Loader for the specified URL. 0N/A * Returns the base URL for this Loader. 0N/A * For a HTTP connection we use the HEAD method to 0N/A * check if the resource exists. 0N/A // our best guess for the other cases 0N/A * Returns the Resource for the specified name, or null if not 0N/A * found or the caller does not have the permission to get the 0N/A * Returns the local class path for this loader, or null if none. 0N/A * Inner class used to represent a Loader of resources from a JAR URL. 0N/A * Creates a new JarLoader for the specified URL referring to 0N/A // If the meta index is found but the file is not 0N/A // installed, set metaIndex to null. A typical 0N/A // senario is charsets.jar which won't be installed 0N/A // when the user is running in certain locale environment. 0N/A // The side effect of null metaIndex will cause 0N/A // ensureOpen get called so that IOException is thrown. 0N/A // metaIndex is null when either there is no such jar file 0N/A // entry recorded in meta-index file or such jar file is 0N/A // missing in JRE. See bug 6340399. 0N/A // Add all the dependent URLs to the lmap so that loaders 0N/A // will not be created for them by URLClassPath.getLoader(int) 0N/A // if the same URL occurs later on the main class path. We set 0N/A // Loader to null here to avoid creating a Loader for each 0N/A // URL until we actually need to try to load something from them. 0N/A // If a non-null loader already exists, leave it alone. 0N/A // Optimize case where url refers to a local jar file 0N/A * Returns the index of this JarLoader if it exists. 0N/A * Creates the resource and if the check flag is set to true, checks if 0N/A * is its okay to return the resource. 0N/A // throw new IllegalArgumentException("name"); 0N/A * Returns true iff atleast one resource in the jar file has the same 0N/A * package name as that of the specified resource name. 0N/A * Returns the URL for a resource with the specified name 0N/A * Returns the JAR Resource for the specified name. 0N/A * Version of getResource() that tracks the jar files that have been 0N/A * visited by linking through the index files. This helper method uses 0N/A * a HashSet to store the URLs of jar files that have been searched and 0N/A * uses it to avoid going into an infinite loop, looking for a 0N/A * non-existent resource 0N/A /* If there no jar files in the index that can potential contain 0N/A * this resource then return immediately. 0N/A /* loop through the mapped jar file list */ 0N/A /* no loader has been set up for this jar file 0N/A /* this newly opened jar file has its own index, 0N/A * merge it into the parent's index, taking into 0N/A * account the relative path. 0N/A /* put it in the global hashtable */ 0N/A /* Note that the addition of the url to the list of visited 0N/A * jars incorporates a check for presence in the hashmap 0N/A /* Verify that at least one other resource with the 0N/A * same package name as the lookedup resource is 0N/A * present in the new jar 0N/A /* the mapping is wrong */ 0N/A /* If newLoader is the current loader or if it is a 0N/A * loader that has already been searched or if the new 0N/A * loader does not have an index then skip it 0N/A * and move on to the next loader. 0N/A /* Process the index of the new loader 0N/A // Get the list of jar files again as the list could have grown 0N/A // due to merging of index files. 0N/A // If the count is unchanged, we are done. 0N/A * Returns the JAR file local class path, or null if none. 0N/A * parse the standard extension dependencies 0N/A * Parses value of the Class-Path manifest attribute and returns 0N/A * an array of URLs relative to the specified base URL. 0N/A * Inner class used to represent a loader of classes and resources 0N/A * from a file URL that refers to a directory. 0N/A * Returns the URL for a resource with the specified name 0N/A // requested resource had ../..'s in path