X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FVamsasArchive.java;h=4c4fbfd21c7c5e7157ab3b1dc639a12300fe179f;hb=d2cb3f9a881f6b937adb5c54a4b20a08eb2e99db;hp=c58c5f36b762d90393b303b22c6af5cc13a7d4d0;hpb=d3c6dfdbb5e6d268c45ddf261f941af1309649ba;p=vamsas.git diff --git a/src/org/vamsas/client/simpleclient/VamsasArchive.java b/src/org/vamsas/client/simpleclient/VamsasArchive.java index c58c5f3..4c4fbfd 100644 --- a/src/org/vamsas/client/simpleclient/VamsasArchive.java +++ b/src/org/vamsas/client/simpleclient/VamsasArchive.java @@ -15,6 +15,7 @@ import java.util.Iterator; import java.util.Vector; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -187,6 +188,16 @@ public class VamsasArchive { this(archive, overwrite, vamsasdocument, null); } /** + * Constructor for accessing Files under file-lock management (ie a session file) + * @param archive + * @param vamsasdocument + * @param overwrite + * @throws IOException + */ + public VamsasArchive(VamsasFile archive, boolean vamsasdocument, boolean overwrite) throws IOException { + this(archive.sessionFile, overwrite, vamsasdocument, archive); + } + /** * * @param archive file to write * @param overwrite true if original contents should be deleted @@ -205,10 +216,13 @@ public class VamsasArchive { this.vamsasdocument = vamsasdocument; if (archive.exists() && !overwrite) { this.original = archive; - if (extantLock!=null) + if (extantLock!=null) { this.odoclock = extantLock; - else - this.odoclock = new SessionFile(archive); + if (odoclock.fileLock==null || !odoclock.fileLock.isLocked()) + odoclock.lockFile(); + } else { + this.odoclock = new SessionFile(archive); + } odoclock.lockFile(); // lock the file *immediatly* this.archive = null; // archive will be a temp file when the open method is called virginArchive=false; @@ -225,7 +239,7 @@ public class VamsasArchive { else rchive = new SessionFile(archive); rchive.lockFile(); - if (rchive.fileLock==null || rchive.fileLock.rafile==null) + if (rchive.fileLock==null || rchive.fileLock.rafile==null || !rchive.fileLock.isLocked()) throw new IOException("Lock failed for new archive"+archive); rchive.fileLock.rafile.setLength(0); // empty the archive. virginArchive = true; @@ -269,6 +283,7 @@ public class VamsasArchive { JarEntry je = new JarEntry(entry); if (!addEntry(entry)) return false; + newarchive.flush(); newarchive.putNextEntry(je); return true; } @@ -294,6 +309,9 @@ public class VamsasArchive { public boolean cancelArchive() { if (newarchive!=null) { try { + newarchive.closeEntry(); + newarchive.putNextEntry(new JarEntry("deleted")); + newarchive.closeEntry(); newarchive.close(); } catch (Exception e) { @@ -351,10 +369,11 @@ public class VamsasArchive { */ public void closeArchive() throws IOException { if (newarchive!=null) { + newarchive.flush(); newarchive.closeEntry(); if (!isDocumentWritten()) log.warn("Premature closure of archive '"+archive.getAbsolutePath()+"': No document has been written."); - newarchive.close(); + newarchive.finish(); updateOriginal(); closeAndReset(); } else { @@ -533,7 +552,8 @@ public class VamsasArchive { rchive = new SessionFile(archive); if (!rchive.lockFile()) throw new IOException("Failed to get lock on file "+archive); - newarchive = new JarOutputStream(rchive.fileLock.getBufferedOutputStream(true)); + Manifest newmanifest = new Manifest(); + newarchive = new JarOutputStream(rchive.fileLock.getBufferedOutputStream(true), newmanifest); entries = new Hashtable(); } public void putVamsasDocument(VamsasDocument doc) throws IOException, @@ -543,6 +563,8 @@ public class VamsasArchive { public void putVamsasDocument(VamsasDocument doc, VorbaIdFactory vorba) throws IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException { + if (vamsasdocument) + doc.setVersion(VersionEntries.latestVersion()); // LATER: ensure this does the correct thing. VorbaXmlBinder.putVamsasDocument(getDocumentOutputStream(), vorba, doc); }