if (!virginArchive) {
makeBackup();
return ((original!=null) ? originalBackup : null);
-
}
return null;
}
*/
protected boolean isDocumentWritten() {
if (newarchive==null)
- log.warn("isDocumentWritten called for unopened archive.");
+ log.warn("isDocumentWritten() called for unopened archive.");
if (entries!=null) {
if (entries.containsKey(getDocumentJarEntry()))
return true;
* @throws IOException
*/
public AppDataOutputStream getAppDataStream(String appdataReference) throws IOException {
- if (newarchive!=null)
+ if (newarchive==null)
openArchive();
if (addValidEntry(appdataReference)) {
return new AppDataOutputStream(newarchive);
/**
* Stops any current write to archive, and reverts to the backup if it exists.
- * All existing locks on the original will be released.
+ * All existing locks on the original will be released. All backup files are removed.
*/
public boolean cancelArchive() {
if (newarchive!=null) {
return false;
}
}
- // original is untouched
- // just delete temp files
-
-
}
} else {
log.info("cancelArchive called before archive("+original.getAbsolutePath()+") has been opened!");
}
- closeAndReset();
+ closeAndReset(); // tidy up and release locks.
return true;
}
/**
* Tidies up and closes archive, removing any backups that were created.
* NOTE: It is up to the caller to delete the original archive backup obtained from backupFile()
+ * TODO: ensure all extant AppDataReference jar entries are transferred to new Jar
+ * TODO: provide convenient mechanism for generating new unique AppDataReferences and adding them to the document
*/
public void closeArchive() throws IOException {
if (newarchive!=null) {
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.ObjectOutputStream;
+import java.io.PrintWriter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.SimpleClient;
import org.vamsas.client.simpleclient.*;
+import org.vamsas.objects.core.AppDataChoice;
+import org.vamsas.objects.core.ApplicationData;
+import org.vamsas.objects.core.User;
import org.vamsas.objects.core.VAMSAS;
import org.vamsas.objects.core.VamsasDocument;
+import org.vamsas.test.objects.Core;
public class VamsasArchive {
/**
* test the org.vamsas.simpleclient.vamsasArchive class
*/
static Log log = LogFactory.getLog("org.vamsas.test.simpleclient.vamsasArchive");
-
+
public static void main(String args[]) {
-
+
try {
File av;
if (args.length>0)
log.info(av+" is a New Archive.");
else
log.info(av+" has been backed up as "+backup);
- varchive.cancelArchive();
if (backup!=null) {
- log.info("Backup "+backup+" is being deleted.");
+ log.info("Backup "+backup+" is being written to.");
+ // open another and...
+ ApplicationData appdata = new ApplicationData();
+ appdata.setName("org.vamsas.test.simpleclient.VamsasArchive");
+ appdata.setAppDataChoice(new AppDataChoice());
+ appdata.getAppDataChoice().setData(new String("this is some test data.").getBytes());
+ User apuser = new User();
+ apuser.setFullname("arnold Bugger esq");
+ apuser.setOrganization("disOrganised");
+ String appdata_ref = "vamsas:org.vamsas.test.simpleclient.VamsasArchive/"+apuser.getOrganization()+"/"+apuser.getFullname();
+ org.vamsas.client.simpleclient.VamsasArchive va = new org.vamsas.client.simpleclient.VamsasArchive(backup, true);
+ VamsasArchiveReader vread = va.getOriginalArchiveReader();
+ if (vread.getAppdataStream(appdata_ref)!=null) {
+ int i=0;
+ while (vread.getAppdataStream(appdata_ref+"/"+Integer.toString(++i))!=null)
+ ;
+ appdata_ref+="/"+Integer.toString(i);
+ }
+ log.info("Writing an apdata reference using AppDataStream interface.");
+ apuser.setAppDataChoice(new AppDataChoice());
+ apuser.getAppDataChoice().setDataReference(appdata_ref);
+ ObjectOutputStream apdos = new ObjectOutputStream(va.getAppDataStream(appdata_ref));
+ apdos.writeObject(appdata_ref);
+ apdos.writeObject(apuser); // some random content
+ apdos.close(); // this should *not* close the archive!
+ log.info("Preparing to write new document.");
+ doc.addApplicationData(appdata);
+ doc.addVAMSAS(Core.getDemoVamsas());
+ PrintWriter vxml = varchive.getDocumentOutputStream();
+ doc.marshal(vxml);
+ // TODO: verify that the vxml stream close method overridden ?
+ va.closeArchive();
+ log.info("Dump of new vamsas document :");
+ va = new org.vamsas.client.simpleclient.VamsasArchive(backup, true); // TODO - refactor Reader class to have deserializing helpers
+ ArchiveReports.reportDocument(va.getVamsasDocument(), va.getOriginalArchiveReader(), true);
backup.delete(); // tidy up
}
- } catch (Exception e) {
+ log.info("Cancelling write to original archive "+av);
+ varchive.cancelArchive();
+
+ } catch (Exception e) {
e.printStackTrace(System.err);
}
}