8129880: Cleanup usage of Class.getResource in jaxp
authordfuchs
Thu, 25 Jun 2015 20:06:37 +0200
changeset 31287 d6581a7c221f
parent 31286 4c6b605ceb9f
child 31288 204ed502eb8a
8129880: Cleanup usage of Class.getResource in jaxp Reviewed-by: joehw, mchung
jaxp/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncSystemProperty.java
jaxp/src/java.xml/share/classes/javax/xml/validation/SchemaFactoryFinder.java
jaxp/src/java.xml/share/classes/javax/xml/validation/SecuritySupport.java
jaxp/src/java.xml/share/classes/javax/xml/xpath/SecuritySupport.java
jaxp/src/java.xml/share/classes/javax/xml/xpath/XPathFactoryFinder.java
--- a/jaxp/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncSystemProperty.java	Thu Jun 25 11:06:28 2015 +0200
+++ b/jaxp/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncSystemProperty.java	Thu Jun 25 20:06:37 2015 +0200
@@ -157,7 +157,7 @@
   }
 
   /**
-   * Retrieve a property bundle from a XSLT_PROPERTIES
+   * Retrieve a property bundle from XSLT_PROPERTIES
    *
    * @param target The target property bag the file will be placed into.
    */
--- a/jaxp/src/java.xml/share/classes/javax/xml/validation/SchemaFactoryFinder.java	Thu Jun 25 11:06:28 2015 +0200
+++ b/jaxp/src/java.xml/share/classes/javax/xml/validation/SchemaFactoryFinder.java	Thu Jun 25 20:06:37 2015 +0200
@@ -418,30 +418,8 @@
     private static final Class<SchemaFactory> SERVICE_CLASS = SchemaFactory.class;
 
 
+    // Used for debugging purposes
     private static String which( Class<?> clazz ) {
-        return which( clazz.getName(), clazz.getClassLoader() );
-    }
-
-    /**
-     * <p>Search the specified classloader for the given classname.</p>
-     *
-     * @param classname the fully qualified name of the class to search for
-     * @param loader the classloader to search
-     *
-     * @return the source location of the resource, or null if it wasn't found
-     */
-    private static String which(String classname, ClassLoader loader) {
-
-        String classnameAsResource = classname.replace('.', '/') + ".class";
-
-        if( loader==null )  loader = ClassLoader.getSystemClassLoader();
-
-        //URL it = loader.getResource(classnameAsResource);
-        URL it = ss.getResourceAsURL(loader, classnameAsResource);
-        if (it != null) {
-            return it.toString();
-        } else {
-            return null;
-        }
+        return ss.getClassSource(clazz);
     }
 }
--- a/jaxp/src/java.xml/share/classes/javax/xml/validation/SecuritySupport.java	Thu Jun 25 11:06:28 2015 +0200
+++ b/jaxp/src/java.xml/share/classes/javax/xml/validation/SecuritySupport.java	Thu Jun 25 20:06:37 2015 +0200
@@ -95,46 +95,22 @@
         }
     }
 
-    URL getResourceAsURL(final ClassLoader cl,
-                                           final String name)
-    {
-        return (URL)
-            AccessController.doPrivileged(new PrivilegedAction() {
-                public Object run() {
-                    URL url;
-                    if (cl == null) {
-                        url = Object.class.getResource(name);
-                    } else {
-                        url = cl.getResource(name);
-                    }
-                    return url;
-                }
-            });
-    }
-
-    Enumeration getResources(final ClassLoader cl,
-                                           final String name) throws IOException
-    {
-        try{
-        return (Enumeration)
-            AccessController.doPrivileged(new PrivilegedExceptionAction() {
-                public Object run() throws IOException{
-                    Enumeration enumeration;
-                    if (cl == null) {
-                        enumeration = ClassLoader.getSystemResources(name);
-                    } else {
-                        enumeration = cl.getResources(name);
-                    }
-                    return enumeration;
-                }
-            });
-        }catch(PrivilegedActionException e){
-            throw (IOException)e.getException();
-        }
+    // Used for debugging purposes
+    String getClassSource(Class<?> cls) {
+        return AccessController.doPrivileged(new PrivilegedAction<String>() {
+            public String run() {
+               CodeSource cs = cls.getProtectionDomain().getCodeSource();
+               if (cs != null) {
+                    return cs.getLocation().toString();
+               } else {
+                   return "(no code source)";
+               }
+            }
+        });
     }
 
     boolean doesFileExist(final File f) {
-    return ((Boolean)
+        return ((Boolean)
             AccessController.doPrivileged(new PrivilegedAction() {
                 public Object run() {
                     return new Boolean(f.exists());
--- a/jaxp/src/java.xml/share/classes/javax/xml/xpath/SecuritySupport.java	Thu Jun 25 11:06:28 2015 +0200
+++ b/jaxp/src/java.xml/share/classes/javax/xml/xpath/SecuritySupport.java	Thu Jun 25 20:06:37 2015 +0200
@@ -92,46 +92,22 @@
         }
     }
 
-    URL getResourceAsURL(final ClassLoader cl,
-                                           final String name)
-    {
-        return (URL)
-            AccessController.doPrivileged(new PrivilegedAction() {
-                public Object run() {
-                    URL url;
-                    if (cl == null) {
-                        url = Object.class.getResource(name);
-                    } else {
-                        url = cl.getResource(name);
-                    }
-                    return url;
-                }
-            });
-    }
-
-    Enumeration getResources(final ClassLoader cl,
-                                           final String name) throws IOException
-    {
-        try{
-        return (Enumeration)
-            AccessController.doPrivileged(new PrivilegedExceptionAction() {
-                public Object run() throws IOException{
-                    Enumeration enumeration;
-                    if (cl == null) {
-                        enumeration = ClassLoader.getSystemResources(name);
-                    } else {
-                        enumeration = cl.getResources(name);
-                    }
-                    return enumeration;
-                }
-            });
-        }catch(PrivilegedActionException e){
-            throw (IOException)e.getException();
-        }
+    // Used for debugging purposes
+    String getClassSource(Class<?> cls) {
+        return AccessController.doPrivileged(new PrivilegedAction<String>() {
+            public String run() {
+               CodeSource cs = cls.getProtectionDomain().getCodeSource();
+               if (cs != null) {
+                    return cs.getLocation().toString();
+               } else {
+                   return "(no code source)";
+               }
+            }
+        });
     }
 
     boolean doesFileExist(final File f) {
-    return ((Boolean)
+        return ((Boolean)
             AccessController.doPrivileged(new PrivilegedAction() {
                 public Object run() {
                     return new Boolean(f.exists());
--- a/jaxp/src/java.xml/share/classes/javax/xml/xpath/XPathFactoryFinder.java	Thu Jun 25 11:06:28 2015 +0200
+++ b/jaxp/src/java.xml/share/classes/javax/xml/xpath/XPathFactoryFinder.java	Thu Jun 25 20:06:37 2015 +0200
@@ -414,30 +414,9 @@
 
     private static final Class<XPathFactory> SERVICE_CLASS = XPathFactory.class;
 
-    private static String which( Class clazz ) {
-        return which( clazz.getName(), clazz.getClassLoader() );
+    // Used for debugging purposes
+    private static String which( Class<?> clazz ) {
+        return ss.getClassSource(clazz);
     }
 
-    /**
-     * <p>Search the specified classloader for the given classname.</p>
-     *
-     * @param classname the fully qualified name of the class to search for
-     * @param loader the classloader to search
-     *
-     * @return the source location of the resource, or null if it wasn't found
-     */
-    private static String which(String classname, ClassLoader loader) {
-
-        String classnameAsResource = classname.replace('.', '/') + ".class";
-
-        if( loader==null )  loader = ClassLoader.getSystemClassLoader();
-
-        //URL it = loader.getResource(classnameAsResource);
-        URL it = ss.getResourceAsURL(loader, classnameAsResource);
-        if (it != null) {
-            return it.toString();
-        } else {
-            return null;
-        }
-    }
 }