GNU Classpath (0.20) | |
Frames | No Frames |
1: /* URL.java -- Uniform Resource Locator Class 2: Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006 3: Free Software Foundation, Inc. 4: 5: This file is part of GNU Classpath. 6: 7: GNU Classpath is free software; you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation; either version 2, or (at your option) 10: any later version. 11: 12: GNU Classpath is distributed in the hope that it will be useful, but 13: WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15: General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with GNU Classpath; see the file COPYING. If not, write to the 19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20: 02110-1301 USA. 21: 22: Linking this library statically or dynamically with other modules is 23: making a combined work based on this library. Thus, the terms and 24: conditions of the GNU General Public License cover the whole 25: combination. 26: 27: As a special exception, the copyright holders of this library give you 28: permission to link this library with independent modules to produce an 29: executable, regardless of the license terms of these independent 30: modules, and to copy and distribute the resulting executable under 31: terms of your choice, provided that you also meet, for each linked 32: independent module, the terms and conditions of the license of that 33: module. An independent module is a module which is not derived from 34: or based on this library. If you modify this library, you may extend 35: this exception to your version of the library, but you are not 36: obligated to do so. If you do not wish to do so, delete this 37: exception statement from your version. */ 38: 39: package java.net; 40: 41: import gnu.classpath.SystemProperties; 42: import gnu.java.net.URLParseError; 43: 44: import java.io.IOException; 45: import java.io.InputStream; 46: import java.io.ObjectInputStream; 47: import java.io.ObjectOutputStream; 48: import java.io.Serializable; 49: import java.security.AccessController; 50: import java.security.PrivilegedAction; 51: import java.util.HashMap; 52: import java.util.StringTokenizer; 53: 54: 55: /* 56: * Written using on-line Java Platform 1.2 API Specification, as well 57: * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). 58: * Status: Believed complete and correct. 59: */ 60: 61: /** 62: * This final class represents an Internet Uniform Resource Locator (URL). 63: * For details on the syntax of URL's and what they can be used for, 64: * refer to RFC 1738, available from <a 65: * href="http://ds.internic.net/rfcs/rfc1738.txt"> 66: * http://ds.internic.net/rfcs/rfc1738.txt</a> 67: * <p> 68: * There are a great many protocols supported by URL's such as "http", 69: * "ftp", and "file". This object can handle any arbitrary URL for which 70: * a URLStreamHandler object can be written. Default protocol handlers 71: * are provided for the "http" and "ftp" protocols. Additional protocols 72: * handler implementations may be provided in the future. In any case, 73: * an application or applet can install its own protocol handlers that 74: * can be "chained" with other protocol hanlders in the system to extend 75: * the base functionality provided with this class. (Note, however, that 76: * unsigned applets cannot access properties by default or install their 77: * own protocol handlers). 78: * <p> 79: * This chaining is done via the system property java.protocol.handler.pkgs 80: * If this property is set, it is assumed to be a "|" separated list of 81: * package names in which to attempt locating protocol handlers. The 82: * protocol handler is searched for by appending the string 83: * ".<protocol>.Handler" to each packed in the list until a hander is 84: * found. If a protocol handler is not found in this list of packages, or if 85: * the property does not exist, then the default protocol handler of 86: * "gnu.java.net.<protocol>.Handler" is tried. If this is 87: * unsuccessful, a MalformedURLException is thrown. 88: * <p> 89: * All of the constructor methods of URL attempt to load a protocol 90: * handler and so any needed protocol handlers must be installed when 91: * the URL is constructed. 92: * <p> 93: * Here is an example of how URL searches for protocol handlers. Assume 94: * the value of java.protocol.handler.pkgs is "com.foo|com.bar" and the 95: * URL is "news://comp.lang.java.programmer". URL would looking the 96: * following places for protocol handlers: 97: * <p><pre> 98: * com.foo.news.Handler 99: * com.bar.news.Handler 100: * gnu.java.net.news.Handler 101: * </pre><p> 102: * If the protocol handler is not found in any of those locations, a 103: * MalformedURLException would be thrown. 104: * <p> 105: * Please note that a protocol handler must be a subclass of 106: * URLStreamHandler. 107: * <p> 108: * Normally, this class caches protocol handlers. Once it finds a handler 109: * for a particular protocol, it never tries to look up a new handler 110: * again. However, if the system property 111: * gnu.java.net.nocache_protocol_handlers is set, then this 112: * caching behavior is disabled. This property is specific to this 113: * implementation. Sun's JDK may or may not do protocol caching, but it 114: * almost certainly does not examine this property. 115: * <p> 116: * Please also note that an application can install its own factory for 117: * loading protocol handlers (see setURLStreamHandlerFactory). If this is 118: * done, then the above information is superseded and the behavior of this 119: * class in loading protocol handlers is dependent on that factory. 120: * 121: * @author Aaron M. Renn (arenn@urbanophile.com) 122: * @author Warren Levy (warrenl@cygnus.com) 123: * 124: * @see URLStreamHandler 125: */ 126: public final class URL implements Serializable 127: { 128: private static final String DEFAULT_SEARCH_PATH = 129: "gnu.java.net.protocol|gnu.inet"; 130: 131: // Cached System ClassLoader 132: private static ClassLoader systemClassLoader; 133: 134: /** 135: * The name of the protocol for this URL. 136: * The protocol is always stored in lower case. 137: */ 138: private String protocol; 139: 140: /** 141: * The "authority" portion of the URL. 142: */ 143: private String authority; 144: 145: /** 146: * The hostname or IP address of this protocol. 147: * This includes a possible user. For example <code>joe@some.host.net</code>. 148: */ 149: private String host; 150: 151: /** 152: * The user information necessary to establish the connection. 153: */ 154: private String userInfo; 155: 156: /** 157: * The port number of this protocol or -1 if the port number used is 158: * the default for this protocol. 159: */ 160: private int port = -1; // Initialize for constructor using context. 161: 162: /** 163: * The "file" portion of the URL. It is defined as <code>path[?query]</code>. 164: */ 165: private String file; 166: 167: /** 168: * The anchor portion of the URL. 169: */ 170: private String ref; 171: 172: /** 173: * This is the hashCode for this URL 174: */ 175: private int hashCode; 176: 177: /** 178: * The protocol handler in use for this URL 179: */ 180: transient URLStreamHandler ph; 181: 182: /** 183: * If an application installs its own protocol handler factory, this is 184: * where we keep track of it. 185: */ 186: private static URLStreamHandlerFactory factory; 187: private static final long serialVersionUID = -7627629688361524110L; 188: 189: /** 190: * This a table where we cache protocol handlers to avoid the overhead 191: * of looking them up each time. 192: */ 193: private static HashMap ph_cache = new HashMap(); 194: 195: /** 196: * Whether or not to cache protocol handlers. 197: */ 198: private static boolean cache_handlers; 199: 200: static 201: { 202: String s = SystemProperties.getProperty("gnu.java.net.nocache_protocol_handlers"); 203: 204: if (s == null) 205: cache_handlers = true; 206: else 207: cache_handlers = false; 208: } 209: 210: /** 211: * Constructs a URL and loads a protocol handler for the values passed as 212: * arguments. 213: * 214: * @param protocol The protocol for this URL ("http", "ftp", etc) 215: * @param host The hostname or IP address to connect to 216: * @param port The port number to use, or -1 to use the protocol's 217: * default port 218: * @param file The "file" portion of the URL. 219: * 220: * @exception MalformedURLException If a protocol handler cannot be loaded or 221: * a parse error occurs. 222: */ 223: public URL(String protocol, String host, int port, String file) 224: throws MalformedURLException 225: { 226: this(protocol, host, port, file, null); 227: } 228: 229: /** 230: * Constructs a URL and loads a protocol handler for the values passed in 231: * as arugments. Uses the default port for the protocol. 232: * 233: * @param protocol The protocol for this URL ("http", "ftp", etc) 234: * @param host The hostname or IP address for this URL 235: * @param file The "file" portion of this URL. 236: * 237: * @exception MalformedURLException If a protocol handler cannot be loaded or 238: * a parse error occurs. 239: */ 240: public URL(String protocol, String host, String file) 241: throws MalformedURLException 242: { 243: this(protocol, host, -1, file, null); 244: } 245: 246: /** 247: * This method initializes a new instance of <code>URL</code> with the 248: * specified protocol, host, port, and file. Additionally, this method 249: * allows the caller to specify a protocol handler to use instead of 250: * the default. If this handler is specified, the caller must have 251: * the "specifyStreamHandler" permission (see <code>NetPermission</code>) 252: * or a <code>SecurityException</code> will be thrown. 253: * 254: * @param protocol The protocol for this URL ("http", "ftp", etc) 255: * @param host The hostname or IP address to connect to 256: * @param port The port number to use, or -1 to use the protocol's default 257: * port 258: * @param file The "file" portion of the URL. 259: * @param ph The protocol handler to use with this URL. 260: * 261: * @exception MalformedURLException If no protocol handler can be loaded 262: * for the specified protocol. 263: * @exception SecurityException If the <code>SecurityManager</code> exists 264: * and does not allow the caller to specify its own protocol handler. 265: * 266: * @since 1.2 267: */ 268: public URL(String protocol, String host, int port, String file, 269: URLStreamHandler ph) throws MalformedURLException 270: { 271: if (protocol == null) 272: throw new MalformedURLException("null protocol"); 273: protocol = protocol.toLowerCase(); 274: this.protocol = protocol; 275: 276: if (ph != null) 277: { 278: SecurityManager s = System.getSecurityManager(); 279: if (s != null) 280: s.checkPermission(new NetPermission("specifyStreamHandler")); 281: 282: this.ph = ph; 283: } 284: else 285: this.ph = getURLStreamHandler(protocol); 286: 287: if (this.ph == null) 288: throw new MalformedURLException("Protocol handler not found: " 289: + protocol); 290: 291: this.host = host; 292: this.port = port; 293: this.authority = (host != null) ? host : ""; 294: if (port >= 0 && host != null) 295: this.authority += ":" + port; 296: 297: int hashAt = file.indexOf('#'); 298: if (hashAt < 0) 299: { 300: this.file = file; 301: this.ref = null; 302: } 303: else 304: { 305: this.file = file.substring(0, hashAt); 306: this.ref = file.substring(hashAt + 1); 307: } 308: hashCode = hashCode(); // Used for serialization. 309: } 310: 311: /** 312: * Initializes a URL from a complete string specification such as 313: * "http://www.urbanophile.com/arenn/". First the protocol name is parsed 314: * out of the string. Then a handler is located for that protocol and 315: * the parseURL() method of that protocol handler is used to parse the 316: * remaining fields. 317: * 318: * @param spec The complete String representation of a URL 319: * 320: * @exception MalformedURLException If a protocol handler cannot be found 321: * or the URL cannot be parsed 322: */ 323: public URL(String spec) throws MalformedURLException 324: { 325: this((URL) null, spec != null ? spec : "", (URLStreamHandler) null); 326: } 327: 328: /** 329: * This method parses a String representation of a URL within the 330: * context of an existing URL. Principally this means that any 331: * fields not present the URL are inheritied from the context URL. 332: * This allows relative URL's to be easily constructed. If the 333: * context argument is null, then a complete URL must be specified 334: * in the URL string. If the protocol parsed out of the URL is 335: * different from the context URL's protocol, then then URL String 336: * is also expected to be a complete URL. 337: * 338: * @param context The context on which to parse the specification 339: * @param spec The string to parse an URL 340: * 341: * @exception MalformedURLException If a protocol handler cannot be found 342: * for the URL cannot be parsed 343: */ 344: public URL(URL context, String spec) throws MalformedURLException 345: { 346: this(context, spec, (context == null) ? (URLStreamHandler)null : context.ph); 347: } 348: 349: /** 350: * Creates an URL from given arguments 351: * This method parses a String representation of a URL within the 352: * context of an existing URL. Principally this means that any fields 353: * not present the URL are inheritied from the context URL. This allows 354: * relative URL's to be easily constructed. If the context argument is 355: * null, then a complete URL must be specified in the URL string. 356: * If the protocol parsed out of the URL is different 357: * from the context URL's protocol, then then URL String is also 358: * expected to be a complete URL. 359: * <p> 360: * Additionally, this method allows the caller to specify a protocol handler 361: * to use instead of the default. If this handler is specified, the caller 362: * must have the "specifyStreamHandler" permission 363: * (see <code>NetPermission</code>) or a <code>SecurityException</code> 364: * will be thrown. 365: * 366: * @param context The context in which to parse the specification 367: * @param spec The string to parse as an URL 368: * @param ph The stream handler for the URL 369: * 370: * @exception MalformedURLException If a protocol handler cannot be found 371: * or the URL cannot be parsed 372: * @exception SecurityException If the <code>SecurityManager</code> exists 373: * and does not allow the caller to specify its own protocol handler. 374: * 375: * @since 1.2 376: */ 377: public URL(URL context, String spec, URLStreamHandler ph) 378: throws MalformedURLException 379: { 380: /* A protocol is defined by the doc as the substring before a ':' 381: * as long as the ':' occurs before any '/'. 382: * 383: * If context is null, then spec must be an absolute URL. 384: * 385: * The relative URL need not specify all the components of a URL. 386: * If the protocol, host name, or port number is missing, the value 387: * is inherited from the context. A bare file component is appended 388: * to the context's file. The optional anchor is not inherited. 389: */ 390: 391: // If this is an absolute URL, then ignore context completely. 392: // An absolute URL must have chars prior to "://" but cannot have a colon 393: // right after the "://". The second colon is for an optional port value 394: // and implies that the host from the context is used if available. 395: int colon; 396: int slash = spec.indexOf('/'); 397: if ((colon = spec.indexOf("://", 1)) > 0 398: && ((colon < slash || slash < 0)) 399: && ! spec.regionMatches(colon, "://:", 0, 4)) 400: context = null; 401: 402: if ((colon = spec.indexOf(':')) > 0 403: && (colon < slash || slash < 0)) 404: { 405: // Protocol specified in spec string. 406: protocol = spec.substring(0, colon).toLowerCase(); 407: if (context != null && context.protocol.equals(protocol)) 408: { 409: // The 1.2 doc specifically says these are copied to the new URL. 410: host = context.host; 411: port = context.port; 412: userInfo = context.userInfo; 413: authority = context.authority; 414: } 415: } 416: else if (context != null) 417: { 418: // Protocol NOT specified in spec string. 419: // Use context fields (except ref) as a foundation for relative URLs. 420: colon = -1; 421: protocol = context.protocol; 422: host = context.host; 423: port = context.port; 424: userInfo = context.userInfo; 425: if (spec.indexOf(":/", 1) < 0) 426: { 427: file = context.file; 428: if (file == null || file.length() == 0) 429: file = "/"; 430: } 431: authority = context.authority; 432: } 433: else // Protocol NOT specified in spec. and no context available. 434: throw new MalformedURLException("Absolute URL required with null" 435: + " context: " + spec); 436: 437: protocol = protocol.trim(); 438: 439: if (ph != null) 440: { 441: SecurityManager s = System.getSecurityManager(); 442: if (s != null) 443: s.checkPermission(new NetPermission("specifyStreamHandler")); 444: 445: this.ph = ph; 446: } 447: else 448: this.ph = getURLStreamHandler(protocol); 449: 450: if (this.ph == null) 451: throw new MalformedURLException("Protocol handler not found: " 452: + protocol); 453: 454: // JDK 1.2 doc for parseURL specifically states that any '#' ref 455: // is to be excluded by passing the 'limit' as the indexOf the '#' 456: // if one exists, otherwise pass the end of the string. 457: int hashAt = spec.indexOf('#', colon + 1); 458: 459: try 460: { 461: this.ph.parseURL(this, spec, colon + 1, 462: hashAt < 0 ? spec.length() : hashAt); 463: } 464: catch (URLParseError e) 465: { 466: throw new MalformedURLException(e.getMessage()); 467: } 468: 469: if (hashAt >= 0) 470: ref = spec.substring(hashAt + 1); 471: 472: hashCode = hashCode(); // Used for serialization. 473: } 474: 475: /** 476: * Test another URL for equality with this one. This will be true only if 477: * the argument is non-null and all of the fields in the URL's match 478: * exactly (ie, protocol, host, port, file, and ref). Overrides 479: * Object.equals(), implemented by calling the equals method of the handler. 480: * 481: * @param obj The URL to compare with 482: * 483: * @return true if the URL is equal, false otherwise 484: */ 485: public boolean equals(Object obj) 486: { 487: if (! (obj instanceof URL)) 488: return false; 489: 490: return ph.equals(this, (URL) obj); 491: } 492: 493: /** 494: * Returns the contents of this URL as an object by first opening a 495: * connection, then calling the getContent() method against the connection 496: * 497: * @return A content object for this URL 498: * @exception IOException If opening the connection or getting the 499: * content fails. 500: * 501: * @since 1.3 502: */ 503: public Object getContent() throws IOException 504: { 505: return openConnection().getContent(); 506: } 507: 508: /** 509: * Gets the contents of this URL 510: * 511: * @param classes The allow classes for the content object. 512: * 513: * @return a context object for this URL. 514: * 515: * @exception IOException If an error occurs 516: */ 517: public Object getContent(Class[] classes) throws IOException 518: { 519: // FIXME: implement this 520: return getContent(); 521: } 522: 523: /** 524: * Returns the file portion of the URL. 525: * Defined as <code>path[?query]</code>. 526: * Returns the empty string if there is no file portion. 527: * 528: * @return The filename specified in this URL, or an empty string if empty. 529: */ 530: public String getFile() 531: { 532: return file == null ? "" : file; 533: } 534: 535: /** 536: * Returns the path of the URL. This is the part of the file before any '?' 537: * character. 538: * 539: * @return The path specified in this URL, or null if empty. 540: * 541: * @since 1.3 542: */ 543: public String getPath() 544: { 545: // The spec says we need to return an empty string, but some 546: // applications depends on receiving null when the path is empty. 547: if (file == null) 548: return null; 549: int quest = file.indexOf('?'); 550: return quest < 0 ? getFile() : file.substring(0, quest); 551: } 552: 553: /** 554: * Returns the authority of the URL 555: * 556: * @return The authority specified in this URL. 557: * 558: * @since 1.3 559: */ 560: public String getAuthority() 561: { 562: return authority; 563: } 564: 565: /** 566: * Returns the host of the URL 567: * 568: * @return The host specified in this URL. 569: */ 570: public String getHost() 571: { 572: int at = (host == null) ? -1 : host.indexOf('@'); 573: return at < 0 ? host : host.substring(at + 1, host.length()); 574: } 575: 576: /** 577: * Returns the port number of this URL or -1 if the default port number is 578: * being used. 579: * 580: * @return The port number 581: * 582: * @see #getDefaultPort() 583: */ 584: public int getPort() 585: { 586: return port; 587: } 588: 589: /** 590: * Returns the default port of the URL. If the StreamHandler for the URL 591: * protocol does not define a default port it returns -1. 592: * 593: * @return The default port of the current protocol. 594: */ 595: public int getDefaultPort() 596: { 597: return ph.getDefaultPort(); 598: } 599: 600: /** 601: * Returns the protocol of the URL 602: * 603: * @return The specified protocol. 604: */ 605: public String getProtocol() 606: { 607: return protocol; 608: } 609: 610: /** 611: * Returns the ref (sometimes called the "# reference" or "anchor") portion 612: * of the URL. 613: * 614: * @return The ref 615: */ 616: public String getRef() 617: { 618: return ref; 619: } 620: 621: /** 622: * Returns the user information of the URL. This is the part of the host 623: * name before the '@'. 624: * 625: * @return the user at a particular host or null when no user defined. 626: */ 627: public String getUserInfo() 628: { 629: if (userInfo != null) 630: return userInfo; 631: int at = (host == null) ? -1 : host.indexOf('@'); 632: return at < 0 ? null : host.substring(0, at); 633: } 634: 635: /** 636: * Returns the query of the URL. This is the part of the file before the 637: * '?'. 638: * 639: * @return the query part of the file, or null when there is no query part. 640: */ 641: public String getQuery() 642: { 643: int quest = (file == null) ? -1 : file.indexOf('?'); 644: return quest < 0 ? null : file.substring(quest + 1, file.length()); 645: } 646: 647: /** 648: * Returns a hashcode computed by the URLStreamHandler of this URL 649: * 650: * @return The hashcode for this URL. 651: */ 652: public int hashCode() 653: { 654: if (hashCode != 0) 655: return hashCode; // Use cached value if available. 656: else 657: return ph.hashCode(this); 658: } 659: 660: /** 661: * Returns a URLConnection object that represents a connection to the remote 662: * object referred to by the URL. The URLConnection is created by calling the 663: * openConnection() method of the protocol handler 664: * 665: * @return A URLConnection for this URL 666: * 667: * @exception IOException If an error occurs 668: */ 669: public URLConnection openConnection() throws IOException 670: { 671: return ph.openConnection(this); 672: } 673: 674: /** 675: * Opens a connection to this URL and returns an InputStream for reading 676: * from that connection 677: * 678: * @return An <code>InputStream</code> for this URL. 679: * 680: * @exception IOException If an error occurs 681: */ 682: public InputStream openStream() throws IOException 683: { 684: return openConnection().getInputStream(); 685: } 686: 687: /** 688: * Tests whether or not another URL refers to the same "file" as this one. 689: * This will be true if and only if the passed object is not null, is a 690: * URL, and matches all fields but the ref (ie, protocol, host, port, 691: * and file); 692: * 693: * @param url The URL object to test with 694: * 695: * @return true if URL matches this URL's file, false otherwise 696: */ 697: public boolean sameFile(URL url) 698: { 699: return ph.sameFile(this, url); 700: } 701: 702: /** 703: * Sets the specified fields of the URL. This is not a public method so 704: * that only URLStreamHandlers can modify URL fields. This might be called 705: * by the <code>parseURL()</code> method in that class. URLs are otherwise 706: * constant. If the given protocol does not exist, it will keep the previously 707: * set protocol. 708: * 709: * @param protocol The protocol name for this URL 710: * @param host The hostname or IP address for this URL 711: * @param port The port number of this URL 712: * @param file The "file" portion of this URL. 713: * @param ref The anchor portion of this URL. 714: */ 715: protected void set(String protocol, String host, int port, String file, 716: String ref) 717: { 718: URLStreamHandler protocolHandler = null; 719: protocol = protocol.toLowerCase(); 720: if (! this.protocol.equals(protocol)) 721: protocolHandler = getURLStreamHandler(protocol); 722: 723: // It is an hidden feature of the JDK. If the protocol does not exist, 724: // we keep the previously initialized protocol. 725: if (protocolHandler != null) 726: { 727: this.ph = protocolHandler; 728: this.protocol = protocol; 729: } 730: this.authority = ""; 731: this.port = port; 732: this.host = host; 733: this.file = file; 734: this.ref = ref; 735: 736: if (host != null) 737: this.authority += host; 738: if (port >= 0) 739: this.authority += ":" + port; 740: 741: hashCode = hashCode(); // Used for serialization. 742: } 743: 744: /** 745: * Sets the specified fields of the URL. This is not a public method so 746: * that only URLStreamHandlers can modify URL fields. URLs are otherwise 747: * constant. If the given protocol does not exist, it will keep the previously 748: * set protocol. 749: * 750: * @param protocol The protocol name for this URL. 751: * @param host The hostname or IP address for this URL. 752: * @param port The port number of this URL. 753: * @param authority The authority of this URL. 754: * @param userInfo The user and password (if needed) of this URL. 755: * @param path The "path" portion of this URL. 756: * @param query The query of this URL. 757: * @param ref The anchor portion of this URL. 758: * 759: * @since 1.3 760: */ 761: protected void set(String protocol, String host, int port, String authority, 762: String userInfo, String path, String query, String ref) 763: { 764: URLStreamHandler protocolHandler = null; 765: protocol = protocol.toLowerCase(); 766: if (! this.protocol.equals(protocol)) 767: protocolHandler = getURLStreamHandler(protocol); 768: 769: // It is an hidden feature of the JDK. If the protocol does not exist, 770: // we keep the previously initialized protocol. 771: if (protocolHandler != null) 772: { 773: this.ph = protocolHandler; 774: this.protocol = protocol; 775: } 776: this.host = host; 777: this.userInfo = userInfo; 778: this.port = port; 779: this.authority = authority; 780: if (query == null) 781: this.file = path; 782: else 783: this.file = path + "?" + query; 784: this.ref = ref; 785: hashCode = hashCode(); // Used for serialization. 786: } 787: 788: /** 789: * Sets the URLStreamHandlerFactory for this class. This factory is 790: * responsible for returning the appropriate protocol handler for 791: * a given URL. 792: * 793: * @param fac The URLStreamHandlerFactory class to use 794: * 795: * @exception Error If the factory is alread set. 796: * @exception SecurityException If a security manager exists and its 797: * checkSetFactory method doesn't allow the operation 798: */ 799: public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) 800: { 801: if (factory != null) 802: throw new Error("URLStreamHandlerFactory already set"); 803: 804: // Throw an exception if an extant security mgr precludes 805: // setting the factory. 806: SecurityManager s = System.getSecurityManager(); 807: if (s != null) 808: s.checkSetFactory(); 809: factory = fac; 810: } 811: 812: /** 813: * Returns a String representing this URL. The String returned is 814: * created by calling the protocol handler's toExternalForm() method. 815: * 816: * @return A string for this URL 817: */ 818: public String toExternalForm() 819: { 820: // Identical to toString(). 821: return ph.toExternalForm(this); 822: } 823: 824: /** 825: * Returns a String representing this URL. Identical to toExternalForm(). 826: * The value returned is created by the protocol handler's 827: * toExternalForm method. Overrides Object.toString() 828: * 829: * @return A string for this URL 830: */ 831: public String toString() 832: { 833: // Identical to toExternalForm(). 834: return ph.toExternalForm(this); 835: } 836: 837: /** 838: * This internal method is used in two different constructors to load 839: * a protocol handler for this URL. 840: * 841: * @param protocol The protocol to load a handler for 842: * 843: * @return A URLStreamHandler for this protocol, or null when not found. 844: */ 845: private static synchronized URLStreamHandler getURLStreamHandler(String protocol) 846: { 847: URLStreamHandler ph = null; 848: 849: // First, see if a protocol handler is in our cache. 850: if (cache_handlers) 851: { 852: if ((ph = (URLStreamHandler) ph_cache.get(protocol)) != null) 853: return ph; 854: } 855: 856: // If a non-default factory has been set, use it to find the protocol. 857: if (factory != null) 858: { 859: ph = factory.createURLStreamHandler(protocol); 860: } 861: 862: // Non-default factory may have returned null or a factory wasn't set. 863: // Use the default search algorithm to find a handler for this protocol. 864: if (ph == null) 865: { 866: // Get the list of packages to check and append our default handler 867: // to it, along with the JDK specified default as a last resort. 868: // Except in very unusual environments the JDK specified one shouldn't 869: // ever be needed (or available). 870: String ph_search_path = 871: SystemProperties.getProperty("java.protocol.handler.pkgs"); 872: 873: // Tack our default package on at the ends. 874: if (ph_search_path != null) 875: ph_search_path += "|" + DEFAULT_SEARCH_PATH; 876: else 877: ph_search_path = DEFAULT_SEARCH_PATH; 878: 879: // Finally loop through our search path looking for a match. 880: StringTokenizer pkgPrefix = new StringTokenizer(ph_search_path, "|"); 881: 882: // Cache the systemClassLoader 883: if (systemClassLoader == null) 884: { 885: systemClassLoader = (ClassLoader) AccessController.doPrivileged 886: (new PrivilegedAction() { 887: public Object run() 888: { 889: return ClassLoader.getSystemClassLoader(); 890: } 891: }); 892: } 893: 894: do 895: { 896: try 897: { 898: // Try to get a class from the system/application 899: // classloader, initialize it, make an instance 900: // and try to cast it to a URLStreamHandler. 901: String clsName = 902: (pkgPrefix.nextToken() + "." + protocol + ".Handler"); 903: Class c = Class.forName(clsName, true, systemClassLoader); 904: ph = (URLStreamHandler) c.newInstance(); 905: } 906: catch (ThreadDeath death) 907: { 908: throw death; 909: } 910: catch (Throwable t) 911: { 912: // Ignored. 913: } 914: } 915: while (ph == null && pkgPrefix.hasMoreTokens()); 916: } 917: 918: // Update the hashtable with the new protocol handler. 919: if (ph != null && cache_handlers) 920: ph_cache.put(protocol, ph); 921: else 922: ph = null; 923: 924: return ph; 925: } 926: 927: private void readObject(ObjectInputStream ois) 928: throws IOException, ClassNotFoundException 929: { 930: ois.defaultReadObject(); 931: this.ph = getURLStreamHandler(protocol); 932: if (this.ph == null) 933: throw new IOException("Handler for protocol " + protocol + " not found"); 934: } 935: 936: private void writeObject(ObjectOutputStream oos) throws IOException 937: { 938: oos.defaultWriteObject(); 939: } 940: 941: /** 942: * Returns the equivalent <code>URI</code> object for this <code>URL</code>. 943: * This is the same as calling <code>new URI(this.toString())</code>. 944: * RFC2396-compliant URLs are guaranteed a successful conversion to 945: * a <code>URI</code> instance. However, there are some values which 946: * form valid URLs, but which do not also form RFC2396-compliant URIs. 947: * 948: * @throws URISyntaxException if this URL is not RFC2396-compliant, 949: * and thus can not be successfully converted to a URI. 950: */ 951: public URI toURI() 952: throws URISyntaxException 953: { 954: return new URI(toString()); 955: } 956: 957: }
GNU Classpath (0.20) |