4 package org.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;
16 import org.vamsas.test.objects.Core;
18 import uk.ac.vamsas.client.ClientHandle;
19 import uk.ac.vamsas.client.IVorbaIdFactory;
20 import uk.ac.vamsas.client.SessionHandle;
21 import uk.ac.vamsas.client.UserHandle;
22 import uk.ac.vamsas.client.Vobject;
23 import uk.ac.vamsas.client.VorbaId;
24 import uk.ac.vamsas.client.simpleclient.AppDataOutputStream;
25 import uk.ac.vamsas.client.simpleclient.FileWatcher;
26 import uk.ac.vamsas.client.simpleclient.IdFactory;
27 import uk.ac.vamsas.client.simpleclient.SessionFile;
28 import uk.ac.vamsas.client.simpleclient.SimpleDocBinding;
29 import uk.ac.vamsas.client.simpleclient.SimpleDocument;
30 import uk.ac.vamsas.client.simpleclient.VamsasArchive;
31 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
32 import uk.ac.vamsas.client.simpleclient.VamsasFile;
33 import uk.ac.vamsas.objects.core.AppData;
34 import uk.ac.vamsas.objects.core.ApplicationData;
35 import uk.ac.vamsas.objects.core.User;
36 import uk.ac.vamsas.objects.core.VamsasDocument;
37 import uk.ac.vamsas.objects.utils.AppDataReference;
38 import uk.ac.vamsas.objects.utils.DocumentStuff;
39 import uk.ac.vamsas.objects.utils.ProvenanceStuff;
40 import uk.ac.vamsas.objects.utils.SeqSet;
41 import uk.ac.vamsas.objects.utils.document.VersionEntries;
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) {
127 log.debug("extractAppData called for null document object");
130 AppData appsGlobal=null, usersData=null;
131 Vector apldataset = AppDataReference.getUserandApplicationsData(
132 doc, this.getUserHandle(), this.getClientHandle());
133 if (apldataset!=null) {
134 if (apldataset.size()>0) {
135 AppData clientdat = (AppData) apldataset.get(0);
136 if (clientdat instanceof ApplicationData) {
137 appsGlobal = (ApplicationData) clientdat;
138 if (apldataset.size()>1) {
139 clientdat = (AppData) apldataset.get(1);
140 if (clientdat instanceof User) {
141 usersData = (User) clientdat;
143 if (apldataset.size()>2)
144 log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
147 log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
149 apldataset.removeAllElements(); // destroy references.
152 return new AppData[] { appsGlobal, usersData};
155 protected ClientDoc _getReadonly(VamsasArchiveReader vreader) throws IOException, ValidationException, MarshalException {
158 SimpleDocBinding docb = new SimpleDocBinding();
161 d = docb.getVamsasDocument(vreader);
164 ClientDoc creader = new ClientDoc(d, null, vreader, getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
172 * @return user field for a provenance entry
174 protected String getProvenanceUser() {
175 return new String(getUserHandle().getFullName()+" ["+getClientHandle().getClientUrn()+"]");
178 public ClientDoc getUpdateable() {
179 return getUpdateable(null);
181 public ClientDoc getUpdateable(uk.ac.vamsas.client.simpleclient.Lock lock) {
182 getVorbaIdHash().clear();
185 // patiently wait for a lock on the document.
187 while (lock==null && ((lock=vsess.getLock())==null || !lock.isLocked()) && --tries>0) {
189 log.debug("Trying to get a document lock for the "+tries+"'th time.");
191 VamsasArchive varc = new VamsasArchive(vsess, true, false); // read archive, write as vamsasDocument, don't erase original contents.
193 VamsasDocument d = varc.getVamsasDocument(getProvenanceUser(), "Created new document.", VersionEntries.latestVersion()); // VAMSAS: provenance user and client combined
196 log.warn("Backing out from opening a VamsasArchive writable IO session");
197 varc.cancelArchive();
200 ClientDoc cdoc = new ClientDoc(d, varc, varc.getOriginalArchiveReader(), getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
203 } catch (Exception e) {
204 log.error("Failed to get Updateable version of "+vsess.getVamsasFile(), e);
209 * trust client to not do anything stupid to the document roots which will now be written to the archive.
211 * @return true if write was a success.
213 public boolean doUpdate(ClientDoc cdoc) {
216 log.warn("Invalid ClientDoc passed to org.vamsas.test.simpleclient.doUpdate()");
219 if (cdoc.iohandler==null) {
220 log.warn("Read only ClientDoc object passed to org.vamsas.test.simpleclient.doUpdate()");
223 if (cdoc.iohandler.getVorba()!=this) {
224 log.error("Mismatch between ClientDoc instances and ArchiveClient instances!");
228 // do any appDatas first.
229 if (cdoc.iohandler.transferRemainingAppDatas())
230 log.debug("Remaining appdatas were transfered.");
231 cdoc.updateDocumentRoots();
232 cdoc.iohandler.putVamsasDocument(cdoc.doc);
233 cdoc.iohandler.closeArchive();
234 this.extantids.clear();// we forget our ids after we close the document.
238 } catch (Exception e) {
239 log.warn("While updating archive in "+vsess.getVamsasFile(),e);
247 public static void usage() {
248 throw new Error("Usage: Username Organization VamsasFile [command,args]*");
250 public static void main(String[] args) {
255 ArchiveClient client = new ArchiveClient(args[0],args[1], new File(args[2]));
259 cdoc = client.getUpdateable();
260 // ArchiveReports.reportDocument(cdoc.doc, cdoc.getReader(), true, System.out);
261 System.out.println("Report Roots :");
262 ArchiveReports.rootReport(cdoc.getVamsasRoots(), true, System.out);
263 cdoc.addVamsasRoot(Core.getDemoVamsas());
264 System.out.println("Doing update.");
265 client.doUpdate(cdoc);
270 System.out.println("Watch for more... ("+u+" left)");
271 ClientDoc ucdoc = client.watch(0000);
273 System.out.println("****\nUpdate detected at "+new Date());
274 ArchiveReports.reportDocument(ucdoc.doc, ucdoc.getReader(), true, System.out);
278 System.out.println("!!!! Null document update detected at "+new Date());
282 catch (Exception e) {
283 client.log.error("Broken!", e);
285 System.out.println("Finished at "+new Date());
287 public uk.ac.vamsas.client.Vobject getObject(VorbaId id) {
288 Hashtable idhash = this.getVorbaIdHash();
289 if (idhash!=null && idhash.containsKey(id))
290 return (Vobject) idhash.get(id);