From: jprocter Date: Tue, 17 Jan 2006 16:41:34 +0000 (+0000) Subject: added and tested simple document handling and update with test.ArchiveWriter. X-Git-Tag: Release_0.2~378 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=9778c09c639e26835897ca55d89c9ba65382eec3;p=vamsas.git added and tested simple document handling and update with test.ArchiveWriter. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@142 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/org/vamsas/client/simpleclient/IdFactory.java b/src/org/vamsas/client/simpleclient/IdFactory.java index 79ec47c..36fd3bc 100644 --- a/src/org/vamsas/client/simpleclient/IdFactory.java +++ b/src/org/vamsas/client/simpleclient/IdFactory.java @@ -133,6 +133,6 @@ public class IdFactory extends VorbaIdFactory { 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.")); } } diff --git a/src/org/vamsas/client/simpleclient/SimpleDocument.java b/src/org/vamsas/client/simpleclient/SimpleDocument.java new file mode 100644 index 0000000..1fc8fdc --- /dev/null +++ b/src/org/vamsas/client/simpleclient/SimpleDocument.java @@ -0,0 +1,150 @@ +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" - // TODO: VAMSAS: decide on machine readable info embedding in provenance should be done - +ths.original+""), 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); } } diff --git a/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java b/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java index f56ed40..b00ebce 100644 --- a/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java +++ b/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java @@ -3,7 +3,9 @@ package org.vamsas.client.simpleclient; 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; @@ -149,4 +151,24 @@ public class VamsasArchiveReader { } } } + + /** + * 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; + } } diff --git a/src/org/vamsas/objects/utils/AppDataReference.java b/src/org/vamsas/objects/utils/AppDataReference.java index 2f1bc78..ecf9a43 100644 --- a/src/org/vamsas/objects/utils/AppDataReference.java +++ b/src/org/vamsas/objects/utils/AppDataReference.java @@ -2,12 +2,36 @@ * */ 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; q0) + return apdrefs; + } + return null; + } } diff --git a/src/org/vamsas/test/simpleclient/VamsasArchive.java b/src/org/vamsas/test/simpleclient/VamsasArchive.java index c023a24..3f0da4c 100644 --- a/src/org/vamsas/test/simpleclient/VamsasArchive.java +++ b/src/org/vamsas/test/simpleclient/VamsasArchive.java @@ -5,6 +5,7 @@ import java.io.ObjectOutputStream; 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; @@ -17,7 +18,74 @@ public class VamsasArchive { * 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 { @@ -45,61 +113,57 @@ public class VamsasArchive { 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); }