db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r483653 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/services/io/ engine/org/apache/derby/impl/services/reflect/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Thu, 07 Dec 2006 20:41:12 GMT
Author: djd
Date: Thu Dec  7 12:41:11 2006
New Revision: 483653

URL: http://svn.apache.org/viewvc?view=rev&rev=483653
Log:
DERBY-552 Fix fetching resources from an installed jar when the database itself is a jar file.
The old code did not handle the case when the length of the resource (jar) entry was unknown.
New code reads the resource from the JarInputStream into a local byte array and returns a
stream based upon that to the application. This also provides better isolation between
the application and derby, denying user code the ability to read the entire jar or
to prevent correct shutdown of the database. Added utilities to AccessibleByteArrayOutputStream
to enable copying an InputStream into a local array.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/AccessibleByteArrayOutputStream.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/AccessibleByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/AccessibleByteArrayOutputStream.java?view=diff&rev=483653&r1=483652&r2=483653
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/AccessibleByteArrayOutputStream.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/AccessibleByteArrayOutputStream.java
Thu Dec  7 12:41:11 2006
@@ -22,6 +22,8 @@
 package org.apache.derby.iapi.services.io;
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 
 /**
  * This allows us to get to the byte array to go back and
@@ -54,4 +56,56 @@
 	public byte[] getInternalByteArray() {
 		return buf;
 	}
+    
+    /**
+     * Read the complete contents of the passed input stream
+     * into this byte array.
+     * @throws IOException 
+     */
+    public void readFrom(InputStream in) throws IOException
+    {
+       byte[] buffer = new byte[8192];
+        
+        for(;;)
+        {
+            int read = in.read(buffer, 0, buf.length);
+            if (read == -1)
+                break;
+            write(buffer, 0, read);
+        }
+    }
+    
+    /**
+     * Return an InputStream that wraps the valid byte array.
+     * Note that no copy is made of the byte array from the
+     * input stream, it is up to the caller to ensure the correct
+     * co-ordination.
+     */
+    public InputStream getInputStream()
+    {
+        return new NewByteArrayInputStream(buf, 0, count);
+    }
+    
+    /**
+     * Copy an InputStream into an array of bytes and return
+     * an InputStream against those bytes. The input stream
+     * is copied until EOF is returned. This is useful to provide
+     * streams to applications in order to isolate them from
+     * Derby's internals.
+     * 
+     * @param in InputStream to be copied
+     * @param bufferSize Initial size of the byte array
+     * 
+     * @return InputStream against the raw data.
+     * 
+     * @throws IOException Error reading the stream
+     */
+    public static InputStream copyStream(InputStream in, int bufferSize)
+         throws IOException
+    {
+        AccessibleByteArrayOutputStream raw =
+            new AccessibleByteArrayOutputStream(bufferSize);
+        raw.readFrom(in);
+        return raw.getInputStream();
+    }
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java?view=diff&rev=483653&r1=483652&r2=483653
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
Thu Dec  7 12:41:11 2006
@@ -36,6 +36,7 @@
 import java.util.jar.JarFile;
 import java.util.jar.JarInputStream;
 
+import org.apache.derby.iapi.services.io.AccessibleByteArrayOutputStream;
 import org.apache.derby.iapi.services.io.InputStreamUtil;
 import org.apache.derby.iapi.services.io.LimitInputStream;
 import org.apache.derby.iapi.util.IdUtil;
@@ -347,9 +348,18 @@
 
 	/**
 		Get a stream from a zip file that is itself a stream.
-		Here we need to get the size of the zip entry and
-		put a limiting stream around it. Otherwise the
-		caller would end up reading the entire zip file!
+        We copy to the contents to a byte array and return a
+        stream around that to the caller. Though a copy is
+        involved it has the benefit of:
+        <UL>
+        <LI> Isolating the application from the JarInputStream, thus
+        denying any possibility of the application reading more of the
+        jar that it should be allowed to. E.g. the contents class files are not
+        exposed through getResource.
+        <LI> Avoids any possibility of the application holding onto
+        the open stream beyond shutdown of the database, thus leading
+        to leaked file descriptors or inability to remove the jar.
+        </UL>
 	*/
 	private InputStream getRawStream(InputStream in, String name) { 
 
@@ -361,22 +371,27 @@
 			while ((e = jarIn.getNextJarEntry()) != null) {
 
 				if (e.getName().equals(name)) {
-					LimitInputStream lis = new LimitInputStream(jarIn);
-					lis.setLimit((int) e.getSize());
-					return lis;
+                    int size = (int) e.getSize();
+                    if (size == -1)
+                    {
+                        // unknown size so just pick a good buffer size.
+                        size = 8192;
+                    }
+                    return AccessibleByteArrayOutputStream.copyStream(jarIn, size);
 				}
 			}
 
-			jarIn.close();
-
 		} catch (IOException ioe) {
-			if (jarIn != null) {
-				try {
-					jarIn.close();
-				} catch (IOException ioe2) {
-				}
-			}
+            // can't read the jar file just assume it doesn't exist.
 		}
+        finally {
+            if (jarIn != null) {
+                try {
+                    jarIn.close();
+                } catch (IOException ioe2) {
+                }
+            }            
+        }
 		return null;
 	}
     

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java?view=diff&rev=483653&r1=483652&r2=483653
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
Thu Dec  7 12:41:11 2006
@@ -662,8 +662,7 @@
             assertStatementError("25502", s,
                     "CALL EMC.ADDCONTACT(3, 'really@is_read_only.gov')");
 
-            // Disabled due to DERBY-552
-            // getResourceTests(conn);
+            getResourceTests(conn);
             
             // Disabled due to DERBY-553
             // signersTests(conn);



Mime
View raw message