You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2006/12/12 16:59:54 UTC

svn commit: r486201 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/services/loader/JarReader.java impl/db/BasicDatabase.java impl/services/reflect/JarLoader.java

Author: djd
Date: Tue Dec 12 07:59:52 2006
New Revision: 486201

URL: http://svn.apache.org/viewvc?view=rev&rev=486201
Log:
DERBY-2040 (partial) Change the api for jarReader to return the StorageFile rather
than the (potentially) opened jar. Removes the possibility for user code to
access the jar by calling methods of JarReader directly (and hence abusing any
privileged blocks that would be required while opening the jar). Cleans up JarLoader
by having explict calls to open the stream or file within the class loader.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/JarReader.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/db/BasicDatabase.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/JarReader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/JarReader.java?view=diff&rev=486201&r1=486200&r2=486201
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/JarReader.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/loader/JarReader.java Tue Dec 12 07:59:52 2006
@@ -22,6 +22,7 @@
 package org.apache.derby.iapi.services.loader;
 
 import org.apache.derby.iapi.error.StandardException;
+import org.apache.derby.io.StorageFile;
 
 /**
 	Abstract out the loading of JarFiles.
@@ -30,12 +31,9 @@
 public interface JarReader {
 
 	/**
-		Load the contents of a Jarfile. The return is either
-		an java.io.InputStream representing the contents of the JarFile
-		or a java.io.File representing the location of the file.
-		If the jar does not exist an exception is thrown.
+	 * Get the StorageFile for an installed jar file.
 	*/
-	Object readJarFile(String schemaName, String sqlName)
+	StorageFile getJarFile(String schemaName, String sqlName)
 		throws StandardException;
 }
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/db/BasicDatabase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/db/BasicDatabase.java?view=diff&rev=486201&r1=486200&r2=486201
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/db/BasicDatabase.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/db/BasicDatabase.java Tue Dec 12 07:59:52 2006
@@ -790,7 +790,7 @@
 	/*
 	** Methods of JarReader
 	*/
-	public Object readJarFile(String schemaName, String sqlName)
+	public StorageFile getJarFile(String schemaName, String sqlName)
 		throws StandardException {
 
 		DataDictionaryContext ddc =
@@ -808,15 +808,7 @@
 
 		String externalName = org.apache.derby.impl.sql.execute.JarDDL.mkExternalName(schemaName, sqlName, fr.getSeparatorChar());
 
-		StorageFile f = fr.getAsFile(externalName, generationId);
-		if (f instanceof java.io.File)
-			return f;
-
-		try {
-			return f.getInputStream();
-		} catch (java.io.IOException ioe) {
-            throw StandardException.newException(SQLState.LANG_FILE_ERROR, ioe, ioe.toString());    
-		}
+		return fr.getAsFile(externalName, generationId);
 	}
 
 }

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=486201&r1=486200&r2=486201
==============================================================================
--- 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 Tue Dec 12 07:59:52 2006
@@ -26,6 +26,7 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.IOException;
 
@@ -43,6 +44,7 @@
 
 import org.apache.derby.iapi.reference.MessageId;
 import org.apache.derby.iapi.services.i18n.MessageService;
+import org.apache.derby.io.StorageFile;
 
 
 class JarLoader extends ClassLoader {
@@ -53,6 +55,11 @@
     private final String[] name;
     
     /**
+     * Handle to the installed jar file.
+     */
+    private StorageFile installedJar;
+    
+    /**
      * When the jar file can be manipulated as a java.util.JarFile
      * this holds the reference to the open jar. When the jar can
      * only be manipulated as an InputStream (because the jar is itself
@@ -77,45 +84,53 @@
 		this.vs = vs;
 	}
 
-	// Initialize the class loader so it knows if it
-	// is loading from a ZipFile or an InputStream
+	/**
+	 *  Initialize the class loader so it knows if it
+	 *  is loading from a ZipFile or an InputStream
+	 */
 	void initialize() {
 
-		Object zipData = load();
+		String schemaName = name[IdUtil.DBCP_SCHEMA_NAME];
+		String sqlName = name[IdUtil.DBCP_SQL_JAR_NAME];
 
+		Exception e;
 		try {
+			installedJar =
+				updateLoader.getJarReader().getJarFile(
+					schemaName, sqlName);
 
-			if (zipData instanceof File) {
-                jar = new JarFile((File) zipData);
+			if (installedJar instanceof File) {
+				jar = new JarFile((File) installedJar);
 				return;
 			}
 
-            // Jar is only accessible as an INputStream,
-            // which means we need to re-open the stream for
-            // each access. Thus we close the stream now as we have
-            // no further use for it.
-			if (zipData instanceof InputStream) {
-				isStream = true;
-				try {
-					((InputStream) zipData).close();
-				} catch (IOException ioe) {
-				}
-				return;
-			}
+			// Jar is only accessible as an InputStream,
+			// which means we need to re-open the stream for
+			// each access.
+
+			isStream = true;
+			return;
+
 		} catch (IOException ioe) {
-			if (vs != null)
-				vs.println(MessageService.getTextMessage(MessageId.CM_LOAD_JAR_EXCEPTION, getJarName(), ioe));
+			e = ioe;
+		} catch (StandardException se) {
+			e = se;
 		}
 
+		if (vs != null)
+			vs.println(MessageService.getTextMessage(
+					MessageId.CM_LOAD_JAR_EXCEPTION, getJarName(), e));
+
 		// No such zip.
-		setInvalid();	
+		setInvalid();
 	}
 
 	/**
-		Handle all requests to the top-level loader.
-
-		@exception ClassNotFoundException Class can not be found
-	*/
+	 * Handle all requests to the top-level loader.
+	 * 
+	 * @exception ClassNotFoundException
+	 *                Class can not be found
+	 */
 	public Class loadClass(String className, boolean resolve) 
 		throws ClassNotFoundException {
 
@@ -164,11 +179,14 @@
 
 			if (isStream) {
 				// have to use a new stream each time
-				return loadClassData((InputStream) load(),
+				return loadClassData(installedJar.getInputStream(),
 						className, jvmClassName, resolve);
 			}
 
 			return null;
+		} catch (FileNotFoundException fnfe) {
+			// No such entry.
+			return null;
 		} catch (IOException ioe) {
 			if (vs != null)
 				vs.println(MessageService.getTextMessage(MessageId.CM_CLASS_LOAD_EXCEPTION, className, getJarName(), ioe));
@@ -188,7 +206,11 @@
 			return getRawStream(name);
 
 		if (isStream) {
-			return getRawStream((InputStream) load(), name);
+			try {
+				return getRawStream(installedJar.getInputStream(), name);
+			} catch (FileNotFoundException e) {
+				// no such entry
+			}
 		}
 		return null;
 	}
@@ -285,24 +307,6 @@
 		if ((c != null) && resolve)
 			resolveClass(c);
 		return c;
-	}
-
-	private Object load() {
-
-		String[] dbJarName = name;
-
-		String schemaName = dbJarName[IdUtil.DBCP_SCHEMA_NAME];
-		String sqlName = dbJarName[IdUtil.DBCP_SQL_JAR_NAME];
-
-		// don't need a connection, just call the code directly
-		try {
-			return updateLoader.getJarReader().readJarFile(schemaName, sqlName);
-		} catch (StandardException se) {
-			if (vs != null)
-				vs.println(MessageService.getTextMessage(MessageId.CM_LOAD_JAR_EXCEPTION, getJarName(), se));
-			return null;
-		}
-
 	}
 
     /**