From 62afce2577f8553bb28ca2bab4c4b5a1202b22a0 Mon Sep 17 00:00:00 2001 From: jprocter Date: Tue, 24 Jan 2006 19:31:49 +0000 Subject: [PATCH] initial org.vamsas.client.IClientAppData implementation phase. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@174 be28352e-c001-0410-b1a7-c7978e42abec --- .../vamsas/client/simpleclient/ClientDocument.java | 105 +++++++++++++------- .../vamsas/client/simpleclient/SimpleClient.java | 94 ++++++++++++------ .../client/simpleclient/SimpleClientAppdata.java | 98 ++++++++++++++++++ 3 files changed, 229 insertions(+), 68 deletions(-) create mode 100644 src/org/vamsas/client/simpleclient/SimpleClientAppdata.java diff --git a/src/org/vamsas/client/simpleclient/ClientDocument.java b/src/org/vamsas/client/simpleclient/ClientDocument.java index 3e5fcfa..7394188 100644 --- a/src/org/vamsas/client/simpleclient/ClientDocument.java +++ b/src/org/vamsas/client/simpleclient/ClientDocument.java @@ -4,43 +4,79 @@ package org.vamsas.client.simpleclient; import java.util.Hashtable; +import java.util.Vector; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.vamsas.client.IClient; +import org.vamsas.client.IClientAppdata; import org.vamsas.client.IClientDocument; import org.vamsas.client.VorbaId; import org.vamsas.client.object; +import org.vamsas.objects.core.AppData; +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; /** * @author jimp Contains a collection of vamsas objects and reference to a * specified ClientHandle's information. */ public class ClientDocument extends org.vamsas.client.ClientDocument implements IClientDocument { + private static Log log = LogFactory.getLog(ClientDocument.class); + private VamsasDocument doc; protected IClient vorba; - protected org.vamsas.objects.core.VAMSAS[] roots; - protected byte[] appData; + protected ApplicationData appsglobal=null; + protected User usersdata=null; + protected byte[] appData=null; protected VamsasArchive archive = null; - /* - * (non-Javadoc) - * - * @see org.vamsas.client.IClientDocument#getApplicationData() - */ - /** + * + * prepare Application-side dataset from the vamsas Document archive + * @param doc - the dataset + * @param docHandler - the sessionFile IO handler + * @param Factory - the source of current and new vorbaIds + * @param vorba - the client instance */ - protected ClientDocument(VamsasArchive document, IdFactory Factory, IClient vorba) { + protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, IClient vorba) { super(Factory.getVorbaIdHash(), Factory); + + /** + * prepare Application-side dataset from the vamsas Document archive + */ this.vorba = vorba; - archive = document; - roots = null; + archive = docHandler; + this.doc = doc; + extractAppData(); } - - public byte[] getApplicationData() { - // Look up client byte stash using client and user handle - return appData; + /** + * gets appropriate app data for the application, if it exists in this dataset + * + */ + private void extractAppData() { + Vector apldataset = AppDataReference.getUserandApplicationsData( + doc, vorbafactory.getUserHandle(), vorbafactory.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()); + } + } + } } - + /* * (non-Javadoc) * @@ -51,7 +87,7 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements // retrieve object and return return null; } - + /* * (non-Javadoc) * @@ -61,19 +97,19 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements // TODO: getObject in bulk return null; } - + /* * (non-Javadoc) * * @see org.vamsas.client.IClientDocument#getVamsasRoots() */ public VAMSAS[] getVamsasRoots() { - if (roots == null) + if (doc.getVAMSAS() == null) // Make a new one to return to client : TODO: Decide if this is correct return new VAMSAS[] { new VAMSAS() }; - return roots; + return doc.getVAMSAS(); } - + /** * update the document with new roots. */ @@ -85,7 +121,7 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements // if they have ? we overwrite them with their new version, ensuring that // provenance is updated. // if they haven't ? do nothing ? - + for (int i = 0, k = newroots.length; i < k; i++) { if (newroots[i].isRegistered()) { // easy - just check if anything has changed and do provenance @@ -104,8 +140,8 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements } else { throw new Error( "SimpleClient error when using setVamsasRoots : The vorbaId for object " - + i - + " does not refer to an object of type VAMSAS in the current document!"); + + i + + " does not refer to an object of type VAMSAS in the current document!"); } } else { if (!newroots[i].is__stored_in_document()) { @@ -125,7 +161,7 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements } } } - + /* (non-Javadoc) * @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS) */ @@ -133,15 +169,7 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements // TODO Auto-generated method stub } - -/* - * (non-Javadoc) - * - * @see org.vamsas.client.IClientDocument#setApplicationData(byte[]) - */ - public void setApplicationData(byte[] newData) { - appData = newData; - } + /* * (non-Javadoc) * @@ -156,7 +184,7 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements } return null; } - + /* (non-Javadoc) * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object) */ @@ -164,4 +192,11 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements // TODO: add provenance stuff to newly registered object return _registerObject(unregistered); } + /* (non-Javadoc) + * @see org.vamsas.client.IClientDocument#getClientAppdata() + */ + public IClientAppdata getClientAppdata() { + // TODO Auto-generated method stub + return null; + } } diff --git a/src/org/vamsas/client/simpleclient/SimpleClient.java b/src/org/vamsas/client/simpleclient/SimpleClient.java index 37595b4..0f967f1 100644 --- a/src/org/vamsas/client/simpleclient/SimpleClient.java +++ b/src/org/vamsas/client/simpleclient/SimpleClient.java @@ -24,7 +24,10 @@ import org.vamsas.client.IClient; import org.vamsas.client.IClientDocument; import org.vamsas.client.SessionHandle; import org.vamsas.client.UserHandle; +import org.vamsas.objects.core.LockFile; import org.vamsas.objects.core.VamsasDocument; +import org.vamsas.objects.utils.ProvenanceStuff; +import org.vamsas.objects.utils.document.VersionEntries; /** * @author jimp @@ -39,12 +42,13 @@ public class SimpleClient implements IClient { protected VamsasSession _session; protected ClientHandle client = null; protected EventGeneratorThread evgen = null; - IdFactory vorba = null; - private void makeVorbaIdFactory() { - if (vorba==null) { - vorba = new IdFactory(getSessionHandle(), client, user); - } else - log.debug("Vorba Id factory exists already."); + /** + * construct a transient IdFactory instance - this should last only as long as the + * SimpleClient object holds the lock on the vamsas document being created/manipulated. + * @return + */ + private IdFactory makeVorbaIdFactory() { + return new IdFactory(getSessionHandle(), client, user); } /** @@ -71,20 +75,31 @@ public class SimpleClient implements IClient { */ protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception { this(user, client, sess); - makeVorbaIdFactory(); VamsasArchive sessdoc = _session.getVamsasDocument(); try { VamsasArchiveReader odoc = new VamsasArchiveReader(importingArchive); - SimpleDocument sdoc = new SimpleDocument(vorba); + SimpleDocument sdoc = new SimpleDocument(makeVorbaIdFactory()); VamsasDocument doc = sdoc.getVamsasDocument(odoc); - sessdoc.putVamsasDocument(doc, vorba); + sessdoc.putVamsasDocument(doc, sdoc.vorba); sessdoc.closeArchive(); } catch (Exception e) { sessdoc.cancelArchive(); // write a dummy archive + _session.slog.info("Exception when importing document data from "+importingArchive); throw new Exception("Failed to import data from "+importingArchive, e); } } + /** + * create new empty document with initial provenance entry + * @return + */ + private VamsasDocument createDummyVamsasDocument() { + VamsasDocument doc = new VamsasDocument(); + doc.setProvenance(ProvenanceStuff.newProvenance(user.getFullName()+" "+client.getClientName(), "new session document")); + doc.setVersion(VersionEntries.latestVersion()); + return doc; + } + /* * (non-Javadoc) * TODO: LATER: check that build substitution variables are correct @@ -181,16 +196,6 @@ public class SimpleClient implements IClient { // mark this instance as finalized } - /** - * extract data appropriate for client, session and user - * from vamsas document. - * @return application's byte array - */ - private byte[] getApplicationData() { - // TODO: extract correct byte object from Jar and return it to application. - return null; - } - /* * (non-Javadoc) * @@ -198,23 +203,36 @@ public class SimpleClient implements IClient { */ public IClientDocument getClientDocument() throws IOException { - makeVorbaIdFactory(); - VamsasArchive va = _session.getVamsasDocument(); + VamsasArchive va = null; + try { + va = _session.getVamsasDocument(); + } + catch (IOException e) { + throw new IOException("Failed to get kock on session document"); + } + VamsasDocument doc=null; + IdFactory vorba = null; // TODO: reduce size of vorba ids generated from these parameters to IdFactory (mainly sessionHandle rationalization ?) try { - va.getOriginalVamsasDocument(va, vorba); - // if session currently holds data - read it in. + va.setVorba(vorba=makeVorbaIdFactory()); + // if session currently holds data - read it in - or get a dummy + _session.slog.debug("Accessing document"); + doc = + va.getVamsasDocument(user.getFullName()+" using "+client.getClientName(), + "created new session document.", null); + if (doc!=null) + _session.slog.debug("Successfully retrieved document."); + else + log.error("Unexpectedly retrieved null document!"); } catch (Exception e) { - + log.error("Failed to get session document for session directory '"+_session.sessionDir+"'", e); + throw new IOException("Failed to get session document for session directory '"+_session.sessionDir+"'"); } - //ClientDocument cdoc = new ClientDocument(); + // Construct the IClientDocument instance - Object[] vdoc;// TODO: = getVamsasDocument(new Reader()); - // ClientDocument cdoc = new ClientDocument(getApplicationData(), - // ((VamsasDocument) vdoc[0]).getVAMSAS(), (Hashtable) vdoc[1], this); - // - return null; + ClientDocument cdoc = new ClientDocument(doc, va, vorba, this); + return cdoc; } /* @@ -239,7 +257,7 @@ public class SimpleClient implements IClient { * @see org.vamsas.client.IClient#storeDocument(java.io.File) */ public void storeDocument(File location) { - + // write storeDocument file to inform other clients that they should raise Lock vamlock = evgen.want_to_store(); // Events.DOCUMENT_FINALIZEAPPDATA @@ -271,17 +289,27 @@ public class SimpleClient implements IClient { * @see org.vamsas.client.IClient#pollUpdate() */ public void pollUpdate() { - + if (evgen==null) { log.warn("pollUpdate called on incomplete SimpleClient object."); return; } + + if (!evgen.isAlive()) { + log.warn("pollUpdate called before joinSession()"); + try { + joinSession(); + } catch (Exception e) { + log.error("Unexpected exception on default call to joinSession",e); + } + } + //TODO ensure event generator robustly handles these interrupts. log.debug("interrrupting event generator."); evgen.interrupt(); log.debug("interrrupted event generator."); } - + /* (non-Javadoc) * @see org.vamsas.client.IClient#joinSession() */ diff --git a/src/org/vamsas/client/simpleclient/SimpleClientAppdata.java b/src/org/vamsas/client/simpleclient/SimpleClientAppdata.java new file mode 100644 index 0000000..9a68c4b --- /dev/null +++ b/src/org/vamsas/client/simpleclient/SimpleClientAppdata.java @@ -0,0 +1,98 @@ +/** + * + */ +package org.vamsas.client.simpleclient; + +import java.io.DataInput; +import java.io.DataOutput; + +import org.vamsas.client.IClientAppdata; + +/** + * @author jimp + * Access interface to data chunks read from a VamsasArchiveReader stream + * (or byte buffer input stream) or written to a VamsasArchive stream. + */ +public class SimpleClientAppdata implements IClientAppdata { + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#getClientAppdata() + */ + public byte[] getClientAppdata() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#getClientInputStream() + */ + public DataInput getClientInputStream() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#getClientOutputStream() + */ + public DataOutput getClientOutputStream() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#getUserAppdata() + */ + public byte[] getUserAppdata() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#getUserInputStream() + */ + public DataInput getUserInputStream() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#getUserOutputStream() + */ + public DataOutput getUserOutputStream() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#hasClientAppdata() + */ + public boolean hasClientAppdata() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#hasUserAppdata() + */ + public boolean hasUserAppdata() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#setClientAppdata(byte[]) + */ + public void setClientAppdata(byte[] data) { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see org.vamsas.client.IClientAppdata#setUserAppdata(byte[]) + */ + public void setUserAppdata(byte[] data) { + // TODO Auto-generated method stub + + } + +} -- 1.7.10.2