4 package uk.ac.vamsas.test.simpleclient;
7 import java.io.IOException;
9 import java.util.Hashtable;
10 import java.util.Vector;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.exolab.castor.xml.MarshalException;
15 import org.exolab.castor.xml.ValidationException;
17 import uk.ac.vamsas.client.ClientHandle;
18 import uk.ac.vamsas.client.IVorbaIdFactory;
19 import uk.ac.vamsas.client.SessionHandle;
20 import uk.ac.vamsas.client.UserHandle;
21 import uk.ac.vamsas.client.Vobject;
22 import uk.ac.vamsas.client.VorbaId;
23 import uk.ac.vamsas.client.simpleclient.AppDataOutputStream;
24 import uk.ac.vamsas.client.simpleclient.FileWatcher;
25 import uk.ac.vamsas.client.simpleclient.IdFactory;
26 import uk.ac.vamsas.client.simpleclient.SessionFile;
27 import uk.ac.vamsas.client.simpleclient.SimpleDocBinding;
28 import uk.ac.vamsas.client.simpleclient.SimpleDocument;
29 import uk.ac.vamsas.client.simpleclient.VamsasArchive;
30 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
31 import uk.ac.vamsas.client.simpleclient.VamsasFile;
32 import uk.ac.vamsas.objects.core.AppData;
33 import uk.ac.vamsas.objects.core.ApplicationData;
34 import uk.ac.vamsas.objects.core.User;
35 import uk.ac.vamsas.objects.core.VamsasDocument;
36 import uk.ac.vamsas.objects.utils.AppDataReference;
37 import uk.ac.vamsas.objects.utils.DocumentStuff;
38 import uk.ac.vamsas.objects.utils.ProvenanceStuff;
39 import uk.ac.vamsas.objects.utils.SeqSet;
40 import uk.ac.vamsas.objects.utils.document.VersionEntries;
41 import uk.ac.vamsas.test.objects.Core;
45 * test the VamsasFile routines for watching, reading and updating a vamsas document jar file.
46 * simple document access base class.
48 public class ArchiveClient extends IdFactory {
50 private Log log = LogFactory.getLog(ArchiveClient.class);
51 // protected UserHandle user=null;
52 // protected ClientHandle me = new ClientHandle("ArchiveClient","0.01");
59 public ArchiveClient(UserHandle user, VamsasFile vsess) {
60 super(new SessionHandle("vamsasfile://"+vsess.getVamsasFile()), new ClientHandle("ArchiveClient","0.01"), user);
64 private void _openVsess(File vsess) {
66 this.vsess = new VamsasFile(vsess);
69 log.error("Couldn't open session for file "+vsess,e);
73 public ArchiveClient(String username, String organization, File vsess) {
74 super(new SessionHandle("vamsasfile://"+vsess), new ClientHandle("ArchiveClient","0.01"), new UserHandle(username, organization));
78 public ArchiveClient(String username, String organization, String clientName, String clientVersion, File vsess) {
79 super(new SessionHandle("vamsasfile://"+vsess), new ClientHandle(clientName, clientVersion), new UserHandle(username, organization));
85 throw new Error("ArchiveClient instance is invalid!.");
88 * set this to false if watch loop should end immediately
90 protected boolean watchForChange=true;
91 public static int WATCH_SLEEP=300;
93 * watch the document file for updates.
94 * @param time - length of time to watch for.
95 * @return read only IO interface for session document.
97 public ClientDoc watch(long time) {
99 vsess.unLock(); // doh.
100 FileWatcher watcher = new FileWatcher(vsess.getVamsasFile());
101 // watcher.setState();
103 long endtime=System.currentTimeMillis()+time;
105 uk.ac.vamsas.client.simpleclient.Lock doclock;
107 doclock=watcher.getChangedState();
109 Thread.sleep(WATCH_SLEEP);
110 } while (watchForChange && doclock==null && (time==0 || endtime>System.currentTimeMillis())); // tuning.
114 return getUpdateable(vsess.getLock(doclock));
115 /*VamsasArchiveReader varc = new VamsasArchiveReader(vsess.getVamsasFile());
116 return _getReadonly(varc);*/
118 } catch (Exception e) {
119 log.error("Whilst watching file "+vsess.getVamsasFile(), e);
124 // from ClientDocument.getClientAppdata
125 private AppData[] getAppData(VamsasDocument doc) {
126 // TODO: codefest - not yet tested or merged in to SimpleClient
128 log.debug("extractAppData called for null document object");
131 AppData appsGlobal=null, usersData=null;
132 Vector apldataset = AppDataReference.getUserandApplicationsData(
133 doc, this.getUserHandle(), this.getClientHandle());
134 if (apldataset!=null) {
135 if (apldataset.size()>0) {
136 AppData clientdat = (AppData) apldataset.get(0);
137 if (clientdat instanceof ApplicationData) {
138 appsGlobal = (ApplicationData) clientdat;
139 if (apldataset.size()>1) {
140 clientdat = (AppData) apldataset.get(1);
141 if (clientdat instanceof User) {
142 usersData = (User) clientdat;
144 if (apldataset.size()>2)
145 log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
148 log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
150 apldataset.removeAllElements(); // destroy references.
153 return new AppData[] { appsGlobal, usersData};
156 protected ClientDoc _getReadonly(VamsasArchiveReader vreader) throws IOException, ValidationException, MarshalException {
159 SimpleDocBinding docb = new SimpleDocBinding();
162 d = docb.getVamsasDocument(vreader);
165 ClientDoc creader = new ClientDoc(d, null, vreader, getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
173 * @return user field for a provenance entry
175 protected String getProvenanceUser() {
176 return new String(getUserHandle().getFullName()+" ["+getClientHandle().getClientUrn()+"]");
179 public ClientDoc getUpdateable() {
180 return getUpdateable(null);
182 public ClientDoc getUpdateable(uk.ac.vamsas.client.simpleclient.Lock lock) {
183 getVorbaIdHash().clear();
186 // patiently wait for a lock on the document.
188 while (lock==null && ((lock=vsess.getLock())==null || !lock.isLocked()) && --tries>0) {
190 log.debug("Trying to get a document lock for the "+tries+"'th time.");
192 VamsasArchive varc = new VamsasArchive(vsess, true, false); // read archive, write as vamsasDocument, don't erase original contents.
194 VamsasDocument d = varc.getVamsasDocument(getProvenanceUser(), "Created new document.", VersionEntries.latestVersion()); // VAMSAS: provenance user and client combined
197 log.warn("Backing out from opening a VamsasArchive writable IO session");
198 varc.cancelArchive();
201 ClientDoc cdoc = new ClientDoc(d, varc, varc.getOriginalArchiveReader(), getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
204 } catch (Exception e) {
205 log.error("Failed to get Updateable version of "+vsess.getVamsasFile(), e);
210 * trust client to not do anything stupid to the document roots which will now be written to the archive.
212 * @return true if write was a success.
214 public boolean doUpdate(ClientDoc cdoc) {
217 log.warn("Invalid ClientDoc passed to uk.ac.vamsas.test.simpleclient.doUpdate()");
220 if (cdoc.iohandler==null) {
221 log.warn("Read only ClientDoc object passed to uk.ac.vamsas.test.simpleclient.doUpdate()");
224 if (cdoc.iohandler.getVorba()!=this) {
225 log.error("Mismatch between ClientDoc instances and ArchiveClient instances!");
229 // do any appDatas first.
230 if (cdoc.iohandler.transferRemainingAppDatas())
231 log.debug("Remaining appdatas were transfered.");
232 cdoc.updateDocumentRoots();
233 cdoc.iohandler.putVamsasDocument(cdoc.doc);
234 cdoc.iohandler.closeArchive();
235 this.extantids.clear();// we forget our ids after we close the document.
239 } catch (Exception e) {
240 log.warn("While updating archive in "+vsess.getVamsasFile(),e);
248 public static void usage() {
249 throw new Error("Usage: Username Organization VamsasFile [command,args]*");
251 public static void main(String[] args) {
256 ArchiveClient client = new ArchiveClient(args[0],args[1], new File(args[2]));
260 cdoc = client.getUpdateable();
261 // ArchiveReports.reportDocument(cdoc.doc, cdoc.getReader(), true, System.out);
262 System.out.println("Report Roots :");
263 ArchiveReports.rootReport(cdoc.getVamsasRoots(), true, System.out);
264 cdoc.addVamsasRoot(Core.getDemoVamsas());
265 System.out.println("Doing update.");
266 client.doUpdate(cdoc);
271 System.out.println("Watch for more... ("+u+" left)");
272 ClientDoc ucdoc = client.watch(0000);
274 System.out.println("****\nUpdate detected at "+new Date());
275 ArchiveReports.reportDocument(ucdoc.doc, ucdoc.getReader(), true, System.out);
279 System.out.println("!!!! Null document update detected at "+new Date());
283 catch (Exception e) {
284 client.log.error("Broken!", e);
286 System.out.println("Finished at "+new Date());
288 public uk.ac.vamsas.client.Vobject getObject(VorbaId id) {
289 Hashtable idhash = this.getVorbaIdHash();
290 if (idhash!=null && idhash.containsKey(id))
291 return (Vobject) idhash.get(id);