From 6a22a06cc820ff240f5b37866b768fd7c0df16d7 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 13 Sep 2006 14:24:57 +0000 Subject: [PATCH] bug fix to ensure valid and vorbaIds are generated and stored in vamsas object model git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@242 be28352e-c001-0410-b1a7-c7978e42abec --- src/org/vamsas/client/Vobject.java | 40 ++++++++++++++++++++- src/org/vamsas/client/VorbaXmlBinder.java | 12 +++---- src/org/vamsas/client/simpleclient/IdFactory.java | 2 +- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/org/vamsas/client/Vobject.java b/src/org/vamsas/client/Vobject.java index 1c7388f..d120e6c 100644 --- a/src/org/vamsas/client/Vobject.java +++ b/src/org/vamsas/client/Vobject.java @@ -71,7 +71,7 @@ public abstract class Vobject { super(); testInstanceForIdField(); } - + java.lang.reflect.Field ___id_field=null; // set to ease pain of reflection /** * set the isRegisterable flag based on the presence of a 'private String _id' field in * the reflected class instance. @@ -86,6 +86,7 @@ public abstract class Vobject { try { java.lang.reflect.Field fd = thisclass.getDeclaredField("_id"); if (String.class.isAssignableFrom(fd.getType())) { + ___id_field=fd; this.setRegisterable(true); break; } @@ -132,6 +133,43 @@ public abstract class Vobject { + this.getClass().toString() + " (which cannot be given a vorbaId)"); } } + + protected String __getInstanceIdField() { + /*if (!registerable && !__testedInstance) { + testInstanceForIdField(); + __testedInstance=true; + }*/ + if (registerable) { + if (__vorba != null) + try { + Method fd = this.getClass().getMethod("getId", (Class[]) null); + Object idstring = fd.invoke((Object) this, (Object[]) null); + log.debug(this.getClass().getName()+" called setInstanceVorbaIdField!"); + if (idstring!=null && idstring instanceof String) { + if (((String) idstring).length()>0) + return (String) idstring; + } + } catch (InvocationTargetException e) { + log.error("SourceGeneration of " + + this.getClass().toString() + + "\n has resulted in an inaccessible 'getId' method!\nCannot set ID from the vorbaId Vobject.", e); + } + catch (IllegalAccessException e) { + log.error("SourceGeneration of " + + this.getClass().toString() + + "\n has resulted in an inaccessible 'getId' method!\nCannot set ID from the vorbaId Vobject.", e); + } catch (SecurityException e) { + log.error("Security access violation for "+this.getClass().toString(),e); + } catch (NoSuchMethodException e) { + log.warn(this.getClass().toString()+" was erroneously marked as a Vorba Vobject class (Implementation error?)"); + this.setRegisterable(false); + } + } else { + System.err.println("Client error. Trying to getInstanceIdField on a " + + this.getClass().toString() + " (which cannot be given a vorbaId)"); + } + return null; + } /** * calculate a hash for the Vobject with all housekeeping fields at standard diff --git a/src/org/vamsas/client/VorbaXmlBinder.java b/src/org/vamsas/client/VorbaXmlBinder.java index 0f4feb1..c73d158 100644 --- a/src/org/vamsas/client/VorbaXmlBinder.java +++ b/src/org/vamsas/client/VorbaXmlBinder.java @@ -86,13 +86,13 @@ public class VorbaXmlBinder implements UnmarshalListener { nobj.set__stored_in_document(true); Field fd = null; try { - if (nobj.isRegisterable()) { + if (nobj.isRegisterable() && nobj.___id_field!=null) { // look for the id field (should be an NCName string) nobj.__vorba = vorbafactory; - fd = nobj.getClass().getDeclaredField("_id"); - String idstring; - if (fd.get(nobj) != null) { - idstring = (String) fd.get(nobj); + 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); @@ -106,7 +106,7 @@ public class VorbaXmlBinder implements UnmarshalListener { obj.add(nobj); } } else { - // add to list of objects without a valid vorbaId + // TODO: add to list of objects without a valid vorbaId obj.add(nobj); } diff --git a/src/org/vamsas/client/simpleclient/IdFactory.java b/src/org/vamsas/client/simpleclient/IdFactory.java index 0f3cc36..80f8f28 100644 --- a/src/org/vamsas/client/simpleclient/IdFactory.java +++ b/src/org/vamsas/client/simpleclient/IdFactory.java @@ -49,7 +49,7 @@ public class IdFactory extends VorbaIdFactory { unique.reset(); unique.update(new Object[] { session, client, user}.toString().getBytes()); // TODO: Ensure format of URNs and use standard composition methods. - idstring = client.getClientUrn()+":"+unique.getValue()+"/"; + idstring = client.getClientName()+":"+unique.getValue()+"."; extantids=new Hashtable(); } /** -- 1.7.10.2