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;
*/
File originalBackup = null;
- boolean donotdeletebackup=false;
+ boolean donotdeletebackup=true;
private final int _TRANSFER_BUFFER=4096*4;
protected SimpleDocument vorba = null;
/**
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);
+ // log.debug("using non-functional lock-IO stream jar access constructor");
+ }
+ /**
+ * read and write to archive - will not overwrite original contents, and will always write an up to date vamsas document structure.
+ * @param archive
+ * @throws IOException
+ */
+ public VamsasArchive(VamsasFile archive) throws IOException {
+ this(archive, true, false);
+ }
+ /**
*
* @param archive file to write
* @param overwrite true if original contents should be deleted
*/
public VamsasArchive(File archive, boolean overwrite, boolean vamsasdocument, SessionFile extantLock) throws IOException {
super();
- if (archive==null || (archive!=null && !(archive.getParentFile().canWrite() && (!archive.exists() || archive.canWrite())))) {
+ if (archive==null || (archive!=null && !(archive.getAbsoluteFile().getParentFile().canWrite() && (!archive.exists() || archive.canWrite())))) {
log.fatal("Expect Badness! -- Invalid parameters for VamsasArchive constructor:"+((archive!=null)
? "File cannot be overwritten." : "Null Object not valid constructor parameter"));
return;
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;
else
rchive = new SessionFile(archive);
rchive.lockFile();
- if (rchive.fileLock==null || rchive.fileLock.rafile==null)
+ if (rchive.fileLock==null || !rchive.fileLock.isLocked())
throw new IOException("Lock failed for new archive"+archive);
- rchive.fileLock.rafile.setLength(0); // empty the archive.
+ rchive.fileLock.getRaFile().setLength(0); // empty the archive.
virginArchive = true;
}
this.openArchive(); // open archive
odoclock.lockFile();
if (odoc == null)
odoc = new VamsasArchiveReader(original);
+ // this constructor is not implemented yet odoc = new VamsasArchiveReader(odoclock.fileLock);
}
}
JarEntry je = new JarEntry(entry);
if (!addEntry(entry))
return false;
+ newarchive.flush();
newarchive.putNextEntry(je);
return true;
}
public boolean cancelArchive() {
if (newarchive!=null) {
try {
+ newarchive.closeEntry();
+ newarchive.putNextEntry(new JarEntry("deleted"));
+ newarchive.closeEntry();
newarchive.close();
} catch (Exception e) {
*/
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();// close(); // use newarchive.finish(); for a stream IO
+ newarchive.flush();
+ //
updateOriginal();
closeAndReset();
} else {
rchive = new SessionFile(archive);
if (!rchive.lockFile())
throw new IOException("Failed to get lock on file "+archive);
- newarchive = new JarOutputStream(rchive.fileLock.getBufferedOutputStream(true));
+ // LATER: locked IO stream based access.
+ Manifest newmanifest = new Manifest();
+ newarchive = new JarOutputStream(rchive.fileLock.getBufferedOutputStream(true), newmanifest);
+ //newarchive = new JarOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(archive)));
entries = new Hashtable();
}
public void putVamsasDocument(VamsasDocument doc) throws IOException,
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);
}
odoclock.updateFrom(null, rchive);
}
catch (IOException e) {
- // LATER: decide if leaving nastily named backup files around is necessary.
- log.error("Problem updating archive from temporary file! - backup left in '"
+ // LATER: decide if leaving nastily named backup files around is necessary.
+ File backupFile=backupFile();
+ if (backupFile!=null)
+ log.error("Problem updating archive from temporary file! - backup left in '"
+backupFile().getAbsolutePath()+"'",e);
+ else
+ log.error("Problems updating, and failed to even make a backup file. Ooops!", e);
}
// Tidy up if necessary.
removeBackup();
} else {
+
}
}
}