+ * Access original archive if it exists, pass the reader to the client
+ * Note: this is NOT thread safe and a call to closeArchive() will by necessity
+ * close and invalidate the VamsasArchiveReader object.
+ * @return null if no original archive exists.
+ */
+ public VamsasArchiveReader getOriginalArchiveReader() throws IOException {
+ if (!virginArchive) {
+ accessOriginal();
+ return odoc;
+ }
+ return null;
+ }
+ /**
+ * returns original document's root vamsas elements.
+ * @return
+ * @throws IOException
+ * @throws org.exolab.castor.xml.MarshalException
+ * @throws org.exolab.castor.xml.ValidationException
+ */
+ public Vobject[] getOriginalRoots() throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ return VamsasArchive.getOriginalRoots(this);
+ }
+ /**
+ * @return original document or a new empty document (with default provenance)
+ * @throws IOException
+ * @throws org.exolab.castor.xml.MarshalException
+ * @throws org.exolab.castor.xml.ValidationException
+ */
+ public VamsasDocument getVamsasDocument() throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ return getVamsasDocument("org.vamsas.simpleclient.VamsasArchive", "Created new empty document", null);
+ }
+ /**
+ * Return the original document or a new empty document with initial provenance entry.
+ * @param provenance_user (null sets user to be the class name)
+ * @param provenance_action (null sets action to be 'created new document')
+ * @param version (null means use latest version)
+ * @return (original document or a new vamsas document with supplied provenance and version info)
+ * @throws IOException
+ * @throws org.exolab.castor.xml.MarshalException
+ * @throws org.exolab.castor.xml.ValidationException
+ */
+ public VamsasDocument getVamsasDocument(String provenance_user, String provenance_action, String version) throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ if (_doc!=null)
+ return _doc;
+ _doc = getOriginalVamsasDocument(this, getVorba());
+ if (_doc!=null)
+ return _doc;
+ // validate parameters
+ if (provenance_user==null)
+ provenance_user = "org.vamsas.simpleclient.VamsasArchive";
+ if (provenance_action == null)
+ provenance_action="Created new empty document";
+ if (version==null)
+ version = VersionEntries.latestVersion();
+ // Create a new document and return it
+ _doc = DocumentStuff.newVamsasDocument(new VAMSAS[] { new VAMSAS()},
+ ProvenanceStuff.newProvenance(provenance_user, provenance_action), version);
+ return _doc;
+ }
+ /**
+ * @return Returns the current VorbaIdFactory for the archive.
+ */
+ public VorbaIdFactory getVorba() {
+ if (vorba==null)
+ vorba = new SimpleDocument("simpleclient.VamsasArchive");
+ return vorba.getVorba();
+ }
+ /**
+ * @return true if Vamsas Document has been written to archive
+ */
+ protected boolean isDocumentWritten() {
+ if (newarchive==null)
+ log.warn("isDocumentWritten() called for unopened archive.");
+ if (entries!=null) {
+ if (entries.containsKey(getDocumentJarEntry()))
+ return true;
+ }
+ return false;
+ }
+ private void makeBackup() {
+ if (!virginArchive) {
+ if (originalBackup==null && original!=null && original.exists()) {
+ try {
+ accessOriginal();
+ originalBackup = odoclock.backupSessionFile(null, original.getName(), ".bak", original.getParentFile());
+ }
+ catch (IOException e) {
+ log.warn("Problem whilst making a backup of original archive.",e);
+ }
+ }
+ }
+ }
+ /**
+ * opens the new archive ready for writing. If the new archive is replacing an existing one,
+ * then the existing archive will be locked, and the new archive written to a temporary file.
+ * The new archive will be put in place once close() is called.
+ * @param doclock TODO
+ * @throws IOException
+ */
+ private void openArchive() throws IOException {
+
+ if (newarchive!=null) {
+ log.warn("openArchive() called multiple times.");
+ throw new IOException("Vamsas Archive '"+archive.getAbsolutePath()+"' is already open.");
+ }
+ if (archive==null && (virginArchive || original==null)) {
+ log.warn("openArchive called on uninitialised VamsasArchive object.");
+ throw new IOException("Badly initialised VamsasArchive object - no archive file specified.");
+ }
+ if (!virginArchive) {
+ // lock the original
+ accessOriginal();
+ // make a temporary file to write to
+ archive = File.createTempFile(original.getName(), ".new",original.getParentFile());
+ } else {
+ if (archive.exists())
+ log.warn("New archive file name already in use! Possible lock failure imminent?");
+ }
+
+ if (rchive==null)
+ rchive = new SessionFile(archive);
+ if (!rchive.lockFile())
+ throw new IOException("Failed to get lock on file "+archive);
+ newarchive = new JarOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(archive)));
+ entries = new Hashtable();
+ }
+ public void putVamsasDocument(VamsasDocument doc) throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ putVamsasDocument(doc, getVorba());
+ }
+
+ public void putVamsasDocument(VamsasDocument doc, VorbaIdFactory vorba) throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ VorbaXmlBinder.putVamsasDocument(getDocumentOutputStream(), vorba, doc);
+ }
+
+ /**
+ * recovers the original file's contents from the (temporary) backup.
+ * @throws Exception if any SessionFile or file removal operations fail.
+ */
+ private void recoverBackup() throws Exception {
+ if (originalBackup!=null) {
+ // backup has been made.
+ // revert from backup and delete it (changing backup filename)
+ if (rchive==null) {
+ rchive = new SessionFile(original);
+ }
+ SessionFile bckup = new SessionFile(originalBackup);
+
+ rchive.updateFrom(null, bckup); // recover from backup file.
+ bckup.unlockFile();
+ bckup=null;
+ removeBackup();
+ }
+ }
+
+ /**
+ * forget about any backup that was made - removing it first if it was only temporary.
+ */
+ private void removeBackup() {
+ if (originalBackup!=null) {
+ log.debug("Removing backup in "+originalBackup.getAbsolutePath());
+ if (!donotdeletebackup)
+ if (!originalBackup.delete())
+ log.info("VamsasArchive couldn't remove temporary backup "+originalBackup.getAbsolutePath());
+ originalBackup=null;
+ }
+ }
+ /**
+ * @param vorba the VorbaIdFactory to use for accessing vamsas objects.
+ */
+ public void setVorba(VorbaIdFactory Vorba) {
+ if (Vorba!=null) {
+ if (vorba==null)
+ vorba = new SimpleDocument(Vorba);
+ else
+ vorba.setVorba(Vorba);
+ } else
+ getVorba();
+ }
+ /**