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.client.ClientHandle;
17 import org.vamsas.client.IVorbaIdFactory;
18 import org.vamsas.client.SessionHandle;
19 import org.vamsas.client.UserHandle;
20 import org.vamsas.client.Vobject;
21 import org.vamsas.client.VorbaId;
22 import org.vamsas.client.simpleclient.AppDataOutputStream;
23 import org.vamsas.client.simpleclient.FileWatcher;
24 import org.vamsas.client.simpleclient.IdFactory;
25 import org.vamsas.client.simpleclient.SessionFile;
26 import org.vamsas.client.simpleclient.SimpleDocBinding;
27 import org.vamsas.client.simpleclient.SimpleDocument;
28 import org.vamsas.client.simpleclient.VamsasArchive;
29 import org.vamsas.client.simpleclient.VamsasArchiveReader;
30 import org.vamsas.client.simpleclient.VamsasFile;
31 import org.vamsas.objects.core.AppData;
32 import org.vamsas.objects.core.ApplicationData;
33 import org.vamsas.objects.core.User;
34 import org.vamsas.objects.core.VamsasDocument;
35 import org.vamsas.objects.utils.AppDataReference;
36 import org.vamsas.objects.utils.DocumentStuff;
37 import org.vamsas.objects.utils.ProvenanceStuff;
38 import org.vamsas.objects.utils.SeqSet;
39 import org.vamsas.objects.utils.document.VersionEntries;
40 import org.vamsas.test.objects.Core;
44 * test the VamsasFile routines for watching, reading and updating a vamsas document jar file.
45 * simple document access base class.
47 public class ArchiveClient extends IdFactory {
49 private Log log = LogFactory.getLog(ArchiveClient.class);
50 // protected UserHandle user=null;
51 // protected ClientHandle me = new ClientHandle("ArchiveClient","0.01");
58 public ArchiveClient(UserHandle user, VamsasFile vsess) {
59 super(new SessionHandle("vamsasfile://"+vsess.getVamsasFile()), new ClientHandle("ArchiveClient","0.01"), user);
63 private void _openVsess(File vsess) {
65 this.vsess = new VamsasFile(vsess);
68 log.error("Couldn't open session for file "+vsess,e);
72 public ArchiveClient(String username, String organization, File vsess) {
73 super(new SessionHandle("vamsasfile://"+vsess), new ClientHandle("ArchiveClient","0.01"), new UserHandle(username, organization));
77 public ArchiveClient(String username, String organization, String clientName, String clientVersion, File vsess) {
78 super(new SessionHandle("vamsasfile://"+vsess), new ClientHandle(clientName, clientVersion), new UserHandle(username, organization));
84 throw new Error("ArchiveClient instance is invalid!.");
88 * watch the document file for updates.
89 * @param time - length of time to watch for.
90 * @return read only IO interface for session document.
92 public ClientDoc watch(long time) {
94 FileWatcher watcher = new FileWatcher(vsess.getVamsasFile());
95 long endtime=System.currentTimeMillis()+time;
97 org.vamsas.client.simpleclient.Lock doclock;
100 Thread.sleep(50); // tuning.
101 doclock=watcher.getChangedState();
102 } while (doclock==null && (time==0 || endtime>System.currentTimeMillis()));
106 doclock = vsess.getLock(doclock);
107 VamsasArchiveReader varc = new VamsasArchiveReader(vsess.getVamsasFile());
108 return _getReadonly(varc);
110 } catch (Exception e) {
111 log.error("Whilst watching file "+vsess.getVamsasFile(), e);
116 // from ClientDocument.getClientAppdata
117 private AppData[] getAppData(VamsasDocument doc) {
119 log.debug("extractAppData called for null document object");
122 AppData appsGlobal=null, usersData=null;
123 Vector apldataset = AppDataReference.getUserandApplicationsData(
124 doc, this.getUserHandle(), this.getClientHandle());
125 if (apldataset!=null) {
126 if (apldataset.size()>0) {
127 AppData clientdat = (AppData) apldataset.get(0);
128 if (clientdat instanceof ApplicationData) {
129 appsGlobal = (ApplicationData) clientdat;
130 if (apldataset.size()>1) {
131 clientdat = (AppData) apldataset.get(1);
132 if (clientdat instanceof User) {
133 usersData = (User) clientdat;
135 if (apldataset.size()>2)
136 log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
139 log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
141 apldataset.removeAllElements(); // destroy references.
144 return new AppData[] { appsGlobal, usersData};
147 protected ClientDoc _getReadonly(VamsasArchiveReader vreader) throws IOException, ValidationException, MarshalException {
150 SimpleDocBinding docb = new SimpleDocBinding();
153 d = docb.getVamsasDocument(vreader);
156 ClientDoc creader = new ClientDoc(d, null, vreader, getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
164 * @return user field for a provenance entry
166 protected String getProvenanceUser() {
167 return new String(getUserHandle().getFullName()+" ["+getClientHandle().getClientUrn()+"]");
170 public ClientDoc getUpdateable() {
174 VamsasArchive varc = new VamsasArchive(vsess, true, false); // read archive, write as vamsasDocument, don't erase original contents.
176 VamsasDocument d = varc.getVamsasDocument(getProvenanceUser(), "Created new document.", VersionEntries.latestVersion()); // VAMSAS: provenance user and client combined
179 log.warn("Backing out from opening a VamsasArchive writable IO session");
180 varc.cancelArchive();
183 ClientDoc cdoc = new ClientDoc(d, varc, varc.getOriginalArchiveReader(), getClientHandle().getClientUrn(), getProvenanceUser(), getVorbaIdHash());
186 } catch (Exception e) {
187 log.error("Failed to get Updateable version of "+vsess.getVamsasFile(), e);
192 * trust client to not do anything stupid to the document roots which will now be written to the archive.
194 * @return true if write was a success.
196 public boolean doUpdate(ClientDoc cdoc) {
199 log.warn("Invalid ClientDoc passed to org.vamsas.test.simpleclient.doUpdate()");
202 if (cdoc.iohandler==null) {
203 log.warn("Read only ClientDoc object passed to org.vamsas.test.simpleclient.doUpdate()");
206 if (cdoc.iohandler.getVorba()!=this) {
207 log.error("Mismatch between ClientDoc instances and ArchiveClient instances!");
211 // do any appDatas first.
212 if (cdoc.iohandler.transferRemainingAppDatas())
213 log.debug("Remaining appdatas were transfered.");
214 cdoc.iohandler.putVamsasDocument(cdoc.doc);
215 cdoc.iohandler.closeArchive();
219 } catch (Exception e) {
220 log.warn("While updating archive in "+vsess.getVamsasFile(),e);
228 public static void usage() {
229 throw new Error("Usage: Username Organization VamsasFile [command,args]*");
231 public static void main(String[] args) {
236 ArchiveClient client = new ArchiveClient(args[0],args[1], new File(args[2]));
240 cdoc = client.getUpdateable();
241 // ArchiveReports.reportDocument(cdoc.doc, cdoc.getReader(), true, System.out);
242 System.out.println("Report Roots :");
243 ArchiveReports.rootReport(cdoc.getVamsasRoots(), true, System.out);
244 cdoc.addVamsasRoot(Core.getDemoVamsas());
245 System.out.println("Doing update.");
246 client.doUpdate(cdoc);
250 System.out.println("Watch for more... ("+u+" left)");
251 cdoc = client.watch(0);
253 System.out.println("****\nUpdate detected at "+new Date());
254 ArchiveReports.reportDocument(cdoc.doc, cdoc.getReader(), true, System.out);
256 System.out.println("!!!! Null document update detected at "+new Date());
260 catch (Exception e) {
261 client.log.error("Broken!", e);
263 System.out.println("Finished at "+new Date());
265 public org.vamsas.client.Vobject getObject(VorbaId id) {
266 Hashtable idhash = this.getVorbaIdHash();
267 if (idhash!=null && idhash.containsKey(id))
268 return (Vobject) idhash.get(id);