Switched to using pure java Apache Zip implementation
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasArchiveReader.java
index 7f9a008..97efc73 100644 (file)
@@ -10,13 +10,7 @@ import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Vector;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
-import java.util.jar.JarInputStream;
-import java.util.jar.JarOutputStream;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-
+import org.apache.tools.zip.*;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -33,18 +27,21 @@ public class VamsasArchiveReader {
   private static final int JARFILE_OPEN_RETRIES = 50;
   private static final int JARFILE_OPEN_RETRYWAIT = 1;
   private static Log log = LogFactory.getLog(VamsasArchiveReader.class);
-  JarFile jfile=null;
+  ZipFile jfile=null;
+  String jfileName="randomAccessFile";
   boolean stream=false; // true if we are seeking on the stream.
   RandomAccessFile rfile;
-  ZipInputStream jstream=null;
+  // ZipInputStream jstream=null;
   Hashtable strmentries = null;
   private void streamInit() {
     //throw new Error("VamsasArchiveReader(Stream) Not implemented!");
      if (!stream) {
       log.debug("Skipping init for Jar Stream input.");
       return;
+    } else {
+      throw new Error("Implementation error - we don't do streams - only files or RA files");
     }
-    strmentries = new Hashtable();
+     /*strmentries = new Hashtable();
     log.debug("Jar Stream input Initialisation");
     try {
       rfile.seek(0);
@@ -67,7 +64,7 @@ public class VamsasArchiveReader {
     catch (Exception e) {
       log.warn("Exceptions during init!",e);
       jstream=null;
-    }
+    } */
   }
   
   public VamsasArchiveReader(File vamsasfile) {
@@ -78,7 +75,8 @@ public class VamsasArchiveReader {
       while (jfile==null && --retries>0)
       {
       try {
-        jfile=new JarFile(vamsasfile);
+        jfile=new ZipFile(vamsasfile);
+        jfileName = vamsasfile.toString();
       }
       catch (Exception e) {
         ex = e;
@@ -101,19 +99,25 @@ public class VamsasArchiveReader {
    * @param vamsaslock
    */
   public VamsasArchiveReader(Lock vamsaslock) {
-    // LATER: implement or remove
+    jfile = null;
     if (vamsaslock==null || !vamsaslock.isLocked())
       throw new Error("IMPLEMENTATION ERROR: Cannot create a VamsasArchiveReader without a valid lock.");
     // throw new Error("VamsasArchiveReading from locked IO stream not yet implemented.");
     try {
       rfile = vamsaslock.getRaFile();
+      jfile = new ZipFile(rfile);
+      if (vamsaslock.target!=null)
+        jfileName = vamsaslock.target.toString();
     } catch (Exception e) {
+      rfile = null;
+      jfile = null;
       log.warn("Unexpected IO Exception when accessing locked vamsas archive stream "+vamsaslock.target,e);
     }
-    stream = true;
+    /*stream = true;
     streamInit();
     if (jstream==null)
       throw new Error("Failed to open archive from Locked random access stream.");
+      */
   }
   
   /**
@@ -136,7 +140,6 @@ public class VamsasArchiveReader {
    * seeks jstream to the given entry name and reads it.
    * @param entryname
    * @return
-   */
   private JarEntry seekEntry(String entryname) {
     if (jstream==null)
       return null;
@@ -165,18 +168,19 @@ public class VamsasArchiveReader {
     }
     return null;
   }
+   */
   /**
    * 
    * @return JarEntry for VamsasArchiveReader.VAMSASDOC
    */
-  protected JarEntry getVamsasDocumentEntry() {
+  protected ZipEntry getVamsasDocumentEntry() {
     return getJarEntry(VAMSASDOC);
   }
   /**
    * 
    * @return JarEntry for VamsasArchiveReader.VAMSASXML
    */
-  protected JarEntry getVamsasXmlEntry() {
+  protected ZipEntry getVamsasXmlEntry() {
     return getJarEntry(VAMSASXML);
   }
   /**
@@ -185,22 +189,22 @@ public class VamsasArchiveReader {
    */
   public boolean isValid() {
     // TODO: check if VAMSASDOC is well formed (follows www.vamsas.ac.uk/schemas/vamsasDocument.xsd) and all appData references are resolvable - preferably as jar entries
-    if (jfile!=null || jstream!=null)
+    if (jfile!=null) //  || jstream!=null)
       return (getVamsasDocumentEntry()!=null);
     return false;   
   }
   
   
-  protected JarEntry getAppdataEntry(String AppdataRef) {
-    JarEntry entry;
-    if ((jfile==null && jstream==null) || !isValid() || (entry=getJarEntry(AppdataRef))==null)
+  protected ZipEntry getAppdataEntry(String AppdataRef) {
+    ZipEntry entry;
+    if (jfile==null || !isValid() || (entry=getJarEntry(AppdataRef))==null)
       return null;
     
     return entry;
   }
   
   public InputStream getAppdataStream(String AppdataRef) {
-    JarEntry entry=getAppdataEntry(AppdataRef);
+    ZipEntry entry=getAppdataEntry(AppdataRef);
     try {
       if (entry!=null)
         return getInputStream(entry);
@@ -215,7 +219,7 @@ public class VamsasArchiveReader {
    */
   public InputStream getVamsasDocumentStream() {
     InputStream vdoc;
-    if ((jfile==null && jstream==null) || !isValid())
+    if (jfile==null || !isValid())
       return null;
     try {
       vdoc = getInputStream(getVamsasDocumentEntry());
@@ -234,7 +238,7 @@ public class VamsasArchiveReader {
   
   public InputStream getVamsasXmlStream() {
     // log.warn("Deprecated call");
-    JarEntry xmle=getVamsasXmlEntry();
+    ZipEntry xmle=getVamsasXmlEntry();
     InputStream vdoc;
     if (xmle==null)
       return null;
@@ -255,18 +259,10 @@ public class VamsasArchiveReader {
     if (jfile!=null) {
       try {
         jfile.close();
-      } catch (IOException e) {
-        log.error("Whilst closing JarFile "+jfile.getName(), e);
-      }
-    }
-    if (jstream!=null) {
-      try {
-        jstream.closeEntry();
-        jstream=null;
-        // LATER: reference counting for random access file instances is necessary.
-      }
+        rfile=null;
+      } 
       catch (Exception e) {
-        log.error("Whilst finishing reading from jar input stream",e);
+        log.error("Whilst closing JarFile "+jfileName, e);
       }
     }
   }
@@ -276,10 +272,10 @@ public class VamsasArchiveReader {
    * @return array of entries.
    */
   public Vector getExtraEntries() {
-    if ((jfile==null && jstream==null)|| !isValid())
+    if (jfile==null || !isValid())
       return null;
     Vector e = new Vector();
-    if (jstream!=null) {
+    /*if (jstream!=null) {
       Enumeration entries = strmentries.keys();
       if (entries!=null && entries.hasMoreElements()) {
         do {
@@ -288,18 +284,16 @@ public class VamsasArchiveReader {
             e.add(new String(el.getName())); // avoid references
         } while (entries.hasMoreElements());
       }
-    } else {
-      Enumeration entries = jfile.entries();
-      if (entries!=null && entries.hasMoreElements()) {
-        do {
-          JarEntry el = (JarEntry) entries.nextElement();
-          if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
-            e.add(new String(el.getName())); // avoid references
-        } while (entries.hasMoreElements());
-      }
-      return e;
+    } else */
+    Enumeration entries = jfile.getEntries();
+    if (entries!=null && entries.hasMoreElements()) {
+      do {
+        ZipEntry el = (ZipEntry) entries.nextElement();
+        if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
+          e.add(new String(el.getName())); // avoid references
+      } while (entries.hasMoreElements());
     }
-    return null;
+    return e;
   }
   
   /* (non-Javadoc)
@@ -308,21 +302,15 @@ public class VamsasArchiveReader {
   private InputStream getInputStream(ZipEntry ze) throws IOException {
     if (jfile!=null)
       return jfile.getInputStream(ze);
-    if (jstream!=null) {
-      seekEntry(ze.getName());
-      return new AppDataInputStream(jstream);
-    }
     return null;
   }
     
     /* (non-Javadoc)
      * @see java.util.jar.JarFile#getJarEntry(java.lang.String)
      */
-    private JarEntry getJarEntry(String name) {
+    private ZipEntry getJarEntry(String name) {
       if (jfile!=null)
-        return jfile.getJarEntry(name);
-      if (jstream!=null)
-        return seekEntry(name);
+        return jfile.getEntry(name);
       return null;
     }
   }