From 8a32f6df8762ecdace21ceb0e6412e9908e89661 Mon Sep 17 00:00:00 2001 From: jprocter Date: Sun, 17 Sep 2006 14:41:31 +0000 Subject: [PATCH] ensured that hash binding vorbaIds is passed to client document object for vorba object lookup methods. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@244 be28352e-c001-0410-b1a7-c7978e42abec --- src/org/vamsas/client/Vobject.java | 10 +++++----- src/org/vamsas/client/VorbaXmlBinder.java | 19 +++++++++---------- src/org/vamsas/client/simpleclient/IdFactory.java | 6 +++++- .../vamsas/client/simpleclient/VamsasArchive.java | 2 +- src/org/vamsas/test/simpleclient/ArchiveClient.java | 16 ++++++++++------ 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/org/vamsas/client/Vobject.java b/src/org/vamsas/client/Vobject.java index d120e6c..589b7ff 100644 --- a/src/org/vamsas/client/Vobject.java +++ b/src/org/vamsas/client/Vobject.java @@ -144,7 +144,7 @@ public abstract class Vobject { try { Method fd = this.getClass().getMethod("getId", (Class[]) null); Object idstring = fd.invoke((Object) this, (Object[]) null); - log.debug(this.getClass().getName()+" called setInstanceVorbaIdField!"); + log.debug(this.getClass().getName()+" called getInstanceIdField!"); if (idstring!=null && idstring instanceof String) { if (((String) idstring).length()>0) return (String) idstring; @@ -224,11 +224,11 @@ public abstract class Vobject { * properly instantiated VorbaIdFactory. */ public VorbaId getVorbaId() { - /*if (!registerable && !__testedInstance) { - testInstanceForIdField(); - __testedInstance=true; - }*/ if (registerable && vorbaId == null) { + if (this.__stored_in_document) { + if (__vorba!=null) + vorbaId=org.vamsas.client.VorbaId.newId(this.__getInstanceIdField()); + } // Try to use the associated factory. if (__vorba != null) if ((vorbaId = __vorba.makeVorbaId(this)) == null) diff --git a/src/org/vamsas/client/VorbaXmlBinder.java b/src/org/vamsas/client/VorbaXmlBinder.java index c73d158..2cde9d2 100644 --- a/src/org/vamsas/client/VorbaXmlBinder.java +++ b/src/org/vamsas/client/VorbaXmlBinder.java @@ -84,23 +84,20 @@ public class VorbaXmlBinder implements UnmarshalListener { if (newobj instanceof Vobject) { Vobject nobj = (Vobject) newobj; nobj.set__stored_in_document(true); - Field fd = null; try { if (nobj.isRegisterable() && nobj.___id_field!=null) { // look for the id field (should be an NCName string) nobj.__vorba = vorbafactory; - fd = nobj.___id_field; // use the Vobject accessor method to avoid unpleasant security exceptions. String idstring = nobj.__getInstanceIdField(); if (idstring!=null) { if (idstring.length() > 0) { - if (!objrefs.containsKey(idstring)) { - objrefs.put(idstring, nobj); - nobj.setVorbaId(VorbaId.newId(idstring)); - } else { + nobj.setVorbaId(VorbaId.newId(idstring)); + if (objrefs.containsKey(nobj.getVorbaId()) && !objrefs.get(nobj.getVorbaId()).equals(nobj)) { System.err.println("Serious problem : duplicate id '"+idstring+"' found! expect badness."); // TODO: HANDLE duplicate XML ids correctly } + objrefs.put(nobj.getVorbaId(), nobj); } else { // add to list of objects without a valid vorbaId obj.add(nobj); @@ -150,9 +147,10 @@ public class VorbaXmlBinder implements UnmarshalListener { // forces registration and id field update. VorbaId id = o.getVorbaId(); if (!objrefs.containsKey(id)) { - objrefs.put(id.id, o); + objrefs.put(id, o); } else { - throw new Error("Serious! Duplicate reference made by vorbaIdFactory!"); + if (!objrefs.get(id).equals(o)) + throw new Error("Serious! Duplicate reference made by vorbaIdFactory!"); } } } @@ -173,7 +171,7 @@ public class VorbaXmlBinder implements UnmarshalListener { * @return null or {(Object) VamsasDocument Vobject, (Object) Hashtable of Vobject references, (Object) Boolean(sync) } */ public static Object[] getVamsasObjects(Reader instream, - IVorbaIdFactory factory, Vobject root) { + VorbaIdFactory factory, Vobject root) { Unmarshaller unmarshaller = new Unmarshaller(root); unmarshaller.setIDResolver(new IDResolver() { public Object resolve(String id) { @@ -185,7 +183,7 @@ public class VorbaXmlBinder implements UnmarshalListener { Hashtable refbase = new Hashtable(); Vector unrefed = new Vector(); final Hashtable objrefs = refbase; - final IVorbaIdFactory vorbafactory = factory; + final VorbaIdFactory vorbafactory = factory; final Vector unrefedObj = unrefed; unmarshaller.setUnmarshalListener(new VorbaXmlBinder(vorbafactory, unrefedObj, objrefs)); // Call the unmarshaller. @@ -195,6 +193,7 @@ public class VorbaXmlBinder implements UnmarshalListener { boolean sync=ensure_references(unrefed, objrefs); if (!(obj instanceof Vobject)) return null; + vorbafactory.setNewIdHash(objrefs); // update the Document IO Handler's set of vorbaId<>Object bindings. return new Object[] { obj, objrefs, new Boolean(sync)}; } } catch (MarshalException e) { diff --git a/src/org/vamsas/client/simpleclient/IdFactory.java b/src/org/vamsas/client/simpleclient/IdFactory.java index 80f8f28..dafd08e 100644 --- a/src/org/vamsas/client/simpleclient/IdFactory.java +++ b/src/org/vamsas/client/simpleclient/IdFactory.java @@ -66,6 +66,10 @@ public class IdFactory extends VorbaIdFactory { public VorbaId makeVorbaId(Vobject vobject) { if (session==null) throw new Error("makeVorbaId called on improperly initialised IdFactory Vobject!"); + if (!vobject.isRegisterable()) + throw new Error("makeVorbaId called on unregisterable object."); + if (vobject.isRegistered()) + throw new Error("makeVorbaId called on already registered object."); String newidstring; do { if (sequence>0) { @@ -77,7 +81,7 @@ public class IdFactory extends VorbaIdFactory { newidstring=idstring+Integer.toString(sequence); } while (extantids.containsKey(newidstring)); extantids.put(newidstring, vobject); // hash the Vobject by its new Id - VorbaId id = newId(newidstring); + VorbaId id = newId(newidstring); // VorbaId.hash()==newidstring.hash() so we can still recover vobject return id; } diff --git a/src/org/vamsas/client/simpleclient/VamsasArchive.java b/src/org/vamsas/client/simpleclient/VamsasArchive.java index e697861..c30c0bc 100644 --- a/src/org/vamsas/client/simpleclient/VamsasArchive.java +++ b/src/org/vamsas/client/simpleclient/VamsasArchive.java @@ -154,7 +154,7 @@ public class VamsasArchive { */ File originalBackup = null; - boolean donotdeletebackup=true; + boolean donotdeletebackup=false; private final int _TRANSFER_BUFFER=4096*4; protected SimpleDocument vorba = null; /** diff --git a/src/org/vamsas/test/simpleclient/ArchiveClient.java b/src/org/vamsas/test/simpleclient/ArchiveClient.java index f0b2e16..c681fe0 100644 --- a/src/org/vamsas/test/simpleclient/ArchiveClient.java +++ b/src/org/vamsas/test/simpleclient/ArchiveClient.java @@ -110,9 +110,9 @@ public class ArchiveClient extends IdFactory { if (doclock==null) return null; else { - doclock = vsess.getLock(doclock); - VamsasArchiveReader varc = new VamsasArchiveReader(vsess.getVamsasFile()); - return _getReadonly(varc); + return getUpdateable(vsess.getLock(doclock)); + /*VamsasArchiveReader varc = new VamsasArchiveReader(vsess.getVamsasFile()); + return _getReadonly(varc);*/ } } catch (Exception e) { log.error("Whilst watching file "+vsess.getVamsasFile(), e); @@ -175,12 +175,15 @@ public class ArchiveClient extends IdFactory { } public ClientDoc getUpdateable() { + return getUpdateable(null); + } + public ClientDoc getUpdateable(org.vamsas.client.simpleclient.Lock lock) { + getVorbaIdHash().clear(); valid(); try { // patiently wait for a lock on the document. long tries=5000; - org.vamsas.client.simpleclient.Lock lock; - while (((lock=vsess.getLock())==null || !lock.isLocked()) && --tries>0) { + while (lock==null && ((lock=vsess.getLock())==null || !lock.isLocked()) && --tries>0) { // Thread.sleep(1); log.debug("Trying to get a document lock for the "+tries+"'th time."); } @@ -227,6 +230,7 @@ public class ArchiveClient extends IdFactory { cdoc.updateDocumentRoots(); cdoc.iohandler.putVamsasDocument(cdoc.doc); cdoc.iohandler.closeArchive(); + this.extantids.clear();// we forget our ids after we close the document. cdoc.iohandler=null; cdoc = null; vsess.unLock(); @@ -263,7 +267,7 @@ public class ArchiveClient extends IdFactory { int u=5; while (--u>0) { System.out.println("Watch for more... ("+u+" left)"); - ClientDoc ucdoc = client.watch(50000); + ClientDoc ucdoc = client.watch(0000); if (ucdoc!=null) { System.out.println("****\nUpdate detected at "+new Date()); ArchiveReports.reportDocument(ucdoc.doc, ucdoc.getReader(), true, System.out); -- 1.7.10.2