0N/A/*
3909N/A * Copyright (c) 1997, 2011, 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.misc;
0N/A
848N/Aimport java.util.*;
0N/Aimport java.util.jar.JarFile;
0N/Aimport sun.misc.JarIndex;
0N/Aimport sun.misc.InvalidJarIndexException;
0N/Aimport sun.net.www.ParseUtil;
0N/Aimport java.util.zip.ZipEntry;
0N/Aimport java.util.jar.JarEntry;
0N/Aimport java.util.jar.Manifest;
0N/Aimport java.util.jar.Attributes;
0N/Aimport java.util.jar.Attributes.Name;
0N/Aimport java.net.JarURLConnection;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.net.URL;
0N/Aimport java.net.URLConnection;
0N/Aimport java.net.HttpURLConnection;
0N/Aimport java.net.URLStreamHandler;
0N/Aimport java.net.URLStreamHandlerFactory;
848N/Aimport java.io.*;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.AccessControlException;
0N/Aimport java.security.CodeSigner;
0N/Aimport java.security.Permission;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.PrivilegedExceptionAction;
0N/Aimport java.security.cert.Certificate;
0N/Aimport sun.misc.FileURLMapper;
1489N/Aimport sun.net.util.URLUtil;
0N/A
0N/A/**
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 *
0N/A * @author David Connelly
0N/A */
0N/Apublic class URLClassPath {
0N/A final static String USER_AGENT_JAVA_VERSION = "UA-Java-Version";
0N/A final static String JAVA_VERSION;
0N/A private static final boolean DEBUG;
6329N/A private static final boolean DISABLE_JAR_CHECKING;
0N/A
0N/A static {
0N/A JAVA_VERSION = java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction("java.version"));
0N/A DEBUG = (java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction("sun.misc.URLClassPath.debug")) != null);
6329N/A String p = java.security.AccessController.doPrivileged(
6329N/A new sun.security.action.GetPropertyAction("sun.misc.URLClassPath.disableJarChecking"));
6329N/A DISABLE_JAR_CHECKING = p != null ? p.equals("true") || p.equals("") : false;
0N/A }
0N/A
0N/A /* The original search path of URLs. */
28N/A private ArrayList<URL> path = new ArrayList<URL>();
0N/A
0N/A /* The stack of unopened URLs */
28N/A Stack<URL> urls = new Stack<URL>();
0N/A
0N/A /* The resulting search path of Loaders */
28N/A ArrayList<Loader> loaders = new ArrayList<Loader>();
0N/A
0N/A /* Map of each URL opened to its corresponding Loader */
1489N/A HashMap<String, Loader> lmap = new HashMap<String, Loader>();
0N/A
0N/A /* The jar protocol handler to use when creating new URLs */
0N/A private URLStreamHandler jarHandler;
0N/A
848N/A /* Whether this URLClassLoader has been closed yet */
848N/A private boolean closed = false;
848N/A
0N/A /**
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 *
0N/A * @param urls the directory and JAR file URLs to search for classes
0N/A * and resources
0N/A * @param factory the URLStreamHandlerFactory to use when creating new URLs
0N/A */
0N/A public URLClassPath(URL[] urls, URLStreamHandlerFactory factory) {
0N/A for (int i = 0; i < urls.length; i++) {
0N/A path.add(urls[i]);
0N/A }
0N/A push(urls);
0N/A if (factory != null) {
0N/A jarHandler = factory.createURLStreamHandler("jar");
0N/A }
0N/A }
0N/A
0N/A public URLClassPath(URL[] urls) {
0N/A this(urls, null);
0N/A }
0N/A
848N/A public synchronized List<IOException> closeLoaders() {
848N/A if (closed) {
848N/A return Collections.emptyList();
848N/A }
848N/A List<IOException> result = new LinkedList<IOException>();
848N/A for (Loader loader : loaders) {
848N/A try {
848N/A loader.close();
848N/A } catch (IOException e) {
848N/A result.add (e);
848N/A }
848N/A }
848N/A closed = true;
848N/A return result;
848N/A }
848N/A
0N/A /**
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 * <p>
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 */
1327N/A public synchronized void addURL(URL url) {
1327N/A if (closed)
1327N/A return;
0N/A synchronized (urls) {
0N/A if (url == null || path.contains(url))
0N/A return;
0N/A
0N/A urls.add(0, url);
0N/A path.add(url);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the original search path of URLs.
0N/A */
0N/A public URL[] getURLs() {
0N/A synchronized (urls) {
28N/A return path.toArray(new URL[path.size()]);
0N/A }
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A public URL findResource(String name, boolean check) {
0N/A Loader loader;
0N/A for (int i = 0; (loader = getLoader(i)) != null; i++) {
0N/A URL url = loader.findResource(name, check);
0N/A if (url != null) {
0N/A return url;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A public Resource getResource(String name, boolean check) {
0N/A if (DEBUG) {
0N/A System.err.println("URLClassPath.getResource(\"" + name + "\")");
0N/A }
0N/A
0N/A Loader loader;
0N/A for (int i = 0; (loader = getLoader(i)) != null; i++) {
0N/A Resource res = loader.getResource(name, check);
0N/A if (res != null) {
0N/A return res;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
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 *
0N/A * @param name the resource name
0N/A * @return an Enumeration of all the urls having the specified name
0N/A */
28N/A public Enumeration<URL> findResources(final String name,
0N/A final boolean check) {
28N/A return new Enumeration<URL>() {
0N/A private int index = 0;
0N/A private URL url = null;
0N/A
0N/A private boolean next() {
0N/A if (url != null) {
0N/A return true;
0N/A } else {
0N/A Loader loader;
0N/A while ((loader = getLoader(index++)) != null) {
0N/A url = loader.findResource(name, check);
0N/A if (url != null) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A public boolean hasMoreElements() {
0N/A return next();
0N/A }
0N/A
28N/A public URL nextElement() {
0N/A if (!next()) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A URL u = url;
0N/A url = null;
0N/A return u;
0N/A }
0N/A };
0N/A }
0N/A
0N/A public Resource getResource(String name) {
0N/A return getResource(name, true);
0N/A }
0N/A
0N/A /**
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 *
0N/A * @param name the resource name
0N/A * @return an Enumeration of all the resources having the specified name
0N/A */
28N/A public Enumeration<Resource> getResources(final String name,
0N/A final boolean check) {
28N/A return new Enumeration<Resource>() {
0N/A private int index = 0;
0N/A private Resource res = null;
0N/A
0N/A private boolean next() {
0N/A if (res != null) {
0N/A return true;
0N/A } else {
0N/A Loader loader;
0N/A while ((loader = getLoader(index++)) != null) {
0N/A res = loader.getResource(name, check);
0N/A if (res != null) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A public boolean hasMoreElements() {
0N/A return next();
0N/A }
0N/A
28N/A public Resource nextElement() {
0N/A if (!next()) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A Resource r = res;
0N/A res = null;
0N/A return r;
0N/A }
0N/A };
0N/A }
0N/A
28N/A public Enumeration<Resource> getResources(final String name) {
0N/A return getResources(name, true);
0N/A }
0N/A
0N/A /*
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 */
0N/A private synchronized Loader getLoader(int index) {
848N/A if (closed) {
848N/A return null;
848N/A }
0N/A // Expand URL search path until the request can be satisfied
0N/A // or the URL stack is empty.
0N/A while (loaders.size() < index + 1) {
0N/A // Pop the next URL from the URL stack
0N/A URL url;
0N/A synchronized (urls) {
0N/A if (urls.empty()) {
0N/A return null;
0N/A } else {
28N/A url = urls.pop();
0N/A }
0N/A }
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.)
1489N/A String urlNoFragString = URLUtil.urlNoFragString(url);
1489N/A if (lmap.containsKey(urlNoFragString)) {
0N/A continue;
0N/A }
0N/A // Otherwise, create a new Loader for the URL.
0N/A Loader loader;
0N/A try {
0N/A loader = getLoader(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 URL[] urls = loader.getClassPath();
0N/A if (urls != null) {
0N/A push(urls);
0N/A }
0N/A } catch (IOException e) {
0N/A // Silently ignore for now...
0N/A continue;
0N/A }
0N/A // Finally, add the Loader to the search path.
0N/A loaders.add(loader);
1489N/A lmap.put(urlNoFragString, loader);
0N/A }
28N/A return loaders.get(index);
0N/A }
0N/A
0N/A /*
0N/A * Returns the Loader for the specified base URL.
0N/A */
0N/A private Loader getLoader(final URL url) throws IOException {
0N/A try {
28N/A return java.security.AccessController.doPrivileged(
28N/A new java.security.PrivilegedExceptionAction<Loader>() {
28N/A public Loader run() throws IOException {
0N/A String file = url.getFile();
0N/A if (file != null && file.endsWith("/")) {
0N/A if ("file".equals(url.getProtocol())) {
0N/A return new FileLoader(url);
0N/A } else {
0N/A return new Loader(url);
0N/A }
0N/A } else {
0N/A return new JarLoader(url, jarHandler, lmap);
0N/A }
0N/A }
0N/A });
0N/A } catch (java.security.PrivilegedActionException pae) {
0N/A throw (IOException)pae.getException();
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Pushes the specified URLs onto the list of unopened URLs.
0N/A */
0N/A private void push(URL[] us) {
0N/A synchronized (urls) {
0N/A for (int i = us.length - 1; i >= 0; --i) {
0N/A urls.push(us[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Convert class path specification into an array of file URLs.
0N/A *
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 */
0N/A public static URL[] pathToURLs(String path) {
0N/A StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
0N/A URL[] urls = new URL[st.countTokens()];
0N/A int count = 0;
0N/A while (st.hasMoreTokens()) {
0N/A File f = new File(st.nextToken());
0N/A try {
0N/A f = new File(f.getCanonicalPath());
0N/A } catch (IOException x) {
0N/A // use the non-canonicalized filename
0N/A }
0N/A try {
0N/A urls[count++] = ParseUtil.fileToEncodedURL(f);
0N/A } catch (IOException x) { }
0N/A }
0N/A
0N/A if (urls.length != count) {
0N/A URL[] tmp = new URL[count];
0N/A System.arraycopy(urls, 0, tmp, 0, count);
0N/A urls = tmp;
0N/A }
0N/A return urls;
0N/A }
0N/A
0N/A /*
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 */
0N/A public URL checkURL(URL url) {
0N/A try {
0N/A check(url);
0N/A } catch (Exception e) {
0N/A return null;
0N/A }
0N/A
0N/A return url;
0N/A }
0N/A
0N/A /*
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 */
0N/A static void check(URL url) throws IOException {
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A URLConnection urlConnection = url.openConnection();
0N/A Permission perm = urlConnection.getPermission();
0N/A if (perm != null) {
0N/A try {
0N/A security.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 security.checkRead(perm.getName());
0N/A } else if ((perm instanceof
0N/A java.net.SocketPermission) &&
0N/A perm.getActions().indexOf("connect") != -1) {
0N/A URL locUrl = url;
0N/A if (urlConnection instanceof JarURLConnection) {
0N/A locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
0N/A }
0N/A security.checkConnect(locUrl.getHost(),
0N/A locUrl.getPort());
0N/A } else {
0N/A throw se;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Inner class used to represent a loader of resources and classes
0N/A * from a base URL.
0N/A */
848N/A private static class Loader implements Closeable {
0N/A private final URL base;
3391N/A private JarFile jarfile; // if this points to a jar file
0N/A
0N/A /*
0N/A * Creates a new Loader for the specified URL.
0N/A */
0N/A Loader(URL url) {
0N/A base = url;
0N/A }
0N/A
0N/A /*
0N/A * Returns the base URL for this Loader.
0N/A */
0N/A URL getBaseURL() {
0N/A return base;
0N/A }
0N/A
0N/A URL findResource(final String name, boolean check) {
0N/A URL url;
0N/A try {
0N/A url = new URL(base, ParseUtil.encodePath(name, false));
0N/A } catch (MalformedURLException e) {
0N/A throw new IllegalArgumentException("name");
0N/A }
0N/A
0N/A try {
0N/A if (check) {
0N/A URLClassPath.check(url);
0N/A }
0N/A
0N/A /*
0N/A * For a HTTP connection we use the HEAD method to
0N/A * check if the resource exists.
0N/A */
0N/A URLConnection uc = url.openConnection();
0N/A if (uc instanceof HttpURLConnection) {
0N/A HttpURLConnection hconn = (HttpURLConnection)uc;
0N/A hconn.setRequestMethod("HEAD");
0N/A if (hconn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
0N/A return null;
0N/A }
0N/A } else {
0N/A // our best guess for the other cases
0N/A InputStream is = url.openStream();
0N/A is.close();
0N/A }
0N/A return url;
0N/A } catch (Exception e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A Resource getResource(final String name, boolean check) {
0N/A final URL url;
0N/A try {
0N/A url = new URL(base, ParseUtil.encodePath(name, false));
0N/A } catch (MalformedURLException e) {
0N/A throw new IllegalArgumentException("name");
0N/A }
0N/A final URLConnection uc;
0N/A try {
0N/A if (check) {
0N/A URLClassPath.check(url);
0N/A }
0N/A uc = url.openConnection();
0N/A InputStream in = uc.getInputStream();
3391N/A if (uc instanceof JarURLConnection) {
4294N/A /* Need to remember the jar file so it can be closed
3391N/A * in a hurry.
3391N/A */
3391N/A JarURLConnection juc = (JarURLConnection)uc;
6329N/A jarfile = JarLoader.checkJar(juc.getJarFile());
3391N/A }
0N/A } catch (Exception e) {
0N/A return null;
0N/A }
0N/A return new Resource() {
0N/A public String getName() { return name; }
0N/A public URL getURL() { return url; }
0N/A public URL getCodeSourceURL() { return base; }
0N/A public InputStream getInputStream() throws IOException {
0N/A return uc.getInputStream();
0N/A }
0N/A public int getContentLength() throws IOException {
0N/A return uc.getContentLength();
0N/A }
0N/A };
0N/A }
0N/A
0N/A /*
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 * resource.
0N/A */
0N/A Resource getResource(final String name) {
0N/A return getResource(name, true);
0N/A }
0N/A
0N/A /*
848N/A * close this loader and release all resources
848N/A * method overridden in sub-classes
848N/A */
3391N/A public void close () throws IOException {
3391N/A if (jarfile != null) {
3391N/A jarfile.close();
3391N/A }
3391N/A }
848N/A
848N/A /*
0N/A * Returns the local class path for this loader, or null if none.
0N/A */
0N/A URL[] getClassPath() throws IOException {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Inner class used to represent a Loader of resources from a JAR URL.
0N/A */
0N/A static class JarLoader extends Loader {
0N/A private JarFile jar;
0N/A private URL csu;
0N/A private JarIndex index;
0N/A private MetaIndex metaIndex;
0N/A private URLStreamHandler handler;
1489N/A private HashMap<String, Loader> lmap;
848N/A private boolean closed = false;
6329N/A private static final sun.misc.JavaUtilZipFileAccess zipAccess =
6329N/A sun.misc.SharedSecrets.getJavaUtilZipFileAccess();
0N/A
0N/A /*
0N/A * Creates a new JarLoader for the specified URL referring to
0N/A * a JAR file.
0N/A */
28N/A JarLoader(URL url, URLStreamHandler jarHandler,
1489N/A HashMap<String, Loader> loaderMap)
0N/A throws IOException
0N/A {
0N/A super(new URL("jar", "", -1, url + "!/", jarHandler));
0N/A csu = url;
0N/A handler = jarHandler;
0N/A lmap = loaderMap;
0N/A
0N/A if (!isOptimizable(url)) {
0N/A ensureOpen();
0N/A } else {
0N/A String fileName = url.getFile();
0N/A if (fileName != null) {
0N/A fileName = ParseUtil.decode(fileName);
0N/A File f = new File(fileName);
0N/A metaIndex = MetaIndex.forJar(f);
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 if (metaIndex != null && !f.exists()) {
0N/A metaIndex = null;
0N/A }
0N/A }
0N/A
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 if (metaIndex == null) {
0N/A ensureOpen();
0N/A }
0N/A }
0N/A }
0N/A
848N/A @Override
848N/A public void close () throws IOException {
848N/A // closing is synchronized at higher level
848N/A if (!closed) {
848N/A closed = true;
848N/A // in case not already open.
848N/A ensureOpen();
848N/A jar.close();
848N/A }
848N/A }
848N/A
0N/A JarFile getJarFile () {
0N/A return jar;
0N/A }
0N/A
0N/A private boolean isOptimizable(URL url) {
0N/A return "file".equals(url.getProtocol());
0N/A }
0N/A
0N/A private void ensureOpen() throws IOException {
0N/A if (jar == null) {
0N/A try {
0N/A java.security.AccessController.doPrivileged(
28N/A new java.security.PrivilegedExceptionAction<Void>() {
28N/A public Void run() throws IOException {
0N/A if (DEBUG) {
0N/A System.err.println("Opening " + csu);
0N/A Thread.dumpStack();
0N/A }
0N/A
0N/A jar = getJarFile(csu);
0N/A index = JarIndex.getJarIndex(jar, metaIndex);
0N/A if (index != null) {
0N/A String[] jarfiles = index.getJarFiles();
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 for(int i = 0; i < jarfiles.length; i++) {
0N/A try {
0N/A URL jarURL = new URL(csu, jarfiles[i]);
0N/A // If a non-null loader already exists, leave it alone.
1489N/A String urlNoFragString = URLUtil.urlNoFragString(jarURL);
1489N/A if (!lmap.containsKey(urlNoFragString)) {
1489N/A lmap.put(urlNoFragString, null);
0N/A }
0N/A } catch (MalformedURLException e) {
0N/A continue;
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A );
0N/A } catch (java.security.PrivilegedActionException pae) {
0N/A throw (IOException)pae.getException();
0N/A }
0N/A }
0N/A }
0N/A
6329N/A /* Throws if the given jar file is does not start with the correct LOC */
6329N/A static JarFile checkJar(JarFile jar) throws IOException {
6329N/A if (System.getSecurityManager() != null && !DISABLE_JAR_CHECKING
6430N/A && !zipAccess.startsWithLocHeader(jar)) {
6430N/A IOException x = new IOException("Invalid Jar file");
6430N/A try {
6430N/A jar.close();
6430N/A } catch (IOException ex) {
6430N/A x.addSuppressed(ex);
6430N/A }
6430N/A throw x;
6430N/A }
6430N/A
6329N/A return jar;
6329N/A }
6329N/A
0N/A private JarFile getJarFile(URL url) throws IOException {
0N/A // Optimize case where url refers to a local jar file
0N/A if (isOptimizable(url)) {
0N/A FileURLMapper p = new FileURLMapper (url);
0N/A if (!p.exists()) {
0N/A throw new FileNotFoundException(p.getPath());
0N/A }
6329N/A return checkJar(new JarFile(p.getPath()));
0N/A }
0N/A URLConnection uc = getBaseURL().openConnection();
0N/A uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION);
6329N/A JarFile jarFile = ((JarURLConnection)uc).getJarFile();
6329N/A return checkJar(jarFile);
0N/A }
0N/A
0N/A /*
0N/A * Returns the index of this JarLoader if it exists.
0N/A */
0N/A JarIndex getIndex() {
0N/A try {
0N/A ensureOpen();
0N/A } catch (IOException e) {
0N/A throw (InternalError) new InternalError().initCause(e);
0N/A }
0N/A return index;
0N/A }
0N/A
0N/A /*
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 */
0N/A Resource checkResource(final String name, boolean check,
0N/A final JarEntry entry) {
0N/A
0N/A final URL url;
0N/A try {
0N/A url = new URL(getBaseURL(), ParseUtil.encodePath(name, false));
0N/A if (check) {
0N/A URLClassPath.check(url);
0N/A }
0N/A } catch (MalformedURLException e) {
0N/A return null;
0N/A // throw new IllegalArgumentException("name");
0N/A } catch (IOException e) {
0N/A return null;
0N/A } catch (AccessControlException e) {
0N/A return null;
0N/A }
0N/A
0N/A return new Resource() {
0N/A public String getName() { return name; }
0N/A public URL getURL() { return url; }
0N/A public URL getCodeSourceURL() { return csu; }
0N/A public InputStream getInputStream() throws IOException
0N/A { return jar.getInputStream(entry); }
0N/A public int getContentLength()
0N/A { return (int)entry.getSize(); }
0N/A public Manifest getManifest() throws IOException
0N/A { return jar.getManifest(); };
0N/A public Certificate[] getCertificates()
0N/A { return entry.getCertificates(); };
0N/A public CodeSigner[] getCodeSigners()
0N/A { return entry.getCodeSigners(); };
0N/A };
0N/A }
0N/A
0N/A
0N/A /*
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 */
0N/A boolean validIndex(final String name) {
0N/A String packageName = name;
0N/A int pos;
0N/A if((pos = name.lastIndexOf("/")) != -1) {
0N/A packageName = name.substring(0, pos);
0N/A }
0N/A
0N/A String entryName;
0N/A ZipEntry entry;
28N/A Enumeration<JarEntry> enum_ = jar.entries();
0N/A while (enum_.hasMoreElements()) {
28N/A entry = enum_.nextElement();
0N/A entryName = entry.getName();
0N/A if((pos = entryName.lastIndexOf("/")) != -1)
0N/A entryName = entryName.substring(0, pos);
0N/A if (entryName.equals(packageName)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Returns the URL for a resource with the specified name
0N/A */
0N/A URL findResource(final String name, boolean check) {
0N/A Resource rsc = getResource(name, check);
0N/A if (rsc != null) {
0N/A return rsc.getURL();
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Returns the JAR Resource for the specified name.
0N/A */
0N/A Resource getResource(final String name, boolean check) {
0N/A if (metaIndex != null) {
0N/A if (!metaIndex.mayContain(name)) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A try {
0N/A ensureOpen();
0N/A } catch (IOException e) {
0N/A throw (InternalError) new InternalError().initCause(e);
0N/A }
0N/A final JarEntry entry = jar.getJarEntry(name);
0N/A if (entry != null)
0N/A return checkResource(name, check, entry);
0N/A
0N/A if (index == null)
0N/A return null;
0N/A
1489N/A HashSet<String> visited = new HashSet<String>();
0N/A return getResource(name, check, visited);
0N/A }
0N/A
0N/A /*
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 */
0N/A Resource getResource(final String name, boolean check,
1489N/A Set<String> visited) {
0N/A
0N/A Resource res;
0N/A Object[] jarFiles;
0N/A boolean done = false;
0N/A int count = 0;
0N/A LinkedList jarFilesList = null;
0N/A
0N/A /* If there no jar files in the index that can potential contain
0N/A * this resource then return immediately.
0N/A */
0N/A if((jarFilesList = index.get(name)) == null)
0N/A return null;
0N/A
0N/A do {
0N/A jarFiles = jarFilesList.toArray();
0N/A int size = jarFilesList.size();
0N/A /* loop through the mapped jar file list */
0N/A while(count < size) {
0N/A String jarName = (String)jarFiles[count++];
0N/A JarLoader newLoader;
0N/A final URL url;
0N/A
0N/A try{
0N/A url = new URL(csu, jarName);
1489N/A String urlNoFragString = URLUtil.urlNoFragString(url);
1489N/A if ((newLoader = (JarLoader)lmap.get(urlNoFragString)) == null) {
0N/A /* no loader has been set up for this jar file
0N/A * before
0N/A */
28N/A newLoader = AccessController.doPrivileged(
28N/A new PrivilegedExceptionAction<JarLoader>() {
28N/A public JarLoader run() throws IOException {
0N/A return new JarLoader(url, handler,
0N/A lmap);
0N/A }
0N/A });
0N/A
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 */
0N/A JarIndex newIndex = newLoader.getIndex();
0N/A if(newIndex != null) {
0N/A int pos = jarName.lastIndexOf("/");
0N/A newIndex.merge(this.index, (pos == -1 ?
0N/A null : jarName.substring(0, pos + 1)));
0N/A }
0N/A
0N/A /* put it in the global hashtable */
1489N/A lmap.put(urlNoFragString, newLoader);
0N/A }
0N/A } catch (java.security.PrivilegedActionException pae) {
0N/A continue;
0N/A } catch (MalformedURLException e) {
0N/A continue;
0N/A }
0N/A
0N/A
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 */
1489N/A boolean visitedURL = !visited.add(URLUtil.urlNoFragString(url));
0N/A if (!visitedURL) {
0N/A try {
0N/A newLoader.ensureOpen();
0N/A } catch (IOException e) {
0N/A throw (InternalError) new InternalError().initCause(e);
0N/A }
0N/A final JarEntry entry = newLoader.jar.getJarEntry(name);
0N/A if (entry != null) {
0N/A return newLoader.checkResource(name, check, entry);
0N/A }
0N/A
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 */
0N/A if (!newLoader.validIndex(name)) {
0N/A /* the mapping is wrong */
0N/A throw new InvalidJarIndexException("Invalid index");
0N/A }
0N/A }
0N/A
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 */
0N/A if (visitedURL || newLoader == this ||
0N/A newLoader.getIndex() == null) {
0N/A continue;
0N/A }
0N/A
0N/A /* Process the index of the new loader
0N/A */
0N/A if((res = newLoader.getResource(name, check, visited))
0N/A != null) {
0N/A return res;
0N/A }
0N/A }
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 jarFilesList = index.get(name);
0N/A
0N/A // If the count is unchanged, we are done.
0N/A } while(count < jarFilesList.size());
0N/A return null;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Returns the JAR file local class path, or null if none.
0N/A */
0N/A URL[] getClassPath() throws IOException {
0N/A if (index != null) {
0N/A return null;
0N/A }
0N/A
0N/A if (metaIndex != null) {
0N/A return null;
0N/A }
0N/A
0N/A ensureOpen();
0N/A parseExtensionsDependencies();
0N/A if (SharedSecrets.javaUtilJarAccess().jarFileHasClassPathAttribute(jar)) { // Only get manifest when necessary
0N/A Manifest man = jar.getManifest();
0N/A if (man != null) {
0N/A Attributes attr = man.getMainAttributes();
0N/A if (attr != null) {
0N/A String value = attr.getValue(Name.CLASS_PATH);
0N/A if (value != null) {
0N/A return parseClassPath(csu, value);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * parse the standard extension dependencies
0N/A */
0N/A private void parseExtensionsDependencies() throws IOException {
0N/A ExtensionDependency.checkExtensionsDependencies(jar);
0N/A }
0N/A
0N/A /*
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 */
0N/A private URL[] parseClassPath(URL base, String value)
0N/A throws MalformedURLException
0N/A {
0N/A StringTokenizer st = new StringTokenizer(value);
0N/A URL[] urls = new URL[st.countTokens()];
0N/A int i = 0;
0N/A while (st.hasMoreTokens()) {
0N/A String path = st.nextToken();
0N/A urls[i] = new URL(base, path);
0N/A i++;
0N/A }
0N/A return urls;
0N/A }
0N/A }
0N/A
0N/A /*
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 */
0N/A private static class FileLoader extends Loader {
272N/A /* Canonicalized File */
0N/A private File dir;
0N/A
0N/A FileLoader(URL url) throws IOException {
0N/A super(url);
0N/A if (!"file".equals(url.getProtocol())) {
0N/A throw new IllegalArgumentException("url");
0N/A }
0N/A String path = url.getFile().replace('/', File.separatorChar);
0N/A path = ParseUtil.decode(path);
272N/A dir = (new File(path)).getCanonicalFile();
0N/A }
0N/A
0N/A /*
0N/A * Returns the URL for a resource with the specified name
0N/A */
0N/A URL findResource(final String name, boolean check) {
0N/A Resource rsc = getResource(name, check);
0N/A if (rsc != null) {
0N/A return rsc.getURL();
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A Resource getResource(final String name, boolean check) {
0N/A final URL url;
0N/A try {
0N/A URL normalizedBase = new URL(getBaseURL(), ".");
0N/A url = new URL(getBaseURL(), ParseUtil.encodePath(name, false));
0N/A
0N/A if (url.getFile().startsWith(normalizedBase.getFile()) == false) {
0N/A // requested resource had ../..'s in path
0N/A return null;
0N/A }
0N/A
0N/A if (check)
0N/A URLClassPath.check(url);
272N/A
272N/A final File file;
272N/A if (name.indexOf("..") != -1) {
272N/A file = (new File(dir, name.replace('/', File.separatorChar)))
272N/A .getCanonicalFile();
272N/A if ( !((file.getPath()).startsWith(dir.getPath())) ) {
272N/A /* outside of base dir */
272N/A return null;
272N/A }
272N/A } else {
272N/A file = new File(dir, name.replace('/', File.separatorChar));
272N/A }
272N/A
0N/A if (file.exists()) {
0N/A return new Resource() {
0N/A public String getName() { return name; };
0N/A public URL getURL() { return url; };
0N/A public URL getCodeSourceURL() { return getBaseURL(); };
0N/A public InputStream getInputStream() throws IOException
0N/A { return new FileInputStream(file); };
0N/A public int getContentLength() throws IOException
0N/A { return (int)file.length(); };
0N/A };
0N/A }
0N/A } catch (Exception e) {
0N/A return null;
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A}