clientname="org.vamsas.client.simpleclient.IdFactory";
return new IdFactory(new SessionHandle("dummy.session"),
new ClientHandle(clientname,VersionEntries.latestVersion()),
- new UserHandle("arnoldUser", "none"));
+ new UserHandle(clientname, "Arnold User's Inc."));
}
}
--- /dev/null
+package org.vamsas.client.simpleclient;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Vector;
+import java.util.jar.JarEntry;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.VorbaIdFactory;
+import org.vamsas.client.VorbaXmlBinder;
+import org.vamsas.client.object;
+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.objects.utils.AppDataReference;
+import org.vamsas.objects.utils.DocumentStuff;
+import org.vamsas.objects.utils.ProvenanceStuff;
+import org.vamsas.objects.utils.document.VersionEntries;
+
+/**
+ * holds static vamsasDocument from XML routines and
+ * state objects for a particular unmarshalled Document instance.
+ * @author jimp
+ *
+ */
+public class SimpleDocument {
+ VorbaIdFactory vorba;
+ private static Log log = LogFactory.getLog(SimpleDocument.class);
+
+ private VorbaIdFactory makeDefaultFactory(String name) {
+ return IdFactory.getDummyFactory(name);
+ }
+
+ public SimpleDocument(String name) {
+ vorba = makeDefaultFactory(name);
+ }
+ public SimpleDocument(VorbaIdFactory Vorba) {
+ if (Vorba!=null)
+ vorba = Vorba;
+ else
+ log.error("Invalid SimpleDocument construction - no VorbaIdFactory defined!");
+ }
+ /**
+ * @return Returns the vorba.
+ */
+ public VorbaIdFactory getVorba() {
+ return vorba;
+ }
+ /**
+ * @param vorba The vorba to set.
+ */
+ public void setVorba(VorbaIdFactory vorba) {
+ this.vorba = vorba;
+ }
+
+ /**
+ * Uses VorbaXmlBinder to retrieve the VamsasDocument from the given stream
+ */
+
+ public VamsasDocument getVamsasDocument(VamsasArchiveReader oReader) throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ if (oReader!=null) {
+ // check the factory
+ if (vorba==null) {
+ log.error("Invalid SimpleDocument construction - no VorbaIdFactory defined!");
+ return null;
+ }
+
+ if (oReader.isValid()) {
+ // Read vamsasDocument.xsd instance
+ InputStreamReader vdoc = new InputStreamReader(oReader.getVamsasDocumentStream());
+ Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vdoc, vorba, new VamsasDocument());
+ if (unmarsh==null)
+ log.fatal("Couldn't unmarshall document!");
+
+ object vobjs = (object) unmarsh[0];
+ if (vobjs!=null) {
+ VamsasDocument doc=(VamsasDocument) vobjs;
+ if (doc!=null)
+ return doc;
+ }
+ log.debug("Found no VamsasDocument object in properly formatted Vamsas Archive.");
+ } else {
+ // deprecated data handler (vamsas.xsd instance)
+ InputStream vxmlis = oReader.getVamsasXmlStream();
+ if (vxmlis!=null) { // Might be an old vamsas file.
+ BufferedInputStream ixml = new BufferedInputStream(oReader.getVamsasXmlStream());
+ InputStreamReader vxml = new InputStreamReader(ixml);
+ Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vxml, vorba, new VAMSAS());
+
+ if (unmarsh==null)
+ log.fatal("Couldn't unmarshall document!");
+
+ VAMSAS root[]= new VAMSAS[] { null};
+ root[0] = (VAMSAS) unmarsh[0];
+
+ if (root[0]==null) {
+ log.debug("Found no VAMSAS object in VamsasXML stream.");
+ } else {
+ log.debug("Making new VamsasDocument from VamsasXML stream.");
+ VamsasDocument doc = DocumentStuff.newVamsasDocument(root,
+ ProvenanceStuff.newProvenance(
+ vorba.getUserHandle().getFullName(),
+ "Vamsas Document constructed from vamsas.xml"), VersionEntries.ALPHA_VERSION);
+ // TODO: VAMSAS: decide on 'system' operations provenance form
+ // TODO: implement classes for translating Vorba properties into provenance user fields.
+ // TODO: VAMSAS: decide on machine readable info embedding in provenance should be done
+ root[0]=null;
+ root=null;
+ return doc;
+ }
+ }
+ }
+ }
+ // otherwise - there was no valid original document to read.
+ return null;
+ }
+ /**
+ * Extract all jarEntries in an archive referenced by the vamsas document
+ * TODO: LATER: a family of methods for finding extraneous jarEntries , and invalid appDataReferences
+ * @param doc
+ * @param oReader
+ * @return array of the subset of JarEntry names that are referenced in doc
+ */
+ public Vector getReferencedEntries(VamsasDocument doc, VamsasArchiveReader oReader) {
+ if (oReader==null)
+ return null;
+ if (doc==null) {
+ try { doc = getVamsasDocument(oReader); }
+ catch (Exception e) { log.warn("Failed to get document from "+oReader.jfile.getName()); };
+ }
+ Vector docrefs = AppDataReference.getAppDataReferences(doc);
+ Vector entries = oReader.getExtraEntries();
+ if (entries!=null && docrefs.size()>0) {
+ int i=0, j=entries.size();
+ do {
+ if (!docrefs.contains(entries.get(i))) {
+ entries.remove(i);
+ j--;
+ } else
+ i++;
+ } while (i<j);
+ }
+ return entries;
+ }
+}
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import org.vamsas.objects.core.ApplicationData;
import org.vamsas.objects.core.VAMSAS;
import org.vamsas.objects.core.VamsasDocument;
+import org.vamsas.objects.utils.AppDataReference;
import org.vamsas.objects.utils.DocumentStuff;
import org.vamsas.objects.utils.ProvenanceStuff;
import org.vamsas.objects.utils.document.VersionEntries;
/**
* called after archive is written to put file in its final place
- * TODO: FINISH ?? original should have sessionFile, and archive should also have sessionFile
*/
private void updateOriginal() {
if (!virginArchive) {
return true;
}
/**
+ * transfers any AppDataReferences existing in the old document
+ * that haven't already been transferred to the new one
+ * TODO: LATER: do the same for transfers requiring a namechange - more document dependent.
+ * @return true if data was transferred.
+ */
+ public boolean transferRemainingAppDatas() throws IOException {
+ boolean transfered=false;
+ if (original==null || !original.exists()) {
+ log.warn("No backup archive exists.");
+ return false;
+ }
+ accessOriginal();
+
+ if (getVorba()!=null) {
+ Vector originalRefs=null;
+ try {
+ originalRefs = vorba.getReferencedEntries(getVamsasDocument(), getOriginalArchiveReader());
+ } catch (Exception e) {
+ log.warn("Problems accessing original document entries!",e);
+ }
+ if (originalRefs!=null) {
+ Iterator ref = originalRefs.iterator();
+ while (ref.hasNext()) {
+ String oldentry = (String) ref.next();
+ if (oldentry!=null && !entries.containsKey(oldentry)) {
+ log.debug("Transferring remaining entry '"+oldentry+"'");
+ transfered |= transferAppDataEntry(oldentry);
+ }
+ }
+ }
+ }
+ return transfered;
+ }
+ /**
* 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
}
return null;
}
- protected VorbaIdFactory vorba = null;
+ protected SimpleDocument vorba = null;
/**
* @return Returns the current VorbaIdFactory for the archive.
*/
public VorbaIdFactory getVorba() {
- return vorba;
+ if (vorba==null)
+ vorba = new SimpleDocument("simpleclient.VamsasArchive");
+ return vorba.getVorba();
}
/**
* @param vorba the VorbaIdFactory to use for accessing vamsas objects.
*/
- public void setVorba(VorbaIdFactory vorba) {
- this.vorba = vorba;
+ public void setVorba(VorbaIdFactory Vorba) {
+ if (Vorba!=null) {
+ if (vorba==null)
+ vorba = new SimpleDocument(Vorba);
+ else
+ vorba.setVorba(Vorba);
+ } else
+ getVorba();
}
/**
* @throws org.exolab.castor.xml.MarshalException
* @throws org.exolab.castor.xml.ValidationException
*/
+ private VamsasDocument _doc=null;
public VamsasDocument getVamsasDocument() throws IOException,
org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
- VamsasDocument doc = getOriginalVamsasDocument(this, getVorba());
- if (doc!=null)
- return doc;
+ if (_doc!=null)
+ return _doc;
+ _doc = getOriginalVamsasDocument(this, getVorba());
+ if (_doc!=null)
+ return _doc;
// Create a new document and return it
- doc = DocumentStuff.newVamsasDocument(new VAMSAS[] { new VAMSAS()},
+ _doc = DocumentStuff.newVamsasDocument(new VAMSAS[] { new VAMSAS()},
ProvenanceStuff.newProvenance("org.vamsas.simpleclient.VamsasArchive", "Created new empty document")
, VersionEntries.latestVersion());
- return doc;
+ return _doc;
}
/**
* Access the original vamsas document for a VamsasArchive class, and return it.
org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
return VamsasArchive.getOriginalVamsasDocument(ths, null);
}
- private VorbaIdFactory makeDefaultFactory(VorbaIdFactory vorba) {
- if (vorba==null) {
- vorba = getVorba();
- if (vorba==null) {
- vorba = IdFactory.getDummyFactory("simpleclient.VamsasArchive");
- setVorba(vorba); // save for later use
- }
- }
- return vorba;
- }
+
/**
* Uses VorbaXmlBinder to retrieve the VamsasDocument from the original archive referred to by ths
* @param ths
org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
VamsasArchiveReader oReader = ths.getOriginalArchiveReader();
if (oReader!=null) {
- // check the factory
- vorba = ths.makeDefaultFactory(vorba);
- if (oReader.isValid()) {
- InputStreamReader vdoc = new InputStreamReader(oReader.getVamsasDocumentStream());
- Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vdoc, vorba, new VamsasDocument());
- if (unmarsh==null)
- log.fatal("Couldn't unmarshall document!");
-
- object vobjs = (object) unmarsh[0];
- if (vobjs!=null) {
- VamsasDocument doc=(VamsasDocument) vobjs;
- if (doc!=null)
- return doc;
- }
- log.debug("Found no VamsasDocument object in properly formatted Vamsas Archive.");
- } else {
- // deprecated data handler
- InputStream vxmlis = oReader.getVamsasXmlStream();
- if (vxmlis!=null) { // Might be an old vamsas file.
- BufferedInputStream ixml = new BufferedInputStream(oReader.getVamsasXmlStream());
- InputStreamReader vxml = new InputStreamReader(ixml);
- Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vxml, vorba, new VAMSAS());
-
- if (unmarsh==null)
- log.fatal("Couldn't unmarshall document!");
-
- VAMSAS root[]= new VAMSAS[] { null};
- root[0] = (VAMSAS) unmarsh[0];
-
- if (root[0]==null) {
- log.debug("Found no VAMSAS object in VamsasXML stream.");
- } else {
- log.debug("Making new VamsasDocument from VamsasXML stream.");
- VamsasDocument doc = DocumentStuff.newVamsasDocument(root,
- ProvenanceStuff.newProvenance(
- "org.vamsas.simpleclient.VamsasArchive", // TODO: VAMSAS: decide on 'system' operations provenance form
- "Vamsas Document constructed from vamsas.xml in <file>"
- // TODO: VAMSAS: decide on machine readable info embedding in provenance should be done
- +ths.original+"</file>"), VersionEntries.ALPHA_VERSION);
- root[0]=null;
- root=null;
- return doc;
- }
- }
- }
+ ths.setVorba(vorba);
+ return ths.vorba.getVamsasDocument(oReader);
}
// otherwise - there was no valid original document to read.
return null;
}
+
public void putVamsasDocument(VamsasDocument doc) throws IOException,
org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
- VorbaIdFactory vorba = makeDefaultFactory(getVorba());
+ 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);
}
}
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Enumeration;
import java.util.Iterator;
+import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
}
}
}
+
+ /**
+ * returns all entries not matching the filespec of a vamsas xml entry
+ * @return array of entries.
+ */
+ public Vector getExtraEntries() {
+ if (jfile==null || !isValid())
+ return null;
+ Enumeration entries = jfile.entries();
+ if (entries!=null && entries.hasMoreElements()) {
+ Vector e = new Vector();
+ 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;
+ }
+ return null;
+ }
}
*
*/
package org.vamsas.objects.utils;
+import java.util.Vector;
+import org.vamsas.objects.core.*;
/**
- * Form and validation for ApplicationData references in
+ * Form, accessors and validation for ApplicationData references in
* vamsas document.
- * TODO: extend XML Schema to properly validate against the same forms required by this class
+ * TODO: LATER:extend XML Schema to properly validate against the same forms required by this class
+ * TODO: implement methods for searching appData structure for particular combinations of client and user data
+ * TODO: VAMSAS: URNS for appDatas are supposed to be unique, aren't they ?
*/
public class AppDataReference {
-
+ static public Vector getAppDataReferences(VamsasDocument doc) {
+ if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
+ Vector apdrefs = new Vector();
+ ApplicationData[] appdatas = doc.getApplicationData();
+ for (int q=0; q<appdatas.length; q++) {
+ String refstring=appdatas[q].getDataReference();
+ if (refstring!=null)
+ apdrefs.add(refstring);
+ User users[] = appdatas[q].getUser();
+
+ if (users!=null)
+ for (int u=0; u<users.length; u++)
+ refstring=users[u].getDataReference();
+ if (refstring!=null)
+ apdrefs.add(new String(refstring)); // avoid referencing.
+ }
+ if (apdrefs.size()>0)
+ return apdrefs;
+ }
+ return null;
+ }
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.simpleclient.SimpleDocument;
import org.vamsas.client.simpleclient.VamsasArchiveReader;
import org.vamsas.objects.core.ApplicationData;
import org.vamsas.objects.core.User;
* test the org.vamsas.simpleclient.vamsasArchive class
*/
static Log log = LogFactory.getLog(VamsasArchive.class);
-
+ public static ApplicationData makeDemoAppdata(org.vamsas.client.simpleclient.VamsasArchive va, String apname, String userName, String userOrg) {
+ if (va==null)
+ return null;
+ VamsasArchiveReader vread=null;
+ try {
+ vread = va.getOriginalArchiveReader();
+ }
+ catch (Exception e) {
+ log.error("Failed to get original archive reader!",e);
+ return null;
+ }
+ ApplicationData appdata = new ApplicationData();
+ appdata.setName("org.vamsas.test.simpleclient.VamsasArchive");
+ appdata.setData(new String("this is some test data.").getBytes());
+ User apuser = new User();
+ apuser.setFullname(userName);
+ apuser.setOrganization(userOrg);
+ String appdata_ref = "vamsas:"+apname+"/"+apuser.getOrganization()+"/"+apuser.getFullname();
+ SimpleDocument sdoc = new SimpleDocument("test.simpleclient.VamsasArchive");
+ if (vread!=null) {
+ VamsasDocument orignalnew;
+ try {
+ orignalnew = sdoc.getVamsasDocument(vread);
+ log.info("*** Dump follows ***");
+
+ ArchiveReports.reportDocument(orignalnew, vread, false);
+ log.info("*** Dump precedes ***");
+ } catch (Exception e) {
+ log.info("makeDemoAppdata: Problems accessing original document");
+ }
+
+ log.info("Reading (and avoiding references to) original data");
+ if (vread.getAppdataStream(appdata_ref)!=null) {
+ // transfer over
+ try {
+ va.transferAppDataEntry(appdata_ref);
+ } catch (Exception e) {
+ log.warn("Exception when transferring appdata reference : "+appdata_ref, e);
+ }
+ int i=0;
+ while (vread.getAppdataStream(appdata_ref+"/"+Integer.toString(++i))!=null) {
+ try {
+ // copy over another duplicate.
+ va.transferAppDataEntry(appdata_ref+"/"+Integer.toString(i));
+ } catch (Exception e) {
+ log.warn("Exception when transferring appdata reference : "+appdata_ref, e);
+ }
+ }
+ // this one must be unique!
+ appdata_ref+="/"+Integer.toString(i);
+ }
+ }
+
+ log.info("Adding new data stuff.");
+ log.info("Writing an apdata reference using AppDataStream interface.");
+ apuser.setDataReference(appdata_ref);
+ appdata.addUser(apuser);
+ appdata.setVersion("noggin");
+ appdata.setUrn("program:/the.nog/");
+ try {
+ ObjectOutputStream ost = new ObjectOutputStream(va.getAppDataStream(appdata_ref));
+ ost.writeObject(appdata);
+ ost.close();
+ } catch (Exception e) {
+ log.warn("Couldn't write appdata reference "+appdata_ref);
+ }
+ return appdata;
+ }
public static void main(String args[]) {
try {
log.info(av+" is a New Archive.");
else
log.info(av+" has been backed up as "+backup);
- if (backup!=null) {
- File newf=new File(av.getAbsolutePath()+"_new.zip");
- log.info("Now writing new Archive "+newf.getAbsolutePath());
- org.vamsas.client.simpleclient.VamsasArchive va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true);
- // open another and...
- ApplicationData appdata = new ApplicationData();
- appdata.setName("org.vamsas.test.simpleclient.VamsasArchive");
- appdata.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();
-
-
- VamsasArchiveReader vread = va.getOriginalArchiveReader();
-
- if (vread!=null) {
- log.info("Reading (and avoiding references to) original data in "+newf);
- if (vread.getAppdataStream(appdata_ref)!=null) {
- int i=0;
- while (vread.getAppdataStream(appdata_ref+"/"+Integer.toString(++i))!=null)
- ;
- appdata_ref+="/"+Integer.toString(i);
- }
+ File newf=new File(av.getAbsolutePath()+"_new.zip");
+ if (newf.exists()) {
+ int q=1;
+ do {
+ newf=new File(av.getAbsolutePath()+"_"+q+++"_new.zip");
}
-
- log.info("Adding new data stuff.");
- log.info("Writing an apdata reference using AppDataStream interface.");
- apuser.setDataReference(appdata_ref);
- appdata.addUser(apuser);
- appdata.setVersion("noggin");
- appdata.setUrn("program:/the.nog/");
- 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("Written Appdata Stream");
- log.info("Preparing to write new document.");
- doc.addApplicationData(appdata);
- doc.addVAMSAS(Core.getDemoVamsas());
- // TODO: ensure all jar entries are transferred over (at least those that are referenced - make a flag for 'tidy zip manifest'.
- va.putVamsasDocument(doc); // gets stream and puts it.
- // 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(newf, true); // TODO - refactor Reader class to have deserializing helpers
- ArchiveReports.reportDocument(va.getVamsasDocument(), va.getOriginalArchiveReader(), true);
- log.info("Cancelling write (which was necessary to dump new vamsas document!)");
- va.cancelArchive();
- // backup.delete(); // tidy up
- }
+ while (newf.exists());
+ }
+ log.info("Now writing new Archive "+newf.getAbsolutePath());
+ org.vamsas.client.simpleclient.VamsasArchive va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true);
+ // open another and...
+ ApplicationData appdata = makeDemoAppdata(va,
+ "org.vamsas.test.simpleclient.VamsasArchive", "arnold Bugger esq", "disOrganised");
+ log.info("Preparing to write new document.");
+ doc.addApplicationData(appdata);
+ doc.addVAMSAS(Core.getDemoVamsas());
+ // TODO: ensure all jar entries are transferred over (at least those that are referenced - make a flag for 'tidy zip manifest'.
+ va.putVamsasDocument(doc); // gets stream and puts it.
+ // TODO: verify that the vxml stream close method overridden ?
+ va.closeArchive();
+
+ log.info("Dump of new vamsas document :");
+ log.info("Testing update: ");
+ VamsasArchiveReader vreader = new VamsasArchiveReader(newf);
+ SimpleDocument sdoc = new SimpleDocument("testing new vamsas write");
+ ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true);
+
+ // backup.delete(); // tidy up
+
log.info("Now Cancelling write to original archive "+av);
- varchive.cancelArchive();
-
+ if (varchive.cancelArchive())
+ log.info("Successfully cancelled.");
+ else
+ log.info("Didn't cancel.");
+
+ log.info("Now testing archive update.");
+ va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true); // TODO - refactor Reader class to have deserializing helpers
+ doc = va.getVamsasDocument();
+ doc.addVAMSAS(Core.getDemoVamsas());
+ doc.addApplicationData(makeDemoAppdata(va,
+ "org.vamsas.test.simpleclient.VamsasArchive", "another old Bugger esq", "rescinded"));
+ if (va.transferRemainingAppDatas())
+ log.info("Remain appdatas were transferred.");
+ else
+ log.warn("No appdatas were transferred. This is wrong.");
+ va.putVamsasDocument(doc);
+ va.closeArchive();
+ log.info("Testing update: ");
+ vreader = new VamsasArchiveReader(newf);
+ sdoc = new SimpleDocument("testing vamsas update");
+ ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true);
} catch (Exception e) {
e.printStackTrace(System.err);
}