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)
*
// retrieve object and return
return null;
}
-
+
/*
* (non-Javadoc)
*
// 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.
*/
// 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
} 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()) {
}
}
}
-
+
/* (non-Javadoc)
* @see org.vamsas.client.IClientDocument#addVamsasRoot(org.vamsas.objects.core.VAMSAS)
*/
// TODO Auto-generated method stub
}
-
-/*
- * (non-Javadoc)
- *
- * @see org.vamsas.client.IClientDocument#setApplicationData(byte[])
- */
- public void setApplicationData(byte[] newData) {
- appData = newData;
- }
+
/*
* (non-Javadoc)
*
}
return null;
}
-
+
/* (non-Javadoc)
* @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object)
*/
// 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;
+ }
}
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
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);
}
/**
*/
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
// 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)
*
*/
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;
}
/*
* @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
* @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()
*/
--- /dev/null
+/**
+ *
+ */
+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
+
+ }
+
+}