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/06 16:15:30 UTC

svn commit: r483108 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect: InstalledJar.java JarLoader.java

Author: djd
Date: Wed Dec  6 07:15:29 2006
New Revision: 483108

URL: http://svn.apache.org/viewvc?view=rev&rev=483108
Log:
DERBY-538 (partial) Cleanup the jar loading code to consistently use the java.util.jar classes
instead of the java.util.zip classes.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/InstalledJar.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/impl/services/reflect/InstalledJar.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/InstalledJar.java?view=diff&rev=483108&r1=483107&r2=483108
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/InstalledJar.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/InstalledJar.java Wed Dec  6 07:15:29 2006
@@ -24,11 +24,8 @@
 import java.security.GeneralSecurityException;
 import java.security.cert.Certificate;
 import java.security.cert.X509Certificate;
+import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
-import java.util.jar.JarInputStream;
-import java.util.zip.ZipFile;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
 
 import java.io.IOException;
 import java.io.File;
@@ -47,29 +44,48 @@
  * or an InputStream (database is in a jar file itself).
  */
 final class InstalledJar {
-	final String[] name;
-	private JarFile jar;
-	boolean isStream;
+    /**
+     * Two part name for the jar file.
+     */
+    final String[] name;
+    
+    /**
+     * 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
+     * in a database jar) then this will be null.
+     */
+    private JarFile jar;
+    
+    /**
+     * True if the jar can only be accessed using a stream, because
+     * the jar is itself in a database jar. When fals the jar is accessed
+     * using the jar field.
+     */
+    boolean isStream;
 
 	InstalledJar(String[] name) {
 		this.name = name;
 	}
 
+    /**
+     * Return the SQL name for the installed jar.
+     * Used for error and informational messages.
+     */
 	final String getJarName() {
 		return IdUtil.mkQualifiedName(name);
 	}
 
-
-	final boolean isZip() {
-		return jar != null;
-	}
-
-	final ZipFile getZip() {
+    /**
+     * Return the JarFile for this installed jar when it
+     * is being accessed through java.util.jar.
+     */
+	final JarFile getJarFile() {
 		return jar;
 	}
 
 	void initialize(File jarFile) throws IOException {
-        jar = new java.util.jar.JarFile(jarFile);
+        jar = new JarFile(jarFile);
 	}
 
 	final void setInvalid() {
@@ -84,17 +100,7 @@
 		isStream = false;
 	}
 
-	ZipEntry getEntry(String entryName) {
-		return jar.getJarEntry(entryName);
-	}
-	ZipInputStream getZipOnStream(InputStream in) throws IOException {
-		return new JarInputStream(in);
-	}
-	ZipEntry getNextEntry(ZipInputStream in) throws IOException {
-		return ((JarInputStream) in).getNextJarEntry();
-	}
-
-	byte[] readData(ZipEntry ze, InputStream in, String className) throws IOException {
+	byte[] readData(JarEntry ze, InputStream in, String className) throws IOException {
 
 		try {
             int size = (int) ze.getSize();
@@ -122,11 +128,11 @@
         }
 	}
 
-    Object[] getSigners(String className, ZipEntry ze) throws IOException {
+    Object[] getSigners(String className, JarEntry je) throws IOException {
         Exception e;
 
         try {
-            Certificate[] list = ((java.util.jar.JarEntry) ze).getCertificates();
+            Certificate[] list = je.getCertificates();
             if ((list == null) || (list.length == 0)) {
                 return null;
             }
@@ -159,4 +165,4 @@
         String msg = MessageService.getTextMessage(MessageId.CM_SECURITY_EXCEPTION, className, getJarName(), e.getLocalizedMessage());
         return new SecurityException(msg);
     }
-}
+}
\ No newline at end of file

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=483108&r1=483107&r2=483108
==============================================================================
--- 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 Wed Dec  6 07:15:29 2006
@@ -28,10 +28,9 @@
 import java.io.InputStream;
 import java.io.IOException;
 
-import java.util.zip.ZipFile;
-import java.util.zip.ZipInputStream;
-import java.util.zip.ZipEntry;
-
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
 
 import org.apache.derby.iapi.services.io.LimitInputStream;
 import org.apache.derby.iapi.util.IdUtil;
@@ -130,8 +129,9 @@
 			return null;
 
 		try {
-			if (jf.isZip())
-				return loadClassDataFromJar(className, jvmClassName, resolve);
+            JarFile jar = jf.getJarFile();
+			if (jar != null)
+				return loadClassDataFromJar(jar, className, jvmClassName, resolve);
 
 			if (jf.isStream) {
 				// have to use a new stream each time
@@ -154,9 +154,11 @@
 
 		if (updateLoader == null)
 			return null;
+        
+        JarFile jar = jf.getJarFile();
 
-		if (jf.isZip())
-			return getRawStream(jf.getZip(), name);
+		if (jar != null)
+			return getRawStream(jar, name);
 
 		if (jf.isStream) {
 			return getRawStream((InputStream) load(), name);
@@ -170,51 +172,61 @@
 	*/
 
 
-	private Class loadClassDataFromJar(String className, String jvmClassName, boolean resolve) 
+    /**
+     * Load the class data when the installed jar is accessible
+     * as a java.util.jarFile.
+     */
+	private Class loadClassDataFromJar(
+            JarFile jar,
+            String className, String jvmClassName, boolean resolve) 
 		throws IOException {
 
-		ZipEntry ze = jf.getEntry(jvmClassName);
-		if (ze == null)
+		JarEntry e = jar.getJarEntry(jvmClassName);
+		if (e == null)
 			return null;
 
-		InputStream in = jf.getZip().getInputStream(ze);
+		InputStream in = jar.getInputStream(e);
 
 		try {
-			return loadClassData(ze, in, className, resolve);
+			return loadClassData(e, in, className, resolve);
 		} finally {
 			in.close();
 		}
 	}
 
+    /**
+     * Load the class data when the installed jar is accessible
+     * only as an input stream (the jar is itself in a database jar).
+     */
 	private Class loadClassData(
 		InputStream in, String className, String jvmClassName, boolean resolve) 
 		throws IOException {
 
-		ZipInputStream zipIn = jf.getZipOnStream(in);
+        JarInputStream jarIn = new JarInputStream(in);
 
 		for (;;) {
 
-			ZipEntry ze = jf.getNextEntry(zipIn);
-			if (ze == null) {
-				zipIn.close();
+			JarEntry e = jarIn.getNextJarEntry();
+			if (e == null) {
+				jarIn.close();
 				return null;
 			}
 
-			if (ze.getName().equals(jvmClassName)) {
-				Class c = loadClassData(ze, zipIn, className, resolve);
-				zipIn.close();
+			if (e.getName().equals(jvmClassName)) {
+				Class c = loadClassData(e, jarIn, className, resolve);
+				jarIn.close();
 				return c;
 			}
 		}
 		
 	}
 
-	private Class loadClassData(ZipEntry ze, InputStream in,
+	private Class loadClassData(JarEntry e, InputStream in,
 		String className, boolean resolve) throws IOException {
 
-		byte[] data = jf.readData(ze, in, className);
+		byte[] data = jf.readData(e, in, className);
 
-		Object[] signers = jf.getSigners(className, ze);
+		Object[] signers = jf.getSigners(className, e);
 
 		synchronized (updateLoader) {
 			// see if someone else loaded it while we
@@ -273,19 +285,19 @@
 	*/
 
 	/**
-		Get a stream directly from a ZipFile.
+		Get a stream for a resource directly from a JarFile.
 		In this case we can safely return the stream directly.
 		It's a new stream set up by the zip code to read just
 		the contents of this entry.
 	*/
-	private InputStream getRawStream(ZipFile zip, String name) {
+	private InputStream getRawStream(JarFile jar, String name) {
 
 		try {
-			ZipEntry ze = zip.getEntry(name);
-			if (ze == null)
+			JarEntry e = jar.getJarEntry(name);
+			if (e == null)
 				return null;
 
-			return zip.getInputStream(ze);
+			return jar.getInputStream(e);
 		} catch (IOException ioe) {
 			return null;
 		}
@@ -299,26 +311,26 @@
 	*/
 	private InputStream getRawStream(InputStream in, String name) { 
 
-		ZipInputStream zipIn = null;
+		JarInputStream jarIn = null;
 		try {
-			zipIn = new ZipInputStream(in);
+			jarIn = new JarInputStream(in);
 
-			ZipEntry ze;
-			while ((ze = jf.getNextEntry(zipIn)) != null) {
+		    JarEntry e;
+			while ((e = jarIn.getNextJarEntry()) != null) {
 
-				if (ze.getName().equals(name)) {
-					LimitInputStream lis = new LimitInputStream(zipIn);
-					lis.setLimit((int) ze.getSize());
+				if (e.getName().equals(name)) {
+					LimitInputStream lis = new LimitInputStream(jarIn);
+					lis.setLimit((int) e.getSize());
 					return lis;
 				}
 			}
 
-			zipIn.close();
+			jarIn.close();
 
 		} catch (IOException ioe) {
-			if (zipIn != null) {
+			if (jarIn != null) {
 				try {
-					zipIn.close();
+					jarIn.close();
 				} catch (IOException ioe2) {
 				}
 			}