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;
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);
catch (Exception e) {
log.warn("Exceptions during init!",e);
jstream=null;
- }
+ } */
}
public VamsasArchiveReader(File vamsasfile) {
while (jfile==null && --retries>0)
{
try {
- jfile=new JarFile(vamsasfile);
+ jfile=new ZipFile(vamsasfile);
+ jfileName = vamsasfile.toString();
}
catch (Exception e) {
ex = e;
* @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.");
+ */
}
/**
* seeks jstream to the given entry name and reads it.
* @param entryname
* @return
- */
private JarEntry seekEntry(String entryname) {
if (jstream==null)
return null;
}
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);
}
/**
*/
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);
*/
public InputStream getVamsasDocumentStream() {
InputStream vdoc;
- if ((jfile==null && jstream==null) || !isValid())
+ if (jfile==null || !isValid())
return null;
try {
vdoc = getInputStream(getVamsasDocumentEntry());
public InputStream getVamsasXmlStream() {
// log.warn("Deprecated call");
- JarEntry xmle=getVamsasXmlEntry();
+ ZipEntry xmle=getVamsasXmlEntry();
InputStream vdoc;
if (xmle==null)
return null;
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);
}
}
}
* @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 {
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)
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;
}
}