--- /dev/null
+/**
+ *
+ */
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
+import java.util.Hashtable;
+import java.util.Vector;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.exolab.castor.xml.MarshalException;
+import org.exolab.castor.xml.ValidationException;
+import org.vamsas.test.objects.Core;
+
+import uk.ac.vamsas.client.ClientHandle;
+import uk.ac.vamsas.client.IVorbaIdFactory;
+import uk.ac.vamsas.client.SessionHandle;
+import uk.ac.vamsas.client.UserHandle;
+import uk.ac.vamsas.client.Vobject;
+import uk.ac.vamsas.client.VorbaId;
+import uk.ac.vamsas.client.simpleclient.AppDataOutputStream;
+import uk.ac.vamsas.client.simpleclient.FileWatcher;
+import uk.ac.vamsas.client.simpleclient.IdFactory;
+import uk.ac.vamsas.client.simpleclient.SessionFile;
+import uk.ac.vamsas.client.simpleclient.SimpleDocBinding;
+import uk.ac.vamsas.client.simpleclient.SimpleDocument;
+import uk.ac.vamsas.client.simpleclient.VamsasArchive;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.client.simpleclient.VamsasFile;
+import uk.ac.vamsas.objects.core.AppData;
+import uk.ac.vamsas.objects.core.ApplicationData;
+import uk.ac.vamsas.objects.core.User;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+import uk.ac.vamsas.objects.utils.AppDataReference;
+import uk.ac.vamsas.objects.utils.DocumentStuff;
+import uk.ac.vamsas.objects.utils.ProvenanceStuff;
+import uk.ac.vamsas.objects.utils.SeqSet;
+import uk.ac.vamsas.objects.utils.document.VersionEntries;
+
+/**
+ * @author jimp
+ * test the VamsasFile routines for watching, reading and updating a vamsas document jar file.
+ * simple document access base class.
+ */
+public class ArchiveClient extends IdFactory {
+
+ private Log log = LogFactory.getLog(ArchiveClient.class);
+ // protected UserHandle user=null;
+ // protected ClientHandle me = new ClientHandle("ArchiveClient","0.01");
+ VamsasFile vsess;
+
+ /**
+ * @param user
+ * @param vsess
+ */
+ public ArchiveClient(UserHandle user, VamsasFile vsess) {
+ super(new SessionHandle("vamsasfile://"+vsess.getVamsasFile()), new ClientHandle("ArchiveClient","0.01"), user);
+ this.vsess = vsess;
+ valid();
+ }
+ private void _openVsess(File vsess) {
+ try {
+ this.vsess = new VamsasFile(vsess);
+ }
+ catch (Exception e) {
+ log.error("Couldn't open session for file "+vsess,e);
+ this.vsess = null;
+ }
+ }
+ public ArchiveClient(String username, String organization, File vsess) {
+ super(new SessionHandle("vamsasfile://"+vsess), new ClientHandle("ArchiveClient","0.01"), new UserHandle(username, organization));
+ _openVsess(vsess);
+ valid();
+ }
+ public ArchiveClient(String username, String organization, String clientName, String clientVersion, File vsess) {
+ super(new SessionHandle("vamsasfile://"+vsess), new ClientHandle(clientName, clientVersion), new UserHandle(username, organization));
+ _openVsess(vsess);
+ valid();
+ }
+ public void valid() {
+ if (vsess==null)
+ throw new Error("ArchiveClient instance is invalid!.");
+ }
+ /**
+ * set this to false if watch loop should end immediately
+ */
+ protected boolean watchForChange=true;
+ public static int WATCH_SLEEP=300;
+ /**
+ * watch the document file for updates.
+ * @param time - length of time to watch for.
+ * @return read only IO interface for session document.
+ */
+ public ClientDoc watch(long time) {
+ valid();
+ vsess.unLock(); // doh.
+ FileWatcher watcher = new FileWatcher(vsess.getVamsasFile());
+ // watcher.setState();
+ watchForChange=true;
+ long endtime=System.currentTimeMillis()+time;
+ try {
+ uk.ac.vamsas.client.simpleclient.Lock doclock;
+ do {
+ doclock=watcher.getChangedState();
+ if (doclock==null)
+ Thread.sleep(WATCH_SLEEP);
+ } while (watchForChange && doclock==null && (time==0 || endtime>System.currentTimeMillis())); // tuning.
+ if (doclock==null)
+ return null;
+ else {
+ return getUpdateable(vsess.getLock(doclock));
+ /*VamsasArchiveReader varc = new VamsasArchiveReader(vsess.getVamsasFile());
+ return _getReadonly(varc);*/
+ }
+ } catch (Exception e) {
+ log.error("Whilst watching file "+vsess.getVamsasFile(), e);
+ }
+ return null;
+ }
+
+ // from ClientDocument.getClientAppdata
+ private AppData[] getAppData(VamsasDocument doc) {
+ if (doc==null) {
+ log.debug("extractAppData called for null document object");
+ return null;
+ }
+ AppData appsGlobal=null, usersData=null;
+ Vector apldataset = AppDataReference.getUserandApplicationsData(
+ doc, this.getUserHandle(), this.getClientHandle());
+ if (apldataset!=null) {
+ if (apldataset.size()>0) {
+ AppData clientdat = (AppData) apldataset.get(0);
+ if (clientdat instanceof ApplicationData) {
+ appsGlobal = (ApplicationData) clientdat;
+ if (apldataset.size()>1) {
+ clientdat = (AppData) apldataset.get(1);
+ if (clientdat instanceof User) {
+ usersData = (User) clientdat;
+ }
+ if (apldataset.size()>2)
+ log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
+ }
+ } else {
+ log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
+ }
+ apldataset.removeAllElements(); // destroy references.
+ }
+ }
+ return new AppData[] { appsGlobal, usersData};
+ }
+
+ protected ClientDoc _getReadonly(VamsasArchiveReader vreader) throws IOException, ValidationException, MarshalException {
+ valid();
+ if (vreader!=null) {
+ SimpleDocBinding docb = new SimpleDocBinding();
+ docb.setVorba(this);
+ VamsasDocument d;
+ d = docb.getVamsasDocument(vreader);
+
+ if (d!=null) {
+ ClientDoc creader = new ClientDoc(d, null, vreader, getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
+ return creader;
+ }
+ }
+ return null;
+ }
+ /**
+ * from SimpleClient
+ * @return user field for a provenance entry
+ */
+ protected String getProvenanceUser() {
+ return new String(getUserHandle().getFullName()+" ["+getClientHandle().getClientUrn()+"]");
+ }
+
+ public ClientDoc getUpdateable() {
+ return getUpdateable(null);
+ }
+ public ClientDoc getUpdateable(uk.ac.vamsas.client.simpleclient.Lock lock) {
+ getVorbaIdHash().clear();
+ valid();
+ try {
+ // patiently wait for a lock on the document.
+ long tries=5000;
+ while (lock==null && ((lock=vsess.getLock())==null || !lock.isLocked()) && --tries>0) {
+// Thread.sleep(1);
+ log.debug("Trying to get a document lock for the "+tries+"'th time.");
+ }
+ VamsasArchive varc = new VamsasArchive(vsess, true, false); // read archive, write as vamsasDocument, don't erase original contents.
+ varc.setVorba(this);
+ VamsasDocument d = varc.getVamsasDocument(getProvenanceUser(), "Created new document.", VersionEntries.latestVersion()); // VAMSAS: provenance user and client combined
+
+ if (d==null) {
+ log.warn("Backing out from opening a VamsasArchive writable IO session");
+ varc.cancelArchive();
+ return null;
+ }
+ ClientDoc cdoc = new ClientDoc(d, varc, varc.getOriginalArchiveReader(), getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
+ return cdoc;
+ // do appHandle?
+ } catch (Exception e) {
+ log.error("Failed to get Updateable version of "+vsess.getVamsasFile(), e);
+ }
+ return null;
+ }
+ /**
+ * trust client to not do anything stupid to the document roots which will now be written to the archive.
+ * @param cdoc
+ * @return true if write was a success.
+ */
+ public boolean doUpdate(ClientDoc cdoc) {
+ valid();
+ if (cdoc==null) {
+ log.warn("Invalid ClientDoc passed to uk.ac.vamsas.test.simpleclient.doUpdate()");
+ return false;
+ }
+ if (cdoc.iohandler==null) {
+ log.warn("Read only ClientDoc object passed to uk.ac.vamsas.test.simpleclient.doUpdate()");
+ return false;
+ }
+ if (cdoc.iohandler.getVorba()!=this) {
+ log.error("Mismatch between ClientDoc instances and ArchiveClient instances!");
+ return false;
+ }
+ try {
+ // do any appDatas first.
+ if (cdoc.iohandler.transferRemainingAppDatas())
+ log.debug("Remaining appdatas were transfered.");
+ cdoc.updateDocumentRoots();
+ cdoc.iohandler.putVamsasDocument(cdoc.doc);
+ cdoc.iohandler.closeArchive();
+ this.extantids.clear();// we forget our ids after we close the document.
+ cdoc.iohandler=null;
+ cdoc = null;
+ vsess.unLock();
+ } catch (Exception e) {
+ log.warn("While updating archive in "+vsess.getVamsasFile(),e);
+ return false;
+ }
+ return true;
+ }
+ /**
+ * @param args
+ */
+ public static void usage() {
+ throw new Error("Usage: Username Organization VamsasFile [command,args]*");
+ }
+ public static void main(String[] args) {
+ // really simple.
+ if (args.length<3)
+ usage();
+
+ ArchiveClient client = new ArchiveClient(args[0],args[1], new File(args[2]));
+ ClientDoc cdoc=null;
+ // sanity test.
+ try {
+ cdoc = client.getUpdateable();
+ // ArchiveReports.reportDocument(cdoc.doc, cdoc.getReader(), true, System.out);
+ System.out.println("Report Roots :");
+ ArchiveReports.rootReport(cdoc.getVamsasRoots(), true, System.out);
+ cdoc.addVamsasRoot(Core.getDemoVamsas());
+ System.out.println("Doing update.");
+ client.doUpdate(cdoc);
+ cdoc.closeDoc();
+ cdoc = null;
+ int u=5;
+ while (--u>0) {
+ System.out.println("Watch for more... ("+u+" left)");
+ ClientDoc ucdoc = client.watch(0000);
+ if (ucdoc!=null) {
+ System.out.println("****\nUpdate detected at "+new Date());
+ ArchiveReports.reportDocument(ucdoc.doc, ucdoc.getReader(), true, System.out);
+ ucdoc.closeDoc();
+ ucdoc=null;
+ } else {
+ System.out.println("!!!! Null document update detected at "+new Date());
+ }
+ }
+ }
+ catch (Exception e) {
+ client.log.error("Broken!", e);
+ }
+ System.out.println("Finished at "+new Date());
+ }
+ public uk.ac.vamsas.client.Vobject getObject(VorbaId id) {
+ Hashtable idhash = this.getVorbaIdHash();
+ if (idhash!=null && idhash.containsKey(id))
+ return (Vobject) idhash.get(id);
+ return null;
+ }
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.objects.core.VAMSAS;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+
+public class ArchiveReader {
+ /**
+ * tests VamsasArchiveReader archive reader on a vamsas jar file
+ * @param args
+ */
+ public static void main(String args[]) {
+
+ try {
+ File av = new File(args[0]);
+ VamsasArchiveReader var = new VamsasArchiveReader(av);
+ VAMSAS roots[]=null;
+ if (var.isValid()) {
+ InputStreamReader vdoc = new InputStreamReader(var.getVamsasDocumentStream());
+ VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
+ if (ArchiveReports.reportDocument(doc, var, true, System.out)) {
+ roots = doc.getVAMSAS();
+ }
+ } else {
+ InputStream vxmlis = var.getVamsasXmlStream();
+
+ if (vxmlis!=null) { // Might be an old vamsas file.
+ BufferedInputStream ixml = new BufferedInputStream(var.getVamsasXmlStream());
+ InputStreamReader vxml = new InputStreamReader(ixml);
+ VAMSAS root;
+ // unmarshal seems to always close the stream (should check this)
+ if ((root = VAMSAS.unmarshal(vxml))!=null) {
+ System.out.println("Read a root.");
+ roots = new VAMSAS[1];
+ roots[0] = root;
+ }
+ }
+ }
+ if (!ArchiveReports.rootReport(roots, true, System.out))
+ System.err.print(args[0]+" is not a valid vamsas archive.");
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ }
+ }
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+
+
+import uk.ac.vamsas.client.ClientDocument;
+import uk.ac.vamsas.client.Vobject;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.objects.core.Alignment;
+import uk.ac.vamsas.objects.core.AppData;
+import uk.ac.vamsas.objects.core.ApplicationData;
+import uk.ac.vamsas.objects.core.DataSet;
+import uk.ac.vamsas.objects.core.Entry;
+import uk.ac.vamsas.objects.core.Instance;
+import uk.ac.vamsas.objects.core.Provenance;
+import uk.ac.vamsas.objects.core.Tree;
+import uk.ac.vamsas.objects.core.User;
+import uk.ac.vamsas.objects.core.VAMSAS;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+/**
+ * this class contains static methods for writing info to stdout about a vamsas document
+ * Methods have a 'cascade' switch to indicate if sub-objects should have info printed on them.
+ * Methods return true or false - indicating if the Vobject was valid or not
+ * TODO: LATER: propagate true/false return so that an invalid vamsas Vobject invalidates the whole document
+ * @author jimp
+ *
+ */
+public class ArchiveReports {
+ /**
+ * print an informative summary on a VamsasDocument
+ * @param outstr TODO
+ * @param document - the document itself
+ * @param archive - document source archive for resolving any appData refs
+ * @return
+ */
+ public static boolean reportProvenance(Provenance p, PrintStream outstr) {
+ if (p==null) {
+ outstr.println("No Provenance");
+ return false;
+ }
+ Entry[] pe = p.getEntry();
+ for (int i=0; i<pe.length; i++)
+ outstr.println(pe[i].getDate()+"\t'"+pe[i].getUser()+"'\t"+pe[i].getApp()+"\t'"+pe[i].getAction()+"'");
+ return true;
+ }
+ public static boolean appDataEntryReport(AppData appD, VamsasArchiveReader archive, boolean cascade, PrintStream outstr) {
+ if (appD!=null) {
+ boolean nulldata=false;
+ if (appD.getDataReference()!=null) {
+ String appData=appD.getDataReference();
+ if (appData==null) { outstr.println("Empty DataReference - not valid ?"); }
+ else
+ if (appData.length()>1) {
+ outstr.print("a reference ("+appData+")");
+ InputStream jstrm;
+ if ((jstrm=archive.getAppdataStream(appData))!=null)
+ outstr.println(" which resolves to a JarEntry.");
+ else {
+ outstr.println(" which does not resolve to a JarEntry.");
+ outstr.println("Unresolved appdata reference '"+appData+"'");
+ }
+ } else {
+ nulldata=true;
+ }
+ } else {
+ if (appD.getData()==null)
+ nulldata &= true;
+ else
+ outstr.println("an embedded chunk of "+appD.getData().length+" bytes.");
+ }
+ if (nulldata)
+ outstr.println("Null AppData reference/data chunk.");
+ }
+ return true;
+ }
+
+ public static boolean appDataReport(ApplicationData appD, VamsasArchiveReader archive, boolean cascade, PrintStream outstr) {
+ if (appD!=null) {
+ // Report on root appData
+ appDataEntryReport(appD, archive, cascade, outstr);
+ if (appD.getInstanceCount()>0) {
+ Instance inst[] = appD.getInstance();
+ for (int i=0,j=inst.hashCode(); i<j; i++) {
+ outstr.println("Data for App Instance URN: '"+inst[i].getUrn());
+ appDataEntryReport(inst[i],archive,cascade,outstr);
+ }
+ }
+ if (appD.getUserCount()>0) {
+ User users[] = appD.getUser();
+ for (int i=0,j=users.length; i<j; i++) {
+ outstr.println("Data for User '"+users[i].getFullname()+"' of '"+users[i].getOrganization()+"'");
+ appDataEntryReport(users[i], archive, cascade, outstr);
+ }
+ }
+ }
+
+ return true;
+ }
+ public static boolean reportDocument(VamsasDocument document, VamsasArchiveReader archive, boolean cascade, PrintStream outstr) {
+ if (document!=null) {
+ outstr.println("Vamsas Document version '"+document.getVersion()+"'");
+ reportProvenance(document.getProvenance(), outstr);
+ outstr.print("Document contains "+document.getVAMSASCount()+" VAMSAS Elements and "+document.getApplicationDataCount()+" Application data elements.\n");
+ if (document.getVAMSASCount()>0 && cascade)
+ rootReport(document.getVAMSAS(), true, outstr);
+ if (document.getApplicationDataCount()>0) {
+ outstr.print("There are "+document.getApplicationDataCount()+" ApplicationData references.\n");
+ ApplicationData appd[] = document.getApplicationData();
+ for (int i=0,j=appd.length; i<j; i++) {
+ outstr.print("Application "+i+": '"+appd[i].getName()+"'\nVersion '"+appd[i].getVersion()+"'\n");
+ outstr.print("AppData is :");
+ appDataReport(appd[i], archive, cascade, outstr);
+ }
+
+ }
+ return true;
+ } else {
+ outstr.println("Document Object is null");
+ }
+ return false;
+ }
+
+ /**
+ * summarises all the datasets in a vamsas document.
+ * @param roots
+ * @param cascade TODO
+ * @param outstr TODO
+ * @return
+ */
+ public static boolean rootReport(VAMSAS[] roots, boolean cascade, PrintStream outstr) {
+ if (roots!=null) {
+ for (int i=0; i<roots.length; i++) {
+ VAMSAS r = roots[i];
+ int ds, tr;
+ outstr.print("Vamsas Root "+i+" (id="
+ +((r.getId()!=null) ? r.getId():"<none>")
+ +") contains "+(ds=r.getDataSetCount())+" DataSets, "
+ + (tr=r.getTreeCount())+" Global trees\n");
+ if (cascade) {
+ for (int j=0; j<ds; j++) {
+ outstr.println("Dataset "+j);
+ cascade = datasetReport(r.getDataSet(j), true, outstr) && cascade ;
+ }
+ for (int j=0; j<tr; j++) {
+ outstr.println("Global tree "+j);
+ cascade = treeReport(r.getTree(j), true, outstr) && cascade;
+ }
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+ public static boolean datasetReport(DataSet ds, boolean cascade, PrintStream outstr) {
+ if (cascade)
+ reportProvenance(ds.getProvenance(), outstr);
+ outstr.println("Dataset contains : "+ds.getSequenceCount()
+ +" sequences, "+ds.getAlignmentCount()+" alignments and "+ds.getTreeCount()+" trees.");
+ if (cascade)
+ alignmentReport(ds.getAlignment(), true, outstr);
+ return true;
+ }
+ public static boolean alignmentReport(Alignment[] al, boolean cascade, PrintStream outstr) {
+ boolean val=true;
+ if (al!=null && al.length>0) {
+ for (int i=0; i<al.length; i++) {
+ outstr.println("Alignment "+i+(al[i].isRegistered() ? " ("+al[i].getVorbaId()+")" : " (unregistered)"));
+ if (cascade)
+ reportProvenance(al[i].getProvenance(), outstr);
+ outstr.println("Involves "+al[i].getAlignmentSequenceCount()+" sequences, has "
+ +al[i].getAlignmentAnnotationCount()+" annotations and "+al[i].getTreeCount()+" trees.");
+ if (cascade) {
+ for (int t=0; t<al[i].getTreeCount(); t++)
+ treeReport(al[i].getTree(t), true, outstr);
+ }
+ }
+ }
+ return val;
+ }
+ public static boolean treeReport(Tree t, boolean cascade, PrintStream outstr) {
+ outstr.println("Tree: '"+t.getTitle()+"'");
+ return !cascade || reportProvenance(t.getProvenance(), outstr);
+ }
+
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+
+import uk.ac.vamsas.client.simpleclient.SessionFile;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.client.simpleclient.VamsasFile;
+import uk.ac.vamsas.objects.core.VAMSAS;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+
+public class ArchiveStreamReader {
+ /**
+ * tests VamsasArchiveReader archive reader on a vamsas jar file opened as a stream
+ * @param args
+ */
+ public static void main(String args[]) {
+
+ try {
+ VamsasFile av = new VamsasFile(new File(args[0]));
+ VamsasArchiveReader var = new VamsasArchiveReader(av.getLock());
+ VAMSAS roots[]=null;
+ if (var.isValid()) {
+ InputStreamReader vdoc = new InputStreamReader(var.getVamsasDocumentStream());
+ VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
+ if (ArchiveReports.reportDocument(doc, var, true, System.out)) {
+ roots = doc.getVAMSAS();
+ }
+ } else {
+ InputStream vxmlis = var.getVamsasXmlStream();
+
+ if (vxmlis!=null) { // Might be an old vamsas file.
+ BufferedInputStream ixml = new BufferedInputStream(var.getVamsasXmlStream());
+ InputStreamReader vxml = new InputStreamReader(ixml);
+ VAMSAS root;
+ // unmarshal seems to always close the stream (should check this)
+ if ((root = VAMSAS.unmarshal(vxml))!=null) {
+ System.out.println("Read a root.");
+ roots = new VAMSAS[1];
+ roots[0] = root;
+ }
+ }
+ }
+ if (!ArchiveReports.rootReport(roots, true, System.out))
+ System.err.print(args[0]+" is not a valid vamsas archive.");
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ }
+ }
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.File;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import uk.ac.vamsas.client.ClientHandle;
+import uk.ac.vamsas.client.simpleclient.FileWatcher;
+import uk.ac.vamsas.client.simpleclient.Lock;
+import uk.ac.vamsas.client.simpleclient.SimpleDocument;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.client.simpleclient.VamsasFile;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+/**
+ * demo of archive watching process - should mimic the clientsfileTest watcher/monitor process.
+ * @author jimp
+ *
+ */
+public class ArchiveWatcher {
+ private static Log log = LogFactory.getLog(ArchiveWatcher.class);
+ private static CommandProcessor cproc=new CommandProcessor();
+ static {
+ cproc.addCommand("new", 0, "no args");
+ cproc.addCommand("delete", 0, "no args");
+ cproc.addCommand("watch", 0, "no args");
+ cproc.addCommand("file", 1, "Need vamsas archive as argument.");
+ }
+
+ public static void main(String[] args) {
+ try {
+
+ if (args!=null && args.length>0) {
+ File archive = new File(args[0]);
+ log.info("Watching file "+args[0]);
+ int argc=1;
+ while (argc < args.length) {
+ // vars needed for operations
+ ClientHandle ch;
+ int com = cproc.getCommand(args, argc);
+ argc++;
+ switch (com) {
+ case 0:
+ // new
+ log.info("Doing locked deletion and new-file creation.");
+ {
+ if (!archive.exists())
+ archive.createNewFile();
+ VamsasFile sf = new VamsasFile(archive);
+ Lock l = sf.getLock();
+ archive.delete();
+ archive.createNewFile();
+ sf.unLock();
+ }
+ break;
+ case 1:
+ // delete
+ log.info("Deleting "+archive+" without locking it first.");
+ archive.delete();
+ break;
+ case 2:
+ // watch
+ log.info("Endlessly Watching file "+archive);
+ /* if (!archive.exists())
+ archive.createNewFile();
+ */ // watch the new file... - taken straight from ClientsFileTest
+ FileWatcher w = new FileWatcher(archive);
+ while (true) {
+ // get watcher's lock to ensure state change is fixed for retrieval
+ Lock chlock = w.getChangedState();
+ if (chlock != null) {
+ log.info("Got lock on "+archive+(archive.exists() ? " exists l="+archive.length() : "(non existant)"));
+ if (archive.length()>0) {
+ VamsasArchiveReader vreader = new VamsasArchiveReader(archive);
+ SimpleDocument sdoc = new SimpleDocument("testing vamsas watcher");
+ try {
+ VamsasDocument d = sdoc.getVamsasDocument(vreader);
+ if (d!=null) {
+ ArchiveReports.reportDocument(d, vreader, false, System.out);
+ }
+ System.out.println("Update at "+System.currentTimeMillis()+"\n\n********************************************************\n");
+ } catch (Exception e) {
+ log.error("Unmarshalling failed.",e);
+ }
+ vreader.close();
+ w.setState();
+ }
+ }
+ }
+ // break;
+ case 3: // set file
+ archive = new File(args[argc++]);
+ break;
+ case 4:
+ break;
+ default:
+ log.warn("Unknown command + "+args[argc++]);
+ }
+ }
+ }
+ } catch (Exception e) {
+ log.error(e);
+ }
+
+ }
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Hashtable;
+import java.util.Vector;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+der;
+import uk.ac.vamsas.objects.core.Alignment;
+import uk.ac.vamsas.objects.core.ApplicationData;
+import uk.ac.vamsas.objects.core.Entry;
+import uk.ac.vamsas.objects.core.Instance;
+import uk.ac.vamsas.objects.core.Provenance;
+import uk.ac.vamsas.objects.core.VAMSAS;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+import uk.ac.vamsas.objects.ut
+import uk.ac.vamsas.client.simpleclient.VamsasArchive;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+ils.ProvenanceStuff;
+
+public class ArchiveWriter {
+
+ /**
+ * Test program for writing archive files.
+ * form is ArchiveWriter new/modified argive command list
+ */
+
+ static Log log = LogFactory.getLog(ArchiveWriter.class);
+
+ private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {
+ int i;
+ for (i=0; i<svec1.length; i++)
+ destvec[i] = svec1[i];
+ for (int j=0; j<svec2.length; i++, j++)
+ destvec[i] = svec2[j];
+ }
+ // Merge appDataReferences require transfer of jar entries, perhaps with a renaming of the entry.
+ // Merge appDatas require eventually unique URNS
+ // TODO: merging global appdata from different documents where same app has written them causes conflict
+
+ public static Hashtable hashOfAppDatas(Hashtable ht, Instance[] appdatas) {
+ if (ht==null)
+ ht = new Hashtable();
+ for (int i=0, j=appdatas.length; i<j; i++) {
+ if (!ht.containsKey(appdatas[i].getUrn())) {
+ Hashtable aphash = new Hashtable();
+ ht.put(appdatas[i].getUrn(), aphash);
+ aphash.put(appdatas[i], appdatas[i].getDataReference());
+ } else {
+ // ensure urns and references are unique
+
+
+ }
+
+ }
+ return ht;
+ }
+ /**
+ * safely copies an appData from one archive to another.
+ * @param darc destination archive
+ * @param dest destination document Vobject
+ * @param sarc source archive reader
+ * @param entry application data to be copied from source archive
+ */
+ public static void addAppDataEntry(VamsasArchive darc, VamsasDocument dest, VamsasArchiveReader sarc, ApplicationData entry) {
+ // TODO: fix instances
+ // check uniqueness of instance's[] entry.urn amongst dest.ApplicationData[].getInstances[].urn
+ // check uniqueness of entry.user[].urn amongst dest.ApplicationData[].user[].urn
+ // check uniqueness of entry.user
+ // entry.getAppDataChoice().getData() or getDataReference is unique
+ ApplicationData newo = new ApplicationData();
+ for (int i=0, j=dest.getApplicationDataCount(); i<j; i++) {
+ ApplicationData o = dest.getApplicationData()[i];
+ // ensure new urn is really unique
+ //String urn = entry.getUrn();
+ int v = 1;
+ //while (o.getUrn().equals(urn)) {
+ // urn = entry.getUrn()+v++;
+ // }
+ // uniqueness of urn
+ // check each user ApplicationData
+ // uniqueness (again)
+ // copy over valid objects
+ //
+ }
+ }
+ /**
+ * Copy new datasets and appdatas from one vamsas document to another.
+ * @param darc
+ * @param dest
+ * @param sarc
+ * @param source
+ * @return true if merge was successful.
+ */
+ public static boolean mergeDocs(VamsasArchive darc, VamsasDocument dest, VamsasArchiveReader sarc, VamsasDocument source) {
+ log.debug("mergeDocs entered.");
+ // search for appDatas in cdoc
+ VAMSAS[] newr = new VAMSAS[dest.getVAMSASCount()+source.getVAMSASCount()];
+ mergeVecs(newr, dest.getVAMSAS(), source.getVAMSAS());
+ dest.setVAMSAS(newr);
+ /** TODO: LATER: should verify that all ids really are unique in newly merged document. If not then what ?
+ * investigate possibility of having an id translation between appDatas and the core document -
+ * the mapping is stored when an external application performs a merge, but when the owning
+ * Application accesses the Vobject, the vorba_id is updated to the new one when it writes its
+ * references in to its appdata again
+ */
+ if (source.getApplicationDataCount()>0) {
+ ApplicationData[] newdat = new ApplicationData[source.getApplicationDataCount()+dest.getApplicationDataCount()];
+ ApplicationData[] sappd = source.getApplicationData();
+ // check refs and update/modify if necessary
+ for (int i=0; i<sappd.length; i++) {
+ addAppDataEntry(darc, dest, sarc, sappd[i]);
+ }
+
+ }
+
+ return true; // success
+ }
+
+ private static CommandProcessor cproc;
+ static {
+ cproc.addCommand("new", 0, "no args");
+ cproc.addCommand("add", 1, "Need another vamsas document archive filename as argument.");
+ cproc.addCommand("repair", 0, "no args");
+ cproc.addCommand("list", 0, "no args");
+ cproc.addCommand("monitor", 0, "no args");
+ }
+
+ public static void main(String argv[]) {
+ /**
+ * TODO: switches for setting user identities for writing to vamsas document
+ */
+ if (argv.length<1) {
+ log.fatal("Usage : <archive to create> [(commands)]");
+ return;
+ }
+ File newarch = new File(argv[0]);
+ int argpos = 0;
+ try {
+ // test fully fledged doc construction
+ VamsasArchive varc = new VamsasArchive(newarch, true);
+ VamsasDocument docroot;
+ docroot = new VamsasDocument();
+ docroot.setProvenance(ProvenanceStuff.newProvenance("ArchiveWriter", "user", "Created new Vamsas Document"));
+ while (++argpos<argv.length) {
+ File archive = new File(argv[argpos]);
+ InputStream istream;
+ if (archive.exists()) {
+ VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);
+ if (vdoc.isValid()) {
+ istream = vdoc.getVamsasDocumentStream();
+ if (istream!=null) {
+ VamsasDocument cdocroot = VamsasDocument.unmarshal(new InputStreamReader(istream));
+ if (cdocroot!=null)
+ mergeDocs(varc, docroot, vdoc, cdocroot);
+ } else
+ log.warn("Unexpectedly null document stream from existing document "+archive);
+ } else {
+ // updating an oldformat stream ?
+ if ((istream = vdoc.getVamsasXmlStream())!=null) {
+ // make a new vamsas document from the vamsas.xml entry
+ VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream)); // TODO: verify only one VAMSAS element per vamsas.xml entry.
+ docroot.getProvenance().addEntry(ProvenanceStuff.newProvenanceEntry("ArchiveWriter", "user", "added vamsas.xml from "+argv[argpos-1]));
+ docroot.addVAMSAS(root);
+ }
+ }
+ } else {
+ // Begin a new vamsas document
+ PrintWriter docwriter = varc.getDocumentOutputStream();
+ }
+ }
+ } catch (Exception e) {
+ log.error("Whilst manipulating "+argv[0], e);
+ }
+ }
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.util.Hashtable;
+import java.util.Vector;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import uk.ac.vamsas.client.IClientAppdata;
+import uk.ac.vamsas.client.Vobject;
+import uk.ac.vamsas.client.VorbaId;
+import uk.ac.vamsas.client.simpleclient.ClientDocument;
+import uk.ac.vamsas.client.simpleclient.VamsasArchive;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.objects.core.Entry;
+import uk.ac.vamsas.objects.core.VAMSAS;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+import uk.ac.vamsas.objects.utils.ProvenanceStuff;
+
+// simple holder to pass to client.
+public class ClientDoc extends uk.ac.vamsas.client.ClientDocument {
+ /* (non-Javadoc)
+ * @see uk.ac.vamsas.client.IClientDocument#getClientAppdata()
+ */
+ public IClientAppdata getClientAppdata() {
+ throw new Error("Appdata access is not implemented in the test.simpleclient.ClientDoc instance."); // TODO Auto-generated method stub
+ }
+ protected boolean isModified=false;
+ private Log log = LogFactory.getLog(ClientDoc.class);
+ protected VamsasDocument doc;
+ public uk.ac.vamsas.objects.core.VAMSAS[] _VamsasRoots;
+ protected VamsasArchive iohandler=null;
+ protected VamsasArchiveReader reader=null;
+ private String user=null;
+ private String app=null;
+
+ /**
+ * @param doc
+ * @param iohandler
+ * @param reader
+ * @param app
+ * @param user
+ */
+ public ClientDoc(VamsasDocument doc, VamsasArchive iohandler, VamsasArchiveReader reader, String app, String user, Hashtable objrefs) {
+ super(objrefs, (iohandler!=null) ? iohandler.getVorba() : null);
+ this.doc = doc;
+ this.iohandler = iohandler;
+ this.reader = reader;
+ this.app = app;
+ this.user = user;
+ this.objrefs = objrefs;
+ _VamsasRoots = doc.getVAMSAS();
+ }
+ // AppDataOutputStream appd;
+ //AppDataOutputStream userd;
+ /* (non-Javadoc)
+ * @see java.lang.Object#finalize()
+ */
+ protected Entry getProvenanceEntry(String action) {
+ // VAMSAS: modify schema to allow referencing of user field (plus other issues, ClientUrn field, machine readable action, input parameters, additional data generated notes
+ Entry prov = ProvenanceStuff.newProvenanceEntry(app, user, action);
+ return prov;
+ }
+ public VAMSAS[] getVamsasRoots() {
+ if (doc==null) {
+ log.debug("Null document for getVamsasRoots(), returning null");
+ return null;
+ }
+ if (iohandler==null) {
+ // LATER: decide on read-only status of ClientDocument object
+ log.warn("getVamsasRoots() called on possibly read-only document.");
+ }
+ if (_VamsasRoots!=null)
+ return _VamsasRoots;
+ VAMSAS[] roots = doc.getVAMSAS();
+ if (roots == null) {
+ // Make a new one to return to client to get filled.
+ _VamsasRoots = new VAMSAS[] { new VAMSAS() };
+ // Do provenance now. just in case.
+ doc.getProvenance().addEntry(getProvenanceEntry("Created new document root [id="+_VamsasRoots[0].getId()+"]"));
+ doc.addVAMSAS(_VamsasRoots[0]);
+ } else {
+ _VamsasRoots = new VAMSAS[roots.length];
+ for (int r=0;r<roots.length; r++)
+ _VamsasRoots[r] = roots[r];
+ }
+ return _VamsasRoots;
+ }
+
+ private int _contains(VAMSAS root, VAMSAS[] docRoots) {
+ if (root==null)
+ return -1;
+ if (docRoots==null || docRoots.length==0)
+ return -1;
+ VorbaId r_id = root.getVorbaId();
+ for (int i=0,j=docRoots.length; i<j; i++) {
+ VorbaId n_id=null;
+ if (docRoots[i]==root || (docRoots[i]!=null && (n_id=docRoots[i].getVorbaId())!=null && n_id.equals(r_id)))
+ return i;
+ }
+ return -1;
+ }
+/**
+ * verify that newr version is really an intact version of the
+ * @param newVersion (may be modified)
+ * @param oldVersion
+ * @return true if newVersion is a valid root that preserves original references
+ */
+ private boolean isValidUpdate(VAMSAS newVersion, final VAMSAS oldVersion) {
+ // ideal - this cascades down the two structures, ensuring that all ID'd objects in one are present in the other.
+ if (oldVersion==newVersion) {
+ // may be a virgin root element.
+ if (!newVersion.isRegistered())
+ iohandler.getVorba().makeVorbaId(newVersion);
+ // Should retrieve original version and compare - unless local hashes can be used to determine if resultSet has been truncated.
+ // just do internal validation for moment.
+ if (newVersion.isValid())
+ return true;
+ return false;
+ } else {
+ // redundant ? if (oldVersion.is__stored_in_document())
+ if (!newVersion.isRegistered())
+ iohandler.getVorba().makeVorbaId(newVersion);
+ if (newVersion.isValid())
+ return true;
+ }
+ return false;
+ }
+ /**
+ /**
+ * merge old and new root vectors
+ * @param newr This array may be written to
+ * @param original
+ * @param the client document (usually this) which this root set belongs to.
+ * @return merged vector of vamsas roots
+ */
+ private VAMSAS[] _combineRoots(VAMSAS[] newr, final VAMSAS[] original, ClientDoc modflag) {
+ Vector rts = new Vector();
+ boolean modified=false;
+ for (int i=0,j=original.length; i<j; i++) {
+ int k = _contains(original[i], newr);
+ if (k>-1) {
+ if (isValidUpdate(newr[k], original[i])) {
+ modified=true;
+ rts.add(newr[k]);
+ newr[k]=null;
+ } else {
+ // LATER: try harder to merge ducument roots.
+ log.warn("Couldn't merge new VAMSAS root "+newr[k].getId());
+ newr[k] = null; // LATER: this means we ignore mangled roots. NOT GOOD
+ }
+ } else {
+ // add in order.
+ rts.add(original[i]);
+ }
+ }
+ // add remaining (new) roots
+ for (int i=0,j=newr.length; i<j; i++) {
+ if (newr[i]!=null) {
+ rts.add(newr[i]);
+ modified=true;
+ }
+ }
+ newr = new VAMSAS[rts.size()];
+ for (int i=0,j=rts.size(); i<j; i++)
+ newr[i] = (VAMSAS) rts.get(i);
+ if (modflag!=null)
+ modflag.isModified = modified;
+ return newr;
+ }
+
+ /**
+ * update the document with new roots.
+ * LATER: decide: this affects the next call to getVamsasRoots()
+ * @see org.vamsas.IClientDocument.setVamsasRoots
+ */
+ public void setVamsasRoots(VAMSAS[] newroots) {
+ if (doc==null) {
+ log.debug("setVamsasRoots called on null document.");
+ return;
+ }
+ VAMSAS[] newr;
+ if (newroots==null) {
+ log.debug("setVamsasRoots(null) - do nothing.");
+ return;
+ }
+ // are we dealing with same array ?
+ if (_VamsasRoots!=newroots) {
+ // merge roots into local version.
+ newr = new VAMSAS[newroots.length];
+ for (int i=0;i<newr.length;i++)
+ newr[i] = newroots[i];
+ newr=_combineRoots(newr,_VamsasRoots,this);
+ } else {
+ newr = new VAMSAS[_VamsasRoots.length];
+ for (int i=0;i<newr.length;i++)
+ newr[i]=_VamsasRoots[i];
+ }
+ // actually compare with document root set for final combination (to ensure nothing is lost)
+ _VamsasRoots = _combineRoots(newr, doc.getVAMSAS(), this);
+ }
+
+
+ /* (non-Javadoc)
+ * LATER: decide: this affects the next call to getVamsasRoots()
+ * @see uk.ac.vamsas.client.IClientDocument#addVamsasRoot(uk.ac.vamsas.objects.core.VAMSAS)
+ */
+ public void addVamsasRoot(VAMSAS newroot) {
+ if (doc==null) {
+ log.debug("addVamsasRoots called on null document.");
+ return;
+ }
+ VAMSAS[] newroots = _combineRoots(new VAMSAS[] {newroot}, _VamsasRoots, this);
+ _VamsasRoots = newroots;
+ }
+
+ public VamsasArchiveReader getReader() {
+ return reader;
+ }
+ private void _finalize() {
+ log.debug("finalizing clientDoc");
+ if (doc!=null) {
+ doc = null;
+ }
+ if (_VamsasRoots!=null) {
+ for (int i=0; i<_VamsasRoots.length; i++)
+ _VamsasRoots[i]=null;
+ _VamsasRoots=null;
+
+ }
+
+ if (reader!=null) {
+ log.debug("Closing and removing reader reference");
+ reader.close();
+ reader=null;
+ }
+ if (iohandler!=null) {
+ log.debug("Removing ioHandler reference.");
+ iohandler.cancelArchive();
+ iohandler=null;
+ }
+ }
+ protected void finalize() throws Throwable {
+ _finalize();
+ super.finalize();
+ }
+ private java.util.Hashtable objrefs=null;
+
+ public VorbaId[] registerObjects(Vobject[] unregistered) {
+ if (doc==null) {
+ log.warn("registerObjects[] called on null document.");
+ return null;
+ }
+ if (objrefs==null) {
+ log.warn("registerObjects[] called for null objrefs hasharray.");
+ return null;
+ }
+ if (unregistered!=null) {
+ VorbaId ids[] = new VorbaId[unregistered.length];
+ for (int i=0,k=unregistered.length; i<k; i++)
+ if (unregistered[i]!=null) {
+ log.warn("Null Vobject passed to registerObject[] at position "+i);
+ return null;
+ } else {
+ ids[i]=registerObject(unregistered[i]);
+ }
+ log.debug("Registered "+unregistered.length+" objects - total of "+objrefs.size()+" ids.");
+ return ids;
+ }
+ return null;
+ }
+ /* (non-Javadoc)
+ * @see uk.ac.vamsas.client.IClientDocument#registerObject(uk.ac.vamsas.client.Vobject)
+ */
+ public VorbaId registerObject(Vobject unregistered) {
+ if (doc==null) {
+ log.warn("registerObjects called on null document.");
+ return null;
+ }
+ if (objrefs==null) {
+ log.warn("registerObjects called for null objrefs hasharray.");
+ return null;
+ }
+ if (iohandler==null) {
+ log.warn("registerObjects called for read only document.");
+ return null;
+ }
+
+ if (unregistered!=null) {
+ VorbaId id = this._registerObject(unregistered);
+ log.debug("Registered object - total of "+objrefs.size()+" ids.");
+ return id;
+ }
+ log.warn("Null Vobject passed to registerObject.");
+ return null;
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.vamsas.client.IClientDocument#getObject(uk.ac.vamsas.client.VorbaId)
+ */
+ public Vobject getObject(VorbaId id) {
+ if (objrefs==null) {
+ log.debug("getObject called on null objrefs list.");
+ return null;
+ }
+ if (objrefs.containsKey(id.getId()))
+ return (Vobject) objrefs.get(id.getId());
+ log.debug("Returning null Vobject reference for id "+id.getId());
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.vamsas.client.IClientDocument#getObjects(uk.ac.vamsas.client.VorbaId[])
+ */
+ public Vobject[] getObjects(VorbaId[] ids) {
+ if (objrefs==null) {
+ log.debug("getObject[] called on null objrefs list.");
+ return null;
+ }
+ Vobject[] vo = new Vobject[ids.length];
+ for (int i=0,j=ids.length; i<j;i++)
+ if (objrefs.containsKey(ids[i]))
+ vo[i] = (Vobject) objrefs.get(ids[i]);
+ else
+ log.debug("Returning null Vobject reference for id "+ids[i].getId());
+ return vo;
+ }
+ protected void updateDocumentRoots() {
+ if (doc==null) {
+ log.error("updateDocumentRoots called on null document. Probably an implementation error.");
+ return;
+ }
+ if (isModified) {
+ if (_VamsasRoots!=null) {
+ doc.setVAMSAS(_VamsasRoots);
+ _VamsasRoots=null;
+ }
+ }
+ }
+ /**
+ * tell vamsas client to close the document and reset the object. Once closed, nothing can be done with the object.
+ *
+ */
+ public void closeDoc() {
+ if (doc!=null) {
+ log.debug("Closing open document.");
+ _finalize();
+ } else {
+ log.warn("Ignoring closeDoc on invalid document.");
+ }
+ }
+
+}
\ No newline at end of file
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.File;
+import java.util.Iterator;
+import java.util.Vector;
+
+
+import uk.ac.vamsas.client.ClientHandle;
+import uk.ac.vamsas.client.simpleclient.ClientsFile;
+import uk.ac.vamsas.client.simpleclient.FileWatcher;
+import uk.ac.vamsas.client.simpleclient.Lock;
+
+public class ClientsFileTest {
+ private static CommandProcessor cproc;
+
+ private static Vector commands;
+ static {
+ cproc = new CommandProcessor();
+ ClientsFileTest.commands = new Vector();
+ ClientsFileTest.commands.add(new String("add"));
+ cproc.addCommand("add", 2, "for the Client's 'Name' and 'Version'");
+ ClientsFileTest.commands.add(new String("remove"));
+ cproc.addCommand("remove", 3, "for the Client's 'Name', Version and URN");
+ ClientsFileTest.commands.add(new String("list"));
+ cproc.addCommand("list", 0, "no args needed");
+ ClientsFileTest.commands.add(new String("clear"));
+ cproc.addCommand("clear", 0, "no args needed");
+ ClientsFileTest.commands.add(new String("watch"));
+ cproc.addCommand("watch", 0, "no args needed");
+ ClientsFileTest.commands.add(new String("monitor"));
+ cproc.addCommand("monitor", 2, "for the Client's 'Name' and 'Version'");
+ }
+
+ private static void complainArgs(int argl, int argpos, String cmd,
+ int argneed, String msg) {
+ if (argl - argpos < argneed)
+ throw new Error(cmd + " needs " + argneed + " arguments : " + msg);
+ }
+
+ public static void main(String[] args) {
+ java.io.File cf = new java.io.File(args[0]);
+ System.out.println("Connecting to clientFile " + args[0]);
+ ClientsFile cfhand;
+ try {
+ cfhand = new ClientsFile(cf);
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ return;
+ }
+ int argc = 1;
+ while (argc < args.length) {
+ // vars needed for operations
+ ClientHandle ch;
+ int com = cproc.getCommand(args, argc);
+ argc++;
+ switch (com) {
+ case 0:
+ // Add
+ int pos = cfhand.addClient(ch = new ClientHandle(args[argc],
+ args[argc + 1]));
+ argc += 2;
+ if (pos != 0)
+ System.out.println("Client added at " + pos + " as urn:"
+ + ch.getClientUrn());
+ else
+ System.out.println("Client was not added.");
+ break;
+ case 1:
+ // remove
+ ch = new ClientHandle(args[argc], args[argc + 1]);
+ ch.setClientUrn(args[argc + 2]);
+ argc += 3;
+ cfhand.removeClient(ch, null);
+ System.out.println("Client removed (apparently)");
+ break;
+ case 2:
+ // list
+ ClientHandle[] chlist = cfhand.retrieveClientList();
+ if (chlist != null) {
+ for (int chi = 0, che = chlist.length; chi < che; chi++) {
+ System.out.println("Client " + chi + " ("
+ + chlist[chi].getClientName() + " " + chlist[chi].getVersion()
+ + " " + chlist[chi].getClientUrn() + ")");
+ }
+ } else {
+ System.out.println("Client list is empty.");
+ }
+ break;
+ case 3:
+ // clear
+ //cfhand.get = null;
+ //cf.delete();
+ try {
+
+ cfhand.clearList();
+
+ } catch (Exception e) {
+ System.err.println("Failed on new empty clientfile creation!");
+ e.printStackTrace(System.err);
+ }
+ break;
+ case 4:
+ // watch
+ FileWatcher w = new FileWatcher(cf);
+ while (cf.exists()) {
+ // get watcher's lock to ensure state change is fixed for retrieval
+ Lock chlock = w.getChangedState();
+ if (chlock != null) {
+ ClientHandle[] cl = cfhand.retrieveClientList(chlock);
+ System.out.println("-- Watching " + cf.getName());
+ //while (w.hasChanged())
+ // ;
+ if (cl != null) {
+ for (int chi = 0, che = cl.length; chi < che; chi++) {
+ System.out.println("Client " + chi + " ("
+ + cl[chi].getClientName() + " " + cl[chi].getVersion()
+ + " " + cl[chi].getClientUrn() + ")");
+ }
+ } else {
+ System.out.println("Client list is empty.");
+ }
+ }
+
+ }
+ break;
+ case 5:
+ // monitor
+ int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
+ args[argc + 1]));
+ argc += 2;
+ if (clpos != 0)
+ System.out.println("Monitor Client added at " + clpos + " as urn:"
+ + ch.getClientUrn());
+ else {
+ System.err.println("Monitor Client was not added.");
+ break;
+ }
+ FileWatcher mon = new FileWatcher(cf);
+ while (cf.exists()) {
+ // get watcher's lock to ensure state change is fixed for retrieval
+ Lock chlock = mon.getChangedState();
+ if (chlock != null) {
+ ClientHandle[] cl = cfhand.retrieveClientList(chlock);
+ System.out.println("-- Monitor " + cf.getName());
+ //while (w.hasChanged())
+ // ;
+ int newpos = -1;
+ if (cl != null) {
+ for (int chi = 0, che = cl.length; chi < che; chi++) {
+ if (ch.equals(cl[chi]))
+ newpos = chi + 1;
+ }
+ }
+ if (newpos == -1) {
+ // add self again to cleared list.
+ newpos = cfhand.addClient(ch);
+ mon.setState();
+ if (newpos == 0) {
+ System.err
+ .println("Monitor client could not be re-added to list.");
+ break;
+ }
+ }
+ if (newpos != clpos) {
+ System.out.println("Monitor client moved from " + clpos + " to "
+ + newpos);
+ clpos = newpos;
+ }
+ }
+ }
+ break;
+ default:
+ if (com == -1) {
+ System.err
+ .println("Unknown command : " + args[argc++] + "*Ignored!*");
+ } else
+ System.err.println("Command " + args[argc++]
+ + " *Ignored!* - its not implemented.");
+ }
+
+ for (int j = 0; j < 900000; j++) {
+ Integer i = Integer.getInteger("1");
+ Integer q = i;
+ }
+ }
+
+ }
+
+ /* Iterator coms = commands.iterator();
+ int com=-1;
+ while ((coms!=null) && coms.hasNext()) {
+ com++;
+ if (args[argc].toLowerCase().equals((String) coms.next())) {
+ System.out.println("Doing "+args[argc]);
+ ClientHandle ch;
+ argc++;
+ switch (com) {
+ case 0:
+ // Add
+ ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
+ int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
+ argc+=2;
+ if (pos!=0)
+ System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
+ else
+ System.out.println("Client was not added.");
+ break;
+ case 1:
+ // remove
+ ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
+ ch=new ClientHandle(args[argc], args[argc+1]);
+ ch.setClientUrn(args[argc+2]);
+ argc+=3;
+ cfhand.removeClient(ch, null);
+ System.out.println("Client removed (apparently)");
+ break;
+ case 2:
+ // list
+ ClientHandle[] chlist = cfhand.retrieveClientList();
+ if (chlist!=null) {
+ for (int chi=0,che=chlist.length; chi<che; chi++) {
+ System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
+ }
+ } else {
+ System.out.println("Client list is empty.");
+ }
+ break;
+ case 3:
+ // clear
+ cfhand = null;
+ cf.delete();
+ try {
+ cf.createNewFile();
+ cfhand = new ClientsFile(cf);
+ }
+ catch (Exception e) {
+ System.err.println("Failed on new empty clientfile creation!");
+ e.printStackTrace(System.err);
+ }
+ break;
+ case 4:
+ // watch
+ FileWatcher w=new FileWatcher(cf);
+ while (cf.exists()) {
+ // get watcher's lock to ensure state change is fixed for retrieval
+ Lock chlock=w.getChangedState();
+ if (chlock!=null) {
+ ClientHandle[] cl = cfhand.retrieveClientList(chlock);
+ System.out.println("-- Watching "+cf.getName());
+ //while (w.hasChanged())
+ // ;
+ if (cl!=null) {
+ for (int chi=0,che=cl.length; chi<che; chi++) {
+ System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
+ }
+ } else {
+ System.out.println("Client list is empty.");
+ }
+ }
+
+ }
+ break;
+ case 5:
+ // monitor
+ ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
+ int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
+ argc+=2;
+ if (clpos!=0)
+ System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
+ else {
+ System.err.println("Monitor Client was not added.");
+ break;
+ }
+ FileWatcher mon=new FileWatcher(cf);
+ while (cf.exists()) {
+ // get watcher's lock to ensure state change is fixed for retrieval
+ Lock chlock=mon.getChangedState();
+ if (chlock!=null) {
+ ClientHandle[] cl = cfhand.retrieveClientList(chlock);
+ System.out.println("-- Monitor "+cf.getName());
+ //while (w.hasChanged())
+ // ;
+ int newpos=-1;
+ if (cl!=null) {
+ for (int chi=0,che=cl.length; chi<che; chi++) {
+ if (ch.equals(cl[chi]))
+ newpos=chi+1;
+ }
+ }
+ if (newpos==-1) {
+ // add self again to cleared list.
+ newpos=cfhand.addClient(ch);
+ mon.setState();
+ if (newpos==0) {
+ System.err.println("Monitor client could not be re-added to list.");
+ break;
+ }
+ }
+ if (newpos!=clpos) {
+ System.out.println("Monitor client moved from "+clpos+" to "+newpos);
+ clpos=newpos;
+ }
+ }
+ }
+
+ }
+ coms = null;
+ }
+ } */
+
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.util.Iterator;
+import java.util.Vector;
+
+public class CommandProcessor {
+ /**
+ * this is not getOPT!!!! - processes a *series* of space separated commands - some of which take arguments.
+ */
+ private Vector commands;
+ /* static {
+ ClientsFileTest.commands=new Vector();
+ ClientsFileTest.commands.add(new String("add"));
+ ClientsFileTest.commands.add(new String("remove"));
+ ClientsFileTest.commands.add(new String("list"));
+ ClientsFileTest.commands.add(new String("clear"));
+ ClientsFileTest.commands.add(new String("watch"));
+ ClientsFileTest.commands.add(new String("monitor"));
+ } */
+
+ public int addCommand(String cmd, int argneed, String complainString) {
+ int cnum=0;
+ if (commands==null)
+ commands = new Vector();
+ else
+ cnum = commands.size();
+ Vector cv = new Vector();
+ cv.add(new String(cmd));
+ cv.add(new Integer(argneed));
+ cv.add(new String(complainString));
+ commands.add(cv);
+ return cnum;
+ }
+
+ /**
+ * Integer argl, Integer argpos, String cmd, Integer argneed, String msg in vector
+ */
+ public void complainArgs(int argl, int argpos, Vector ca) {
+ int argneed = ((Integer) ca.get(1)).intValue();
+ if (argl-argpos<argneed)
+ throw new Error(((String) ca.get(0))+" at position "+argpos+" needs "+argneed+" arguments : "+(String) ca.get(2));
+ }
+ /**
+ * find and verify a command
+ * @param args argstring
+ * @param argpos position to check for command
+ * @return matching command or -1
+ */
+ public int getCommand(String[]args, int argpos) {
+ Iterator coms = commands.iterator();
+ int com=-1, argc;
+ argc=argpos;
+ while ((coms!=null) && coms.hasNext()) {
+ com++;
+ Vector comnext = (Vector) coms.next();
+ if (args[argc].toLowerCase().equals((String) comnext.get(0))) {
+ if (comnext.size()>2)
+ complainArgs(args.length, argc+1, comnext);
+ return com;
+ }
+ }
+ return -1;
+
+ }
+}
--- /dev/null
+package uk.ac.vamsas.test.simpleclient;
+
+import java.io.File;
+import java.io.ObjectOutputStream;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.vamsas.test.objects.Core;
+
+import uk.ac.vamsas.client.simpleclient.Lock;
+import uk.ac.vamsas.client.simpleclient.SessionFile;
+import uk.ac.vamsas.client.simpleclient.SimpleDocument;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.client.simpleclient.VamsasFile;
+import uk.ac.vamsas.objects.core.ApplicationData;
+import uk.ac.vamsas.objects.core.User;
+import uk.ac.vamsas.objects.core.VAMSAS;
+import uk.ac.vamsas.objects.core.VamsasDocument;
+
+public class VamsasArchive {
+ /**
+ * test the org.vamsas.simpleclient.vamsasArchive class
+ */
+ static Log log = LogFactory.getLog(VamsasArchive.class);
+ public static ApplicationData makeDemoAppdata(uk.ac.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("uk.ac.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, System.out);
+ 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");
+ //TODO: write instance appdata 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 {
+ File av;
+ if (args.length>0)
+ av = new File(args[0]);
+ else
+ av = new File("test/vamsas.zip");
+ log.info("Opening archive "+av);
+ uk.ac.vamsas.client.simpleclient.VamsasArchive varchive = new uk.ac.vamsas.client.simpleclient.VamsasArchive(av, true);
+
+ VAMSAS[] roots = (VAMSAS[]) varchive.getOriginalRoots();
+
+ if (roots!=null) {
+ log.info("Report on Original roots in archive:");
+ ArchiveReports.rootReport(roots, true, System.out);
+ }
+ log.info("Getting current vamsas document.");
+ VamsasDocument doc = varchive.getVamsasDocument();
+ ArchiveReports.reportDocument(doc, varchive.getOriginalArchiveReader(), true, System.out); // not modified document so references will still be valid
+ // do some stuff
+ log.info("Retrieving backup");
+ File backup = varchive.backupFile();
+ if (backup==null)
+ log.info(av+" is a New Archive.");
+ else
+ log.info(av+" has been backed up as "+backup);
+ File newf=new File(av.getAbsolutePath()+"_new.zip");
+ VamsasFile sfile = new VamsasFile(newf);
+ /* if (newf.exists()) {
+ int q=1;
+ do {
+ newf=new File(av.getAbsolutePath()+"_"+q+++"_new.zip");
+ }
+ while (newf.exists());
+ } */
+ if (newf.exists()) {
+ log.info("Removing existing "+newf);
+ newf.delete();
+ }
+
+ log.info("Now writing new Archive into "+newf.getAbsolutePath());
+ uk.ac.vamsas.client.simpleclient.VamsasArchive va=null;
+ { // hold lock over deletion and write of new archive.
+ //Lock wlock = sfile.getLock();
+ //newf.delete(); // clear out old file.
+ sfile.getLock();
+ va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, true, true, sfile);
+ // open another and...
+ ApplicationData appdata = makeDemoAppdata(va,
+ "uk.ac.vamsas.test.simpleclient.VamsasArchive", "arnold Bugger esq", "disOrganised");
+ log.info("Preparing to write new document.");
+ doc.addApplicationData(appdata);
+ doc.addVAMSAS(Core.getDemoVamsas());
+ va.putVamsasDocument(doc); // gets stream and puts it.
+ va.closeArchive();
+ sfile.unLock();
+ }
+ log.info("Dump of new vamsas document :");
+ log.info("Testing update: ");
+ {
+ Lock lock=sfile.getLock();
+ if (lock==null)
+ while ((lock=sfile.getLock())==null) {
+ log.info("Waiting for lock.");
+ Thread.sleep(100);
+ }
+ VamsasArchiveReader vreader = new VamsasArchiveReader(sfile.getVamsasFile());// lock); // cannot do new JarFile on a locked file. // newf);
+ SimpleDocument sdoc = new SimpleDocument("testing new vamsas write");
+ ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true, System.out);
+ sfile.unLock();
+ }
+ // backup.delete(); // tidy up
+
+ log.info("Now Cancelling write to original archive "+av);
+ if (varchive.cancelArchive())
+ log.info("Successfully cancelled.");
+ else
+ log.info("Didn't cancel.");
+ long t=System.currentTimeMillis()+200; while (t>System.currentTimeMillis());
+ log.info("Now testing archive update.");
+ va = new uk.ac.vamsas.client.simpleclient.VamsasArchive(newf, false, true, sfile);
+ doc = va.getVamsasDocument();
+ doc.addVAMSAS(Core.getDemoVamsas());
+ doc.addApplicationData(makeDemoAppdata(va,
+ "uk.ac.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();
+ sfile.unLock();
+ log.info("Testing update: ");
+ {
+ Lock lock=sfile.getLock();
+ if (lock==null)
+ while ((lock=sfile.getLock())==null)
+ log.info("Waiting for lock.");
+ // VamsasArchiveReader vreader = new VamsasArchiveReader(lock);
+ VamsasArchiveReader vreader = new VamsasArchiveReader(newf);
+
+ SimpleDocument sdoc = new SimpleDocument("testing vamsas update");
+ VamsasDocument finaldoc = sdoc.getVamsasDocument(vreader);
+ if (finaldoc!=null)
+ ArchiveReports.reportDocument(finaldoc, vreader, true, System.out);
+ else
+ log.error("Null Document Read from "+newf);
+ }
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ }
+ }
+}
import javax.swing.JInternalFrame;
-import org.vamsas.test.simpleclient.ArchiveClient;
-import org.vamsas.test.simpleclient.ClientDoc;
import uk.ac.vamsas.client.UserHandle;
import uk.ac.vamsas.client.simpleclient.FileWatcher;
import uk.ac.vamsas.client.simpleclient.VamsasFile;
import uk.ac.vamsas.objects.core.Entry;
import uk.ac.vamsas.objects.core.VamsasDocument;
+import uk.ac.vamsas.test.simpleclient.ArchiveClient;
+import uk.ac.vamsas.test.simpleclient.ClientDoc;
/**
* @author jimp
\r
package uk.ac.vamsas.test.simpleclient.simpleapp;\r
\r
-import org.vamsas.test.simpleclient.ClientDoc;\r
\r
\r
import java.io.*;\r
import uk.ac.vamsas.client.VorbaId;\r
import uk.ac.vamsas.objects.core.*;\r
import uk.ac.vamsas.objects.utils.DocumentStuff;\r
+import uk.ac.vamsas.test.simpleclient.ClientDoc;\r
\r
/*\r
* \r