< prev index next >

src/java.base/share/classes/java/net/URL.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.net.spi.URLStreamHandlerProvider;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Hashtable;
  34 import java.io.InvalidObjectException;
  35 import java.io.ObjectStreamException;
  36 import java.io.ObjectStreamField;
  37 import java.io.ObjectInputStream.GetField;
  38 import java.util.Iterator;
  39 import java.util.Locale;
  40 import java.util.NoSuchElementException;
  41 import java.util.ServiceConfigurationError;
  42 import java.util.ServiceLoader;
  43 
  44 import sun.security.util.SecurityConstants;

  45 
  46 /**
  47  * Class {@code URL} represents a Uniform Resource
  48  * Locator, a pointer to a "resource" on the World
  49  * Wide Web. A resource can be something as simple as a file or a
  50  * directory, or it can be a reference to a more complicated object,
  51  * such as a query to a database or to a search engine. More
  52  * information on the types of URLs and their formats can be found at:
  53  * <a href=
  54  * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
  55  * <i>Types of URL</i></a>
  56  * <p>
  57  * In general, a URL can be broken into several parts. Consider the
  58  * following example:
  59  * <blockquote><pre>
  60  *     http://www.example.com/docs/resource1.html
  61  * </pre></blockquote>
  62  * <p>
  63  * The URL above indicates that the protocol to use is
  64  * {@code http} (HyperText Transfer Protocol) and that the


1193 
1194     private static class DefaultFactory implements URLStreamHandlerFactory {
1195         private static String PREFIX = "sun.net.www.protocol";
1196 
1197         public URLStreamHandler createURLStreamHandler(String protocol) {
1198             String name = PREFIX + "." + protocol + ".Handler";
1199             try {
1200                 Class<?> c = Class.forName(name);
1201                 return (URLStreamHandler)c.newInstance();
1202             } catch (ClassNotFoundException x) {
1203                 // ignore
1204             } catch (Exception e) {
1205                 // For compatibility, all Exceptions are ignored.
1206                 // any number of exceptions can get thrown here
1207             }
1208             return null;
1209         }
1210     }
1211 
1212     private static URLStreamHandler lookupViaProperty(String protocol) {
1213         String packagePrefixList = java.security.AccessController.doPrivileged(
1214                 new PrivilegedAction<>() {
1215                     public String run() {
1216                         return System.getProperty(protocolPathProp, null);
1217                     }
1218                 });
1219         if (packagePrefixList == null) {
1220             // not set
1221             return null;
1222         }
1223 
1224         String[] packagePrefixes = packagePrefixList.split("\\|");
1225         URLStreamHandler handler = null;
1226         for (int i=0; handler == null && i<packagePrefixes.length; i++) {
1227             String packagePrefix = packagePrefixes[i].trim();
1228             try {
1229                 String clsName = packagePrefix + "." + protocol + ".Handler";
1230                 Class<?> cls = null;
1231                 try {
1232                     cls = Class.forName(clsName);
1233                 } catch (ClassNotFoundException e) {
1234                     ClassLoader cl = ClassLoader.getSystemClassLoader();
1235                     if (cl != null) {
1236                         cls = cl.loadClass(clsName);
1237                     }
1238                 }




  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.net.spi.URLStreamHandlerProvider;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Hashtable;
  34 import java.io.InvalidObjectException;
  35 import java.io.ObjectStreamException;
  36 import java.io.ObjectStreamField;
  37 import java.io.ObjectInputStream.GetField;
  38 import java.util.Iterator;
  39 import java.util.Locale;
  40 import java.util.NoSuchElementException;
  41 import java.util.ServiceConfigurationError;
  42 import java.util.ServiceLoader;
  43 
  44 import sun.security.util.SecurityConstants;
  45 import sun.security.action.GetPropertyAction;
  46 
  47 /**
  48  * Class {@code URL} represents a Uniform Resource
  49  * Locator, a pointer to a "resource" on the World
  50  * Wide Web. A resource can be something as simple as a file or a
  51  * directory, or it can be a reference to a more complicated object,
  52  * such as a query to a database or to a search engine. More
  53  * information on the types of URLs and their formats can be found at:
  54  * <a href=
  55  * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
  56  * <i>Types of URL</i></a>
  57  * <p>
  58  * In general, a URL can be broken into several parts. Consider the
  59  * following example:
  60  * <blockquote><pre>
  61  *     http://www.example.com/docs/resource1.html
  62  * </pre></blockquote>
  63  * <p>
  64  * The URL above indicates that the protocol to use is
  65  * {@code http} (HyperText Transfer Protocol) and that the


1194 
1195     private static class DefaultFactory implements URLStreamHandlerFactory {
1196         private static String PREFIX = "sun.net.www.protocol";
1197 
1198         public URLStreamHandler createURLStreamHandler(String protocol) {
1199             String name = PREFIX + "." + protocol + ".Handler";
1200             try {
1201                 Class<?> c = Class.forName(name);
1202                 return (URLStreamHandler)c.newInstance();
1203             } catch (ClassNotFoundException x) {
1204                 // ignore
1205             } catch (Exception e) {
1206                 // For compatibility, all Exceptions are ignored.
1207                 // any number of exceptions can get thrown here
1208             }
1209             return null;
1210         }
1211     }
1212 
1213     private static URLStreamHandler lookupViaProperty(String protocol) {
1214         String packagePrefixList =
1215                 GetPropertyAction.getProperty(protocolPathProp);




1216         if (packagePrefixList == null) {
1217             // not set
1218             return null;
1219         }
1220 
1221         String[] packagePrefixes = packagePrefixList.split("\\|");
1222         URLStreamHandler handler = null;
1223         for (int i=0; handler == null && i<packagePrefixes.length; i++) {
1224             String packagePrefix = packagePrefixes[i].trim();
1225             try {
1226                 String clsName = packagePrefix + "." + protocol + ".Handler";
1227                 Class<?> cls = null;
1228                 try {
1229                     cls = Class.forName(clsName);
1230                 } catch (ClassNotFoundException e) {
1231                     ClassLoader cl = ClassLoader.getSystemClassLoader();
1232                     if (cl != null) {
1233                         cls = cl.loadClass(clsName);
1234                     }
1235                 }


< prev index next >