db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1582754 - /db/derby/code/trunk/java/engine/org/apache/derby/impl/io/CPFile.java
Date Fri, 28 Mar 2014 14:02:13 GMT
Author: kahatlen
Date: Fri Mar 28 14:02:13 2014
New Revision: 1582754

URL: http://svn.apache.org/r1582754
Log:
DERBY-5615: Permission problems with classpath subsubprotocol

Change the structure of CPFile.getInputStream() back to what it was
before the original fix for this issue (revision 1582655), but with
doPrivileged() calls around all operations that require privileges.

The restructuring in the original fix apparently prevented some
resources from being freed, so that DatabaseClassLoadingTest and
NativeAuthenticationServiceTest failed on Windows platforms because
they could not delete the jar file that contained the classpath
database.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/io/CPFile.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/io/CPFile.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/io/CPFile.java?rev=1582754&r1=1582753&r2=1582754&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/io/CPFile.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/io/CPFile.java Fri Mar 28 14:02:13
2014
@@ -26,12 +26,9 @@ import org.apache.derby.io.StorageFile;
 import java.io.InputStream;
 
 import java.io.FileNotFoundException;
-import java.io.IOException;
 import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
 
 /**
  * This class provides a class path based implementation of the StorageFile interface. It
is used by the
@@ -95,22 +92,32 @@ class CPFile extends InputStreamFile
      */
     public InputStream getInputStream( ) throws FileNotFoundException
     {
-        URL url = getURL();
+        InputStream is = null;
+        ClassLoader cl = getContextClassLoader(Thread.currentThread());
+        if (cl != null) {
+            is = getResourceAsStream(cl, path);
+        }
 
-        if (url == null) {
-            throw new FileNotFoundException(toString());
+        // don't assume the context class loader is tied
+        // into the class loader that loaded this class.
+        if (is == null) {
+            cl = getClass().getClassLoader();
+            // Javadoc indicates implementations can use
+            // null as a return from Class.getClassLoader()
+            // to indicate the system/bootstrap classloader.
+            if (cl != null) {
+                is = getResourceAsStream(cl, path);
+            } else {
+                is = getSystemResourceAsStream(path);
+            }
         }
 
-        try {
-            return openStream(url);
-        } catch (FileNotFoundException fnf) {
-            throw fnf;
-        } catch (IOException ioe) {
-            FileNotFoundException fnf = new FileNotFoundException(toString());
-            fnf.initCause(ioe);
-            throw fnf;
+        if (is == null) {
+            throw new FileNotFoundException(toString());
         }
 
+        return is;
+
     } // end of getInputStream
     
 	/**
@@ -169,17 +176,29 @@ class CPFile extends InputStreamFile
         });
     }
 
-    /** Privileged wrapper for {@code URL.openStream()}. */
-    private static InputStream openStream(final URL url) throws IOException {
-        try {
-            return AccessController.doPrivileged(
-                    new PrivilegedExceptionAction<InputStream>() {
-                public InputStream run() throws IOException {
-                    return url.openStream();
-                }
-            });
-        } catch (PrivilegedActionException pae) {
-            throw (IOException) pae.getCause();
-        }
+    /**
+     * Privileged wrapper for {@code ClassLoader.getResourceAsStream(String)}.
+     */
+    private static InputStream getResourceAsStream(
+            final ClassLoader cl, final String name) {
+        return AccessController.doPrivileged(
+                new PrivilegedAction<InputStream>() {
+            public InputStream run() {
+                return cl.getResourceAsStream(name);
+            }
+        });
+    }
+
+    /**
+     * Privileged wrapper for
+     * {@code ClassLoader.getSystemResourceAsStream(String)}.
+     */
+    private static InputStream getSystemResourceAsStream(final String name) {
+        return AccessController.doPrivileged(
+                new PrivilegedAction<InputStream>() {
+            public InputStream run() {
+                return ClassLoader.getSystemResourceAsStream(name);
+            }
+        });
     }
 }



Mime
View raw message