diff -r d739cdce3891 -r 0a582b5a2737 emul/src/main/java/java/net/URL.java --- a/emul/src/main/java/java/net/URL.java Tue Oct 30 09:05:33 2012 +0100 +++ b/emul/src/main/java/java/net/URL.java Tue Oct 30 09:24:41 2012 +0100 @@ -27,10 +27,6 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.util.Hashtable; -import java.util.StringTokenizer; -import sun.security.util.SecurityConstants; /** * Class URL represents a Uniform Resource @@ -200,17 +196,6 @@ */ private String ref; - /** - * The host's IP address, used in equals and hashCode. - * Computed on demand. An uninitialized or unknown hostAddress is null. - */ - transient InetAddress hostAddress; - - /** - * The URLStreamHandler for this URL. - */ - transient URLStreamHandler handler; - /* Our hash code. * @serial */ @@ -323,53 +308,10 @@ this(protocol, host, -1, file); } - /** - * Creates a URL object from the specified - * protocol, host, port - * number, file, and handler. Specifying - * a port number of -1 indicates that - * the URL should use the default port for the protocol. Specifying - * a handler of null indicates that the URL - * should use a default stream handler for the protocol, as outlined - * for: - * java.net.URL#URL(java.lang.String, java.lang.String, int, - * java.lang.String) - * - *

If the handler is not null and there is a security manager, - * the security manager's checkPermission - * method is called with a - * NetPermission("specifyStreamHandler") permission. - * This may result in a SecurityException. - * - * No validation of the inputs is performed by this constructor. - * - * @param protocol the name of the protocol to use. - * @param host the name of the host. - * @param port the port number on the host. - * @param file the file on the host - * @param handler the stream handler for the URL. - * @exception MalformedURLException if an unknown protocol is specified. - * @exception SecurityException - * if a security manager exists and its - * checkPermission method doesn't allow - * specifying a stream handler explicitly. - * @see java.lang.System#getProperty(java.lang.String) - * @see java.net.URL#setURLStreamHandlerFactory( - * java.net.URLStreamHandlerFactory) - * @see java.net.URLStreamHandler - * @see java.net.URLStreamHandlerFactory#createURLStreamHandler( - * java.lang.String) - * @see SecurityManager#checkPermission - * @see java.net.NetPermission - */ - public URL(String protocol, String host, int port, String file, - URLStreamHandler handler) throws MalformedURLException { + private URL(String protocol, String host, int port, String file, + Object handler) throws MalformedURLException { if (handler != null) { - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - // check for permission to specify a handler - checkSpecifyHandler(sm); - } + throw new SecurityException(); } protocol = protocol.toLowerCase(); @@ -406,11 +348,10 @@ // Note: we don't do validation of the URL here. Too risky to change // right now, but worth considering for future reference. -br - if (handler == null && - (handler = getURLStreamHandler(protocol)) == null) { - throw new MalformedURLException("unknown protocol: " + protocol); - } - this.handler = handler; +// if (handler == null && +// (handler = getURLStreamHandler(protocol)) == null) { +// throw new MalformedURLException("unknown protocol: " + protocol); +// } } /** @@ -500,7 +441,7 @@ * @see java.net.URLStreamHandler#parseURL(java.net.URL, * java.lang.String, int, int) */ - public URL(URL context, String spec, URLStreamHandler handler) + private URL(URL context, String spec, Object handler) throws MalformedURLException { String original = spec; @@ -512,10 +453,7 @@ // Check for permission to specify a handler if (handler != null) { - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - checkSpecifyHandler(sm); - } + throw new SecurityException(); } try { @@ -556,9 +494,9 @@ newProtocol.equalsIgnoreCase(context.protocol))) { // inherit the protocol handler from the context // if not specified to the constructor - if (handler == null) { - handler = context.handler; - } +// if (handler == null) { +// handler = context.handler; +// } // If the context is a hierarchical URL scheme and the spec // contains a matching scheme then maintain backwards @@ -585,12 +523,12 @@ // Get the protocol handler if not specified or the protocol // of the context could not be used - if (handler == null && - (handler = getURLStreamHandler(protocol)) == null) { - throw new MalformedURLException("unknown protocol: "+protocol); - } +// if (handler == null && +// (handler = getURLStreamHandler(protocol)) == null) { +// throw new MalformedURLException("unknown protocol: "+protocol); +// } - this.handler = handler; +// this.handler = handler; i = spec.indexOf('#', start); if (i >= 0) { @@ -609,7 +547,7 @@ } } - handler.parseURL(this, spec, start, limit); +// handler.parseURL(this, spec, start, limit); } catch(MalformedURLException e) { throw e; @@ -640,13 +578,6 @@ return true; } - /* - * Checks for permission to specify a stream handler. - */ - private void checkSpecifyHandler(SecurityManager sm) { - sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION); - } - /** * Sets the fields of the URL. This is not a public method so that * only URLStreamHandlers can modify URL fields. URLs are @@ -670,7 +601,6 @@ /* This is very important. We must recompute this after the * URL has been changed. */ hashCode = -1; - hostAddress = null; int q = file.lastIndexOf('?'); if (q != -1) { query = file.substring(q+1); @@ -709,7 +639,6 @@ /* This is very important. We must recompute this after the * URL has been changed. */ hashCode = -1; - hostAddress = null; this.query = query; this.authority = authority; } @@ -768,19 +697,6 @@ } /** - * Gets the default port number of the protocol associated - * with this URL. If the URL scheme or the URLStreamHandler - * for the URL do not define a default port number, - * then -1 is returned. - * - * @return the port number - * @since 1.4 - */ - public int getDefaultPort() { - return handler.getDefaultPort(); - } - - /** * Gets the protocol name of this URL. * * @return the protocol of this URL. @@ -857,7 +773,8 @@ return false; URL u2 = (URL)obj; - return handler.equals(this, u2); + // return handler.equals(this, u2); + return u2 == this; } /** @@ -872,7 +789,7 @@ if (hashCode != -1) return hashCode; - hashCode = handler.hashCode(this); + // hashCode = handler.hashCode(this); return hashCode; } @@ -888,7 +805,8 @@ * false otherwise. */ public boolean sameFile(URL other) { - return handler.sameFile(this, other); +// return handler.sameFile(this, other); + throw new UnsupportedOperationException(); } /** @@ -916,24 +834,8 @@ * @see java.net.URLStreamHandler#toExternalForm(java.net.URL) */ public String toExternalForm() { - return handler.toExternalForm(this); - } - - /** - * Returns a {@link java.net.URI} equivalent to this URL. - * This method functions in the same way as new URI (this.toString()). - *

Note, any URL instance that complies with RFC 2396 can be converted - * to a URI. However, some URLs that are not strictly in compliance - * can not be converted to a URI. - * - * @exception URISyntaxException if this URL is not formatted strictly according to - * to RFC2396 and cannot be converted to a URI. - * - * @return a URI instance equivalent to this URL. - * @since 1.5 - */ - public URI toURI() throws URISyntaxException { - return new URI (toString()); + throw new UnsupportedOperationException(); +// return handler.toExternalForm(this); } /** @@ -965,58 +867,10 @@ * @see java.net.URL#URL(java.lang.String, java.lang.String, * int, java.lang.String) */ - public URLConnection openConnection() throws java.io.IOException { - return handler.openConnection(this); - } +// public URLConnection openConnection() throws java.io.IOException { +// return handler.openConnection(this); +// } - /** - * Same as {@link #openConnection()}, except that the connection will be - * made through the specified proxy; Protocol handlers that do not - * support proxing will ignore the proxy parameter and make a - * normal connection. - * - * Invoking this method preempts the system's default ProxySelector - * settings. - * - * @param proxy the Proxy through which this connection - * will be made. If direct connection is desired, - * Proxy.NO_PROXY should be specified. - * @return a URLConnection to the URL. - * @exception IOException if an I/O exception occurs. - * @exception SecurityException if a security manager is present - * and the caller doesn't have permission to connect - * to the proxy. - * @exception IllegalArgumentException will be thrown if proxy is null, - * or proxy has the wrong type - * @exception UnsupportedOperationException if the subclass that - * implements the protocol handler doesn't support - * this method. - * @see java.net.URL#URL(java.lang.String, java.lang.String, - * int, java.lang.String) - * @see java.net.URLConnection - * @see java.net.URLStreamHandler#openConnection(java.net.URL, - * java.net.Proxy) - * @since 1.5 - */ - public URLConnection openConnection(Proxy proxy) - throws java.io.IOException { - if (proxy == null) { - throw new IllegalArgumentException("proxy can not be null"); - } - - // Create a copy of Proxy as a security measure - Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy); - SecurityManager sm = System.getSecurityManager(); - if (p.type() != Proxy.Type.DIRECT && sm != null) { - InetSocketAddress epoint = (InetSocketAddress) p.address(); - if (epoint.isUnresolved()) - sm.checkConnect(epoint.getHostName(), epoint.getPort()); - else - sm.checkConnect(epoint.getAddress().getHostAddress(), - epoint.getPort()); - } - return handler.openConnection(this, p); - } /** * Opens a connection to this URL and returns an @@ -1032,7 +886,8 @@ * @see java.net.URLConnection#getInputStream() */ public final InputStream openStream() throws java.io.IOException { - return openConnection().getInputStream(); + throw new IOException(); +// return openConnection().getInputStream(); } /** @@ -1046,7 +901,8 @@ * @see java.net.URLConnection#getContent() */ public final Object getContent() throws java.io.IOException { - return openConnection().getContent(); + throw new IOException(); +// return openConnection().getContent(); } /** @@ -1065,219 +921,11 @@ */ public final Object getContent(Class[] classes) throws java.io.IOException { - return openConnection().getContent(classes); + throw new IOException(); +// return openConnection().getContent(classes); } - /** - * The URLStreamHandler factory. - */ - static URLStreamHandlerFactory factory; - /** - * Sets an application's URLStreamHandlerFactory. - * This method can be called at most once in a given Java Virtual - * Machine. - * - *

The URLStreamHandlerFactory instance is used to - *construct a stream protocol handler from a protocol name. - * - *

If there is a security manager, this method first calls - * the security manager's checkSetFactory method - * to ensure the operation is allowed. - * This could result in a SecurityException. - * - * @param fac the desired factory. - * @exception Error if the application has already set a factory. - * @exception SecurityException if a security manager exists and its - * checkSetFactory method doesn't allow - * the operation. - * @see java.net.URL#URL(java.lang.String, java.lang.String, - * int, java.lang.String) - * @see java.net.URLStreamHandlerFactory - * @see SecurityManager#checkSetFactory - */ - public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) { - synchronized (streamHandlerLock) { - if (factory != null) { - throw new Error("factory already defined"); - } - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkSetFactory(); - } - handlers.clear(); - factory = fac; - } - } - - /** - * A table of protocol handlers. - */ - static Hashtable handlers = new Hashtable(); - private static Object streamHandlerLock = new Object(); - - /** - * Returns the Stream Handler. - * @param protocol the protocol to use - */ - static URLStreamHandler getURLStreamHandler(String protocol) { - - URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol); - if (handler == null) { - - boolean checkedWithFactory = false; - - // Use the factory (if any) - if (factory != null) { - handler = factory.createURLStreamHandler(protocol); - checkedWithFactory = true; - } - - // Try java protocol handler - if (handler == null) { - String packagePrefixList = null; - - packagePrefixList - = java.security.AccessController.doPrivileged( - new sun.security.action.GetPropertyAction( - protocolPathProp,"")); - if (packagePrefixList != "") { - packagePrefixList += "|"; - } - - // REMIND: decide whether to allow the "null" class prefix - // or not. - packagePrefixList += "sun.net.www.protocol"; - - StringTokenizer packagePrefixIter = - new StringTokenizer(packagePrefixList, "|"); - - while (handler == null && - packagePrefixIter.hasMoreTokens()) { - - String packagePrefix = - packagePrefixIter.nextToken().trim(); - try { - String clsName = packagePrefix + "." + protocol + - ".Handler"; - Class cls = null; - try { - cls = Class.forName(clsName); - } catch (ClassNotFoundException e) { - ClassLoader cl = ClassLoader.getSystemClassLoader(); - if (cl != null) { - cls = cl.loadClass(clsName); - } - } - if (cls != null) { - handler = - (URLStreamHandler)cls.newInstance(); - } - } catch (Exception e) { - // any number of exceptions can get thrown here - } - } - } - - synchronized (streamHandlerLock) { - - URLStreamHandler handler2 = null; - - // Check again with hashtable just in case another - // thread created a handler since we last checked - handler2 = (URLStreamHandler)handlers.get(protocol); - - if (handler2 != null) { - return handler2; - } - - // Check with factory if another thread set a - // factory since our last check - if (!checkedWithFactory && factory != null) { - handler2 = factory.createURLStreamHandler(protocol); - } - - if (handler2 != null) { - // The handler from the factory must be given more - // importance. Discard the default handler that - // this thread created. - handler = handler2; - } - - // Insert this handler into the hashtable - if (handler != null) { - handlers.put(protocol, handler); - } - - } - } - - return handler; - - } - - /** - * WriteObject is called to save the state of the URL to an - * ObjectOutputStream. The handler is not saved since it is - * specific to this system. - * - * @serialData the default write object value. When read back in, - * the reader must ensure that calling getURLStreamHandler with - * the protocol variable returns a valid URLStreamHandler and - * throw an IOException if it does not. - */ - private synchronized void writeObject(java.io.ObjectOutputStream s) - throws IOException - { - s.defaultWriteObject(); // write the fields - } - - /** - * readObject is called to restore the state of the URL from the - * stream. It reads the components of the URL and finds the local - * stream handler. - */ - private synchronized void readObject(java.io.ObjectInputStream s) - throws IOException, ClassNotFoundException - { - s.defaultReadObject(); // read the fields - if ((handler = getURLStreamHandler(protocol)) == null) { - throw new IOException("unknown protocol: " + protocol); - } - - // Construct authority part - if (authority == null && - ((host != null && host.length() > 0) || port != -1)) { - if (host == null) - host = ""; - authority = (port == -1) ? host : host + ":" + port; - - // Handle hosts with userInfo in them - int at = host.lastIndexOf('@'); - if (at != -1) { - userInfo = host.substring(0, at); - host = host.substring(at+1); - } - } else if (authority != null) { - // Construct user info part - int ind = authority.indexOf('@'); - if (ind != -1) - userInfo = authority.substring(0, ind); - } - - // Construct path and query part - path = null; - query = null; - if (file != null) { - // Fix: only do this if hierarchical? - int q = file.lastIndexOf('?'); - if (q != -1) { - query = file.substring(q+1); - path = file.substring(0, q); - } else - path = file; - } - } } class Parts {