⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlclassloader.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            {              int response =                ((HttpURLConnection) connection).getResponseCode();              if (response / 100 != 2)                return null;            }          if (stream != null)            return new RemoteResource(this, name, url, stream, length);          else            return null;        }      catch (IOException ioe)        {          return null;        }    }  }  /**   * A resource from some remote location.   */  static final class RemoteResource extends Resource  {    private final URL url;    private final InputStream stream;    private final int length;    RemoteResource(RemoteURLLoader loader, String name, URL url,                   InputStream stream, int length)    {      super(loader);      this.url = url;      this.stream = stream;      this.length = length;    }    InputStream getInputStream() throws IOException    {      return stream;    }    public int getLength()    {      return length;    }    public URL getURL()    {      return url;    }  }  /**   * A <code>SoURLLoader</code> is a type of <code>URLLoader</code>   * that loads classes and resources from a shared library.   */  final static class SoURLLoader extends URLLoader  {    SharedLibHelper helper;    SoURLLoader(URLClassLoader classloader, URL url)    {      this(classloader, url, url);    }    SoURLLoader(URLClassLoader classloader, URL url, URL overrideURL)    {      super(classloader, url, overrideURL);      helper = SharedLibHelper.findHelper(classloader, url.getFile(),					  noCertCodeSource, true);    }    Class getClass(String className)    {      return helper.findClass(className);    }    Resource getResource(String name)    {      URL url = helper.findResource(name);      if (url == null)	return null;      return new SoResource(this, url);    }  }  final static class SoResource extends Resource  {    SoResource(SoURLLoader loader, URL url)    {      super(loader);      this.url = url;    }    InputStream getInputStream() throws IOException    {      URLConnection conn = url.openConnection();      return conn.getInputStream();    }    public int getLength()    {      // FIXME we could find this by asking the core object.      return -1;    }    public URL getURL ()    {      return url;    }    final URL url;  }  /**   * A <code>FileURLLoader</code> is a type of <code>URLLoader</code>   * only loading from file url.   */  static final class FileURLLoader extends URLLoader  {    File dir; //the file for this file url    FileURLLoader(URLClassLoader classloader, URL url, URL absoluteUrl)    {      super(classloader, url, absoluteUrl);      dir = new File(absoluteUrl.getFile());    }    /** get resource with the name "name" in the file url */    Resource getResource(String name)    {      try  	{ 	  File file = new File(dir, name).getCanonicalFile(); 	  if (file.exists() && !file.isDirectory()) 	    return new FileResource(this, file); 	}      catch (IOException e) 	{ 	  // Fall through... 	}      return null;    }  }  static final class FileResource extends Resource  {    final File file;    FileResource(FileURLLoader loader, File file)    {      super(loader);      this.file = file;    }    InputStream getInputStream() throws IOException    {      // Delegate to the URL content handler mechanism to retrieve an      // HTML representation of the directory listing if a directory      if (file.isDirectory())        {          URL url = getURL();          return url.openStream();        }      // Otherwise simply return a FileInputStream      return new FileInputStream(file);    }    public int getLength()    {      // Delegate to the URL content handler mechanism to retrieve the      // length of the HTML representation of the directory listing if      // a directory, or -1 if an exception occurs opening the directory.      if (file.isDirectory())        {          URL url = getURL();          try            {              URLConnection connection = url.openConnection();              return connection.getContentLength();            }          catch (IOException e)            {              return -1;            }        }      // Otherwise simply return the file length      return (int) file.length();    }    public URL getURL()    {      try        {          return file.toURL();        }      catch (MalformedURLException e)        {          InternalError ie = new InternalError();          ie.initCause(e);          throw ie;        }    }  }  /**   * A <code>CoreURLLoader</code> is a type of <code>URLLoader</code>   * only loading from core url.   */  static final class CoreURLLoader extends URLLoader  {    private String dir;    CoreURLLoader(URLClassLoader classloader, URL url)    {      super(classloader, url);      dir = baseURL.getFile();    }    /** get resource with the name "name" in the core url */    Resource getResource(String name)    {      Core core = Core.find (dir + name);      if (core != null)        return new CoreResource(this, name, core);      return null;    }  }  static final class CoreResource extends Resource  {    private final Core core;    private final String name;    CoreResource(CoreURLLoader loader, String name, Core core)    {      super(loader);      this.core = core;      this.name = name;    }    InputStream getInputStream() throws IOException    {      return new CoreInputStream(core);    }    public int getLength()    {      return core.length;    }    public URL getURL()    {      try        {          return new URL(loader.baseURL, name,                         loader.classloader.getURLStreamHandler("core"));        }      catch (MalformedURLException e)        {          InternalError ie = new InternalError();          ie.initCause(e);          throw ie;        }    }  }  // Constructors  /**   * Creates a URLClassLoader that gets classes from the supplied URLs.   * To determine if this classloader may be created the constructor of   * the super class (<code>SecureClassLoader</code>) is called first, which   * can throw a SecurityException. Then the supplied URLs are added   * in the order given to the URLClassLoader which uses these URLs to   * load classes and resources (after using the default parent ClassLoader).   *   * @param urls Locations that should be searched by this ClassLoader when   * resolving Classes or Resources.   * @exception SecurityException if the SecurityManager disallows the   * creation of a ClassLoader.   * @see SecureClassLoader   */  public URLClassLoader(URL[] urls) throws SecurityException  {    super();    this.factory = null;    this.securityContext = null;    addURLs(urls);  }  /**   * Creates a <code>URLClassLoader</code> that gets classes from the supplied   * <code>URL</code>s.   * To determine if this classloader may be created the constructor of   * the super class (<code>SecureClassLoader</code>) is called first, which   * can throw a SecurityException. Then the supplied URLs are added   * in the order given to the URLClassLoader which uses these URLs to   * load classes and resources (after using the supplied parent ClassLoader).   * @param urls Locations that should be searched by this ClassLoader when   * resolving Classes or Resources.   * @param parent The parent class loader used before trying this class   * loader.   * @exception SecurityException if the SecurityManager disallows the   * creation of a ClassLoader.   * @exception SecurityException   * @see SecureClassLoader   */  public URLClassLoader(URL[] urls, ClassLoader parent)    throws SecurityException  {    super(parent);    this.factory = null;    this.securityContext = null;    addURLs(urls);  }  // Package-private to avoid a trampoline constructor.  /**   * Package-private constructor used by the static   * <code>newInstance(URL[])</code> method.  Creates an   * <code>URLClassLoader</code> with the given parent but without any   * <code>URL</code>s yet. This is used to bypass the normal security   * check for creating classloaders, but remembers the security   * context which will be used when defining classes.  The   * <code>URL</code>s to load from must be added by the   * <code>newInstance()</code> method in the security context of the   * caller.   *   * @param securityContext the security context of the unprivileged code.   */  URLClassLoader(ClassLoader parent, AccessControlContext securityContext)  {    super(parent);    this.factory = null;    this.securityContext = securityContext;  }  /**   * Creates a URLClassLoader that gets classes from the supplied URLs.   * To determine if this classloader may be created the constructor of   * the super class (<CODE>SecureClassLoader</CODE>) is called first, which   * can throw a SecurityException. Then the supplied URLs are added   * in the order given to the URLClassLoader which uses these URLs to   * load classes and resources (after using the supplied parent ClassLoader).   * It will use the supplied <CODE>URLStreamHandlerFactory</CODE> to get the   * protocol handlers of the supplied URLs.   * @param urls Locations that should be searched by this ClassLoader when   * resolving Classes or Resources.   * @param parent The parent class loader used before trying this class   * loader.   * @param factory Used to get the protocol handler for the URLs.   * @exception SecurityException if the SecurityManager disallows the   * creation of a ClassLoader.   * @exception SecurityException   * @see SecureClassLoader   */  public URLClassLoader(URL[] urls, ClassLoader parent,                        URLStreamHandlerFactory factory)    throws SecurityException  {    super(parent);    this.securityContext = null;    this.factory = factory;    addURLs(urls);    // If this factory is still not in factoryCache, add it,    //   since we only support three protocols so far, 5 is enough    //   for cache initial size    synchronized (factoryCache)      {        if (factory != null && factoryCache.get(factory) == null)          factoryCache.put(factory, new HashMap(5));      }  }  // Methods  /**   * Adds a new location to the end of the internal URL store.   * @param newUrl the location to add   */  protected void addURL(URL newUrl)  {    urls.add(newUrl);    addURLImpl(newUrl);  }  private void addURLImpl(URL newUrl)  {    synchronized (this)      {        if (newUrl == null)          return; // Silently ignore...	// Reset the toString() value.	thisString = null;        // Check global cache to see if there're already url loader        // for this url.        URLLoader loader = (URLLoader) urlloaders.get(newUrl);        if (loader == null)          {            String file = newUrl.getFile();            String protocol = newUrl.getProtocol();	    // If we have a file: URL, we want to make it absolute	    // here, before we decide whether it is really a jar.	    URL absoluteURL;	    if ("file".equals (protocol))	      {		File dir = new File(file);		URL absUrl;		try		  {		    absoluteURL = dir.getCanonicalFile().toURL();		  }		catch (IOException ignore)		  {		    try		      {			absoluteURL = dir.getAbsoluteFile().toURL();		      }		    catch (MalformedURLException _)		      {			// This really should not happen.			absoluteURL = newUrl;		      }		  }	      }	    else	      {		// This doesn't hurt, and it simplifies the logic a		// little.		absoluteURL = newUrl;	      }            // Check that it is not a directory	    if ("gcjlib".equals(protocol))	      loader = new SoURLLoader(this, newUrl);	    else if (! (file.endsWith("/") || file.endsWith(File.separator)))              loader = new JarURLLoader(this, newUrl, absoluteURL);            else if ("file".equals(protocol))	      loader = new FileURLLoader(this, newUrl, absoluteURL);	    else if ("core".equals(protocol))	      loader = new CoreURLLoader(this, newUrl);            else              loader = new RemoteURLLoader(this, newUrl);            // Cache it.            urlloaders.put(newUrl, loader);          }	urlinfos.add(loader);	Vector extraUrls = loader.getClassPath();	if (extraUrls != null)	  {	    Iterator it = extraUrls.iterator();	    while (it.hasNext())	      {		URL url = (URL)it.next();		URLLoader extraLoader = (URLLoader) urlloaders.get(url);		if (! urlinfos.contains (extraLoader))		  addURLImpl(url);	      }	  }      }  }  /**   * Adds an array of new locations to the end of the internal URL   * store.  Called from the the constructors. Should not call to the   * protected addURL() method since that can be overridden and   * subclasses are not yet in a good state at this point.   * jboss 4.0.3 for example depends on this.   *   * @param newUrls the locations to add   */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -