1.1 --- a/emul/src/main/java/java/net/URL.java Tue Oct 30 09:05:33 2012 +0100
1.2 +++ b/emul/src/main/java/java/net/URL.java Tue Oct 30 09:24:41 2012 +0100
1.3 @@ -27,10 +27,6 @@
1.4
1.5 import java.io.IOException;
1.6 import java.io.InputStream;
1.7 -import java.io.OutputStream;
1.8 -import java.util.Hashtable;
1.9 -import java.util.StringTokenizer;
1.10 -import sun.security.util.SecurityConstants;
1.11
1.12 /**
1.13 * Class <code>URL</code> represents a Uniform Resource
1.14 @@ -200,17 +196,6 @@
1.15 */
1.16 private String ref;
1.17
1.18 - /**
1.19 - * The host's IP address, used in equals and hashCode.
1.20 - * Computed on demand. An uninitialized or unknown hostAddress is null.
1.21 - */
1.22 - transient InetAddress hostAddress;
1.23 -
1.24 - /**
1.25 - * The URLStreamHandler for this URL.
1.26 - */
1.27 - transient URLStreamHandler handler;
1.28 -
1.29 /* Our hash code.
1.30 * @serial
1.31 */
1.32 @@ -323,53 +308,10 @@
1.33 this(protocol, host, -1, file);
1.34 }
1.35
1.36 - /**
1.37 - * Creates a <code>URL</code> object from the specified
1.38 - * <code>protocol</code>, <code>host</code>, <code>port</code>
1.39 - * number, <code>file</code>, and <code>handler</code>. Specifying
1.40 - * a <code>port</code> number of <code>-1</code> indicates that
1.41 - * the URL should use the default port for the protocol. Specifying
1.42 - * a <code>handler</code> of <code>null</code> indicates that the URL
1.43 - * should use a default stream handler for the protocol, as outlined
1.44 - * for:
1.45 - * java.net.URL#URL(java.lang.String, java.lang.String, int,
1.46 - * java.lang.String)
1.47 - *
1.48 - * <p>If the handler is not null and there is a security manager,
1.49 - * the security manager's <code>checkPermission</code>
1.50 - * method is called with a
1.51 - * <code>NetPermission("specifyStreamHandler")</code> permission.
1.52 - * This may result in a SecurityException.
1.53 - *
1.54 - * No validation of the inputs is performed by this constructor.
1.55 - *
1.56 - * @param protocol the name of the protocol to use.
1.57 - * @param host the name of the host.
1.58 - * @param port the port number on the host.
1.59 - * @param file the file on the host
1.60 - * @param handler the stream handler for the URL.
1.61 - * @exception MalformedURLException if an unknown protocol is specified.
1.62 - * @exception SecurityException
1.63 - * if a security manager exists and its
1.64 - * <code>checkPermission</code> method doesn't allow
1.65 - * specifying a stream handler explicitly.
1.66 - * @see java.lang.System#getProperty(java.lang.String)
1.67 - * @see java.net.URL#setURLStreamHandlerFactory(
1.68 - * java.net.URLStreamHandlerFactory)
1.69 - * @see java.net.URLStreamHandler
1.70 - * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
1.71 - * java.lang.String)
1.72 - * @see SecurityManager#checkPermission
1.73 - * @see java.net.NetPermission
1.74 - */
1.75 - public URL(String protocol, String host, int port, String file,
1.76 - URLStreamHandler handler) throws MalformedURLException {
1.77 + private URL(String protocol, String host, int port, String file,
1.78 + Object handler) throws MalformedURLException {
1.79 if (handler != null) {
1.80 - SecurityManager sm = System.getSecurityManager();
1.81 - if (sm != null) {
1.82 - // check for permission to specify a handler
1.83 - checkSpecifyHandler(sm);
1.84 - }
1.85 + throw new SecurityException();
1.86 }
1.87
1.88 protocol = protocol.toLowerCase();
1.89 @@ -406,11 +348,10 @@
1.90
1.91 // Note: we don't do validation of the URL here. Too risky to change
1.92 // right now, but worth considering for future reference. -br
1.93 - if (handler == null &&
1.94 - (handler = getURLStreamHandler(protocol)) == null) {
1.95 - throw new MalformedURLException("unknown protocol: " + protocol);
1.96 - }
1.97 - this.handler = handler;
1.98 +// if (handler == null &&
1.99 +// (handler = getURLStreamHandler(protocol)) == null) {
1.100 +// throw new MalformedURLException("unknown protocol: " + protocol);
1.101 +// }
1.102 }
1.103
1.104 /**
1.105 @@ -500,7 +441,7 @@
1.106 * @see java.net.URLStreamHandler#parseURL(java.net.URL,
1.107 * java.lang.String, int, int)
1.108 */
1.109 - public URL(URL context, String spec, URLStreamHandler handler)
1.110 + private URL(URL context, String spec, Object handler)
1.111 throws MalformedURLException
1.112 {
1.113 String original = spec;
1.114 @@ -512,10 +453,7 @@
1.115
1.116 // Check for permission to specify a handler
1.117 if (handler != null) {
1.118 - SecurityManager sm = System.getSecurityManager();
1.119 - if (sm != null) {
1.120 - checkSpecifyHandler(sm);
1.121 - }
1.122 + throw new SecurityException();
1.123 }
1.124
1.125 try {
1.126 @@ -556,9 +494,9 @@
1.127 newProtocol.equalsIgnoreCase(context.protocol))) {
1.128 // inherit the protocol handler from the context
1.129 // if not specified to the constructor
1.130 - if (handler == null) {
1.131 - handler = context.handler;
1.132 - }
1.133 +// if (handler == null) {
1.134 +// handler = context.handler;
1.135 +// }
1.136
1.137 // If the context is a hierarchical URL scheme and the spec
1.138 // contains a matching scheme then maintain backwards
1.139 @@ -585,12 +523,12 @@
1.140
1.141 // Get the protocol handler if not specified or the protocol
1.142 // of the context could not be used
1.143 - if (handler == null &&
1.144 - (handler = getURLStreamHandler(protocol)) == null) {
1.145 - throw new MalformedURLException("unknown protocol: "+protocol);
1.146 - }
1.147 +// if (handler == null &&
1.148 +// (handler = getURLStreamHandler(protocol)) == null) {
1.149 +// throw new MalformedURLException("unknown protocol: "+protocol);
1.150 +// }
1.151
1.152 - this.handler = handler;
1.153 +// this.handler = handler;
1.154
1.155 i = spec.indexOf('#', start);
1.156 if (i >= 0) {
1.157 @@ -609,7 +547,7 @@
1.158 }
1.159 }
1.160
1.161 - handler.parseURL(this, spec, start, limit);
1.162 +// handler.parseURL(this, spec, start, limit);
1.163
1.164 } catch(MalformedURLException e) {
1.165 throw e;
1.166 @@ -640,13 +578,6 @@
1.167 return true;
1.168 }
1.169
1.170 - /*
1.171 - * Checks for permission to specify a stream handler.
1.172 - */
1.173 - private void checkSpecifyHandler(SecurityManager sm) {
1.174 - sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION);
1.175 - }
1.176 -
1.177 /**
1.178 * Sets the fields of the URL. This is not a public method so that
1.179 * only URLStreamHandlers can modify URL fields. URLs are
1.180 @@ -670,7 +601,6 @@
1.181 /* This is very important. We must recompute this after the
1.182 * URL has been changed. */
1.183 hashCode = -1;
1.184 - hostAddress = null;
1.185 int q = file.lastIndexOf('?');
1.186 if (q != -1) {
1.187 query = file.substring(q+1);
1.188 @@ -709,7 +639,6 @@
1.189 /* This is very important. We must recompute this after the
1.190 * URL has been changed. */
1.191 hashCode = -1;
1.192 - hostAddress = null;
1.193 this.query = query;
1.194 this.authority = authority;
1.195 }
1.196 @@ -768,19 +697,6 @@
1.197 }
1.198
1.199 /**
1.200 - * Gets the default port number of the protocol associated
1.201 - * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
1.202 - * for the URL do not define a default port number,
1.203 - * then -1 is returned.
1.204 - *
1.205 - * @return the port number
1.206 - * @since 1.4
1.207 - */
1.208 - public int getDefaultPort() {
1.209 - return handler.getDefaultPort();
1.210 - }
1.211 -
1.212 - /**
1.213 * Gets the protocol name of this <code>URL</code>.
1.214 *
1.215 * @return the protocol of this <code>URL</code>.
1.216 @@ -857,7 +773,8 @@
1.217 return false;
1.218 URL u2 = (URL)obj;
1.219
1.220 - return handler.equals(this, u2);
1.221 + // return handler.equals(this, u2);
1.222 + return u2 == this;
1.223 }
1.224
1.225 /**
1.226 @@ -872,7 +789,7 @@
1.227 if (hashCode != -1)
1.228 return hashCode;
1.229
1.230 - hashCode = handler.hashCode(this);
1.231 + // hashCode = handler.hashCode(this);
1.232 return hashCode;
1.233 }
1.234
1.235 @@ -888,7 +805,8 @@
1.236 * <code>false</code> otherwise.
1.237 */
1.238 public boolean sameFile(URL other) {
1.239 - return handler.sameFile(this, other);
1.240 +// return handler.sameFile(this, other);
1.241 + throw new UnsupportedOperationException();
1.242 }
1.243
1.244 /**
1.245 @@ -916,24 +834,8 @@
1.246 * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
1.247 */
1.248 public String toExternalForm() {
1.249 - return handler.toExternalForm(this);
1.250 - }
1.251 -
1.252 - /**
1.253 - * Returns a {@link java.net.URI} equivalent to this URL.
1.254 - * This method functions in the same way as <code>new URI (this.toString())</code>.
1.255 - * <p>Note, any URL instance that complies with RFC 2396 can be converted
1.256 - * to a URI. However, some URLs that are not strictly in compliance
1.257 - * can not be converted to a URI.
1.258 - *
1.259 - * @exception URISyntaxException if this URL is not formatted strictly according to
1.260 - * to RFC2396 and cannot be converted to a URI.
1.261 - *
1.262 - * @return a URI instance equivalent to this URL.
1.263 - * @since 1.5
1.264 - */
1.265 - public URI toURI() throws URISyntaxException {
1.266 - return new URI (toString());
1.267 + throw new UnsupportedOperationException();
1.268 +// return handler.toExternalForm(this);
1.269 }
1.270
1.271 /**
1.272 @@ -965,58 +867,10 @@
1.273 * @see java.net.URL#URL(java.lang.String, java.lang.String,
1.274 * int, java.lang.String)
1.275 */
1.276 - public URLConnection openConnection() throws java.io.IOException {
1.277 - return handler.openConnection(this);
1.278 - }
1.279 +// public URLConnection openConnection() throws java.io.IOException {
1.280 +// return handler.openConnection(this);
1.281 +// }
1.282
1.283 - /**
1.284 - * Same as {@link #openConnection()}, except that the connection will be
1.285 - * made through the specified proxy; Protocol handlers that do not
1.286 - * support proxing will ignore the proxy parameter and make a
1.287 - * normal connection.
1.288 - *
1.289 - * Invoking this method preempts the system's default ProxySelector
1.290 - * settings.
1.291 - *
1.292 - * @param proxy the Proxy through which this connection
1.293 - * will be made. If direct connection is desired,
1.294 - * Proxy.NO_PROXY should be specified.
1.295 - * @return a <code>URLConnection</code> to the URL.
1.296 - * @exception IOException if an I/O exception occurs.
1.297 - * @exception SecurityException if a security manager is present
1.298 - * and the caller doesn't have permission to connect
1.299 - * to the proxy.
1.300 - * @exception IllegalArgumentException will be thrown if proxy is null,
1.301 - * or proxy has the wrong type
1.302 - * @exception UnsupportedOperationException if the subclass that
1.303 - * implements the protocol handler doesn't support
1.304 - * this method.
1.305 - * @see java.net.URL#URL(java.lang.String, java.lang.String,
1.306 - * int, java.lang.String)
1.307 - * @see java.net.URLConnection
1.308 - * @see java.net.URLStreamHandler#openConnection(java.net.URL,
1.309 - * java.net.Proxy)
1.310 - * @since 1.5
1.311 - */
1.312 - public URLConnection openConnection(Proxy proxy)
1.313 - throws java.io.IOException {
1.314 - if (proxy == null) {
1.315 - throw new IllegalArgumentException("proxy can not be null");
1.316 - }
1.317 -
1.318 - // Create a copy of Proxy as a security measure
1.319 - Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy);
1.320 - SecurityManager sm = System.getSecurityManager();
1.321 - if (p.type() != Proxy.Type.DIRECT && sm != null) {
1.322 - InetSocketAddress epoint = (InetSocketAddress) p.address();
1.323 - if (epoint.isUnresolved())
1.324 - sm.checkConnect(epoint.getHostName(), epoint.getPort());
1.325 - else
1.326 - sm.checkConnect(epoint.getAddress().getHostAddress(),
1.327 - epoint.getPort());
1.328 - }
1.329 - return handler.openConnection(this, p);
1.330 - }
1.331
1.332 /**
1.333 * Opens a connection to this <code>URL</code> and returns an
1.334 @@ -1032,7 +886,8 @@
1.335 * @see java.net.URLConnection#getInputStream()
1.336 */
1.337 public final InputStream openStream() throws java.io.IOException {
1.338 - return openConnection().getInputStream();
1.339 + throw new IOException();
1.340 +// return openConnection().getInputStream();
1.341 }
1.342
1.343 /**
1.344 @@ -1046,7 +901,8 @@
1.345 * @see java.net.URLConnection#getContent()
1.346 */
1.347 public final Object getContent() throws java.io.IOException {
1.348 - return openConnection().getContent();
1.349 + throw new IOException();
1.350 +// return openConnection().getContent();
1.351 }
1.352
1.353 /**
1.354 @@ -1065,219 +921,11 @@
1.355 */
1.356 public final Object getContent(Class[] classes)
1.357 throws java.io.IOException {
1.358 - return openConnection().getContent(classes);
1.359 + throw new IOException();
1.360 +// return openConnection().getContent(classes);
1.361 }
1.362
1.363 - /**
1.364 - * The URLStreamHandler factory.
1.365 - */
1.366 - static URLStreamHandlerFactory factory;
1.367
1.368 - /**
1.369 - * Sets an application's <code>URLStreamHandlerFactory</code>.
1.370 - * This method can be called at most once in a given Java Virtual
1.371 - * Machine.
1.372 - *
1.373 - *<p> The <code>URLStreamHandlerFactory</code> instance is used to
1.374 - *construct a stream protocol handler from a protocol name.
1.375 - *
1.376 - * <p> If there is a security manager, this method first calls
1.377 - * the security manager's <code>checkSetFactory</code> method
1.378 - * to ensure the operation is allowed.
1.379 - * This could result in a SecurityException.
1.380 - *
1.381 - * @param fac the desired factory.
1.382 - * @exception Error if the application has already set a factory.
1.383 - * @exception SecurityException if a security manager exists and its
1.384 - * <code>checkSetFactory</code> method doesn't allow
1.385 - * the operation.
1.386 - * @see java.net.URL#URL(java.lang.String, java.lang.String,
1.387 - * int, java.lang.String)
1.388 - * @see java.net.URLStreamHandlerFactory
1.389 - * @see SecurityManager#checkSetFactory
1.390 - */
1.391 - public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
1.392 - synchronized (streamHandlerLock) {
1.393 - if (factory != null) {
1.394 - throw new Error("factory already defined");
1.395 - }
1.396 - SecurityManager security = System.getSecurityManager();
1.397 - if (security != null) {
1.398 - security.checkSetFactory();
1.399 - }
1.400 - handlers.clear();
1.401 - factory = fac;
1.402 - }
1.403 - }
1.404 -
1.405 - /**
1.406 - * A table of protocol handlers.
1.407 - */
1.408 - static Hashtable handlers = new Hashtable();
1.409 - private static Object streamHandlerLock = new Object();
1.410 -
1.411 - /**
1.412 - * Returns the Stream Handler.
1.413 - * @param protocol the protocol to use
1.414 - */
1.415 - static URLStreamHandler getURLStreamHandler(String protocol) {
1.416 -
1.417 - URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);
1.418 - if (handler == null) {
1.419 -
1.420 - boolean checkedWithFactory = false;
1.421 -
1.422 - // Use the factory (if any)
1.423 - if (factory != null) {
1.424 - handler = factory.createURLStreamHandler(protocol);
1.425 - checkedWithFactory = true;
1.426 - }
1.427 -
1.428 - // Try java protocol handler
1.429 - if (handler == null) {
1.430 - String packagePrefixList = null;
1.431 -
1.432 - packagePrefixList
1.433 - = java.security.AccessController.doPrivileged(
1.434 - new sun.security.action.GetPropertyAction(
1.435 - protocolPathProp,""));
1.436 - if (packagePrefixList != "") {
1.437 - packagePrefixList += "|";
1.438 - }
1.439 -
1.440 - // REMIND: decide whether to allow the "null" class prefix
1.441 - // or not.
1.442 - packagePrefixList += "sun.net.www.protocol";
1.443 -
1.444 - StringTokenizer packagePrefixIter =
1.445 - new StringTokenizer(packagePrefixList, "|");
1.446 -
1.447 - while (handler == null &&
1.448 - packagePrefixIter.hasMoreTokens()) {
1.449 -
1.450 - String packagePrefix =
1.451 - packagePrefixIter.nextToken().trim();
1.452 - try {
1.453 - String clsName = packagePrefix + "." + protocol +
1.454 - ".Handler";
1.455 - Class cls = null;
1.456 - try {
1.457 - cls = Class.forName(clsName);
1.458 - } catch (ClassNotFoundException e) {
1.459 - ClassLoader cl = ClassLoader.getSystemClassLoader();
1.460 - if (cl != null) {
1.461 - cls = cl.loadClass(clsName);
1.462 - }
1.463 - }
1.464 - if (cls != null) {
1.465 - handler =
1.466 - (URLStreamHandler)cls.newInstance();
1.467 - }
1.468 - } catch (Exception e) {
1.469 - // any number of exceptions can get thrown here
1.470 - }
1.471 - }
1.472 - }
1.473 -
1.474 - synchronized (streamHandlerLock) {
1.475 -
1.476 - URLStreamHandler handler2 = null;
1.477 -
1.478 - // Check again with hashtable just in case another
1.479 - // thread created a handler since we last checked
1.480 - handler2 = (URLStreamHandler)handlers.get(protocol);
1.481 -
1.482 - if (handler2 != null) {
1.483 - return handler2;
1.484 - }
1.485 -
1.486 - // Check with factory if another thread set a
1.487 - // factory since our last check
1.488 - if (!checkedWithFactory && factory != null) {
1.489 - handler2 = factory.createURLStreamHandler(protocol);
1.490 - }
1.491 -
1.492 - if (handler2 != null) {
1.493 - // The handler from the factory must be given more
1.494 - // importance. Discard the default handler that
1.495 - // this thread created.
1.496 - handler = handler2;
1.497 - }
1.498 -
1.499 - // Insert this handler into the hashtable
1.500 - if (handler != null) {
1.501 - handlers.put(protocol, handler);
1.502 - }
1.503 -
1.504 - }
1.505 - }
1.506 -
1.507 - return handler;
1.508 -
1.509 - }
1.510 -
1.511 - /**
1.512 - * WriteObject is called to save the state of the URL to an
1.513 - * ObjectOutputStream. The handler is not saved since it is
1.514 - * specific to this system.
1.515 - *
1.516 - * @serialData the default write object value. When read back in,
1.517 - * the reader must ensure that calling getURLStreamHandler with
1.518 - * the protocol variable returns a valid URLStreamHandler and
1.519 - * throw an IOException if it does not.
1.520 - */
1.521 - private synchronized void writeObject(java.io.ObjectOutputStream s)
1.522 - throws IOException
1.523 - {
1.524 - s.defaultWriteObject(); // write the fields
1.525 - }
1.526 -
1.527 - /**
1.528 - * readObject is called to restore the state of the URL from the
1.529 - * stream. It reads the components of the URL and finds the local
1.530 - * stream handler.
1.531 - */
1.532 - private synchronized void readObject(java.io.ObjectInputStream s)
1.533 - throws IOException, ClassNotFoundException
1.534 - {
1.535 - s.defaultReadObject(); // read the fields
1.536 - if ((handler = getURLStreamHandler(protocol)) == null) {
1.537 - throw new IOException("unknown protocol: " + protocol);
1.538 - }
1.539 -
1.540 - // Construct authority part
1.541 - if (authority == null &&
1.542 - ((host != null && host.length() > 0) || port != -1)) {
1.543 - if (host == null)
1.544 - host = "";
1.545 - authority = (port == -1) ? host : host + ":" + port;
1.546 -
1.547 - // Handle hosts with userInfo in them
1.548 - int at = host.lastIndexOf('@');
1.549 - if (at != -1) {
1.550 - userInfo = host.substring(0, at);
1.551 - host = host.substring(at+1);
1.552 - }
1.553 - } else if (authority != null) {
1.554 - // Construct user info part
1.555 - int ind = authority.indexOf('@');
1.556 - if (ind != -1)
1.557 - userInfo = authority.substring(0, ind);
1.558 - }
1.559 -
1.560 - // Construct path and query part
1.561 - path = null;
1.562 - query = null;
1.563 - if (file != null) {
1.564 - // Fix: only do this if hierarchical?
1.565 - int q = file.lastIndexOf('?');
1.566 - if (q != -1) {
1.567 - query = file.substring(q+1);
1.568 - path = file.substring(0, q);
1.569 - } else
1.570 - path = file;
1.571 - }
1.572 - }
1.573 }
1.574
1.575 class Parts {