todo
[vamsas.git] / src / org / vamsas / client / object.java
1 /**
2  * 
3  */
4 package org.vamsas.client;
5
6 import java.lang.reflect.Field;
7 import java.lang.reflect.InvocationTargetException;
8 import java.lang.reflect.Method;
9 import java.util.Iterator;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.exolab.castor.mapping.FieldDescriptor;
14 import org.exolab.castor.mapping.FieldHandler;
15 import org.exolab.castor.xml.util.XMLClassDescriptorImpl;
16 import org.vamsas.test.simpleclient.VamsasArchive;
17
18 /**
19  * Base class for all Vamsas objects extracted from an IClientDocument. An
20  * object maybe registered or unregistered.
21  * 
22  * @author jimp
23  * 
24  */
25 public abstract class object {
26   static Log log = LogFactory.getLog(object.class);
27   
28   /**
29    * true if object was stored in a vamsas Document or has been retrieved from it
30    */
31   protected boolean __stored_in_document = false;
32   /**
33    * memory of the last doHash() value computed for the object 
34    * @see doHash()
35    */
36   protected long __last_hash = 0; 
37   /**
38    * set by testInstanceForIdField() if object should have a VorbaId
39    */
40   protected boolean registerable = false; 
41
42
43   /**
44    * unique id for all vamsas objects allows unambiguous referencing to any
45    * object in the vamsas document
46    */
47   protected VorbaId vorbaId = null;
48
49   /**
50    * the source of unique VorbaIds.
51    */
52   protected IVorbaIdFactory __vorba = null;
53
54   /**
55    * 
56    */
57   protected object() {
58     super();
59     testInstanceForIdField();
60   }
61
62   /**
63    * set the isRegisterable flag based on the presence of a 'private String _id' field in
64    * the reflected class instance.
65    */
66   private void testInstanceForIdField() {
67     // look for the id field (should be an NCName string)
68     // TODO: decide if 'id' is an appropriate reserved attribute name for the VorbaId
69     try {
70       java.lang.reflect.Field fd = this.getClass().getDeclaredField("_id");
71       if (String.class.isAssignableFrom(fd.getType())) {
72         this.setRegisterable(true);
73       }
74     } catch (SecurityException e) {
75       e.printStackTrace();
76     } catch (NoSuchFieldException e) {
77       this.setRegisterable(false);
78     }
79   }
80
81   /**
82    * update the object instance's _id field, based on the contents of the
83    * VorbaId. Only call this if you mean to do it!
84    */
85   protected void setInstanceIdField() {
86     if (registerable) {
87       if (__vorba != null)
88         try {
89           Method fd = this.getClass().getMethod("setId", new Class[] { String.class });
90           fd.invoke((Object) this, new Object[] {new String(this.getVorbaId().id)});
91           log.debug(this.getClass().getName()+" called setInstanceIdField!");
92         } catch (InvocationTargetException e) { 
93           log.error("SourceGeneration of "
94               + this.getClass().toString()
95               + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object.", e);
96         }
97         catch (IllegalAccessException e) {
98           log.error("SourceGeneration of "
99                   + this.getClass().toString()
100                   + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object.", e);
101         } catch (SecurityException e) {
102           log.error("Security access violation for "+this.getClass().toString(),e);
103         } catch (NoSuchMethodException e) {
104           log.warn(this.getClass().toString()+" was erroneously marked as a Vorba object class (Implementation error?)");
105           this.setRegisterable(false);
106         }
107     } else {
108       System.err.println("Client error. Trying to setInstanceIdField on a "
109           + this.getClass().toString() + " (which cannot be given a vorbaId)");
110     }
111   }
112
113   /**
114    * calculate a hash for the object with all housekeeping fields at standard
115    * values. (isRegisterable is an immutable attribute property)
116    * TODO: decide if __stored_in_document should be included in the hash or not.
117    * @return true if new hash different to last hash
118    */
119   synchronized protected boolean doHash() {
120     long __old_hash = __last_hash;
121     __last_hash = 0;
122     VorbaId thisid = vorbaId;
123     IVorbaIdFactory factory = __vorba;
124     boolean stored = __stored_in_document;
125     vorbaId = null;
126     __vorba = null;
127     __last_hash = this.hashCode();
128     vorbaId = thisid;
129     __vorba = factory;
130     __stored_in_document = stored;
131     return (__old_hash==0) || (__old_hash == __last_hash);
132   }
133
134   /**
135    * TODO: combine two versions of the same collection object to resolve
136    * asynchronous updates to the same vamsas object Merges two vamsas objects,
137    * one of which is a later version of the earlier (ie they have the same
138    * vorbaId but one is a later version recently read from the vamsasDocument
139    * collection.
140    * 
141    * @return
142    */
143   protected boolean merge(object laterCopy) {
144     log.warn(this.getClass().getName()+".merge() not implemented.");
145     return true;
146   }
147
148   /**
149    * 
150    * @return true if object is registered
151    */
152   public boolean isRegistered() {
153     return (registerable) ? (vorbaId != null) : false;
154   }
155
156   /**
157    * Method to get fixed reference for the object in the vamsas document.
158    * 
159    * @returns null if object is neither registered or not associated with a
160    *          properly instantiated VorbaIdFactory.
161    */
162   public VorbaId getVorbaId() {
163     if (registerable && vorbaId == null) {
164       // Try to use the associated factory.
165       if (__vorba != null)
166         if ((vorbaId = __vorba.makeVorbaId()) == null)
167           return null; // Factory not valid.
168         else {
169           this.setInstanceIdField();
170           return vorbaId;
171         }
172     }
173     return vorbaId;
174   }
175
176   /**
177    * used by the IClient implementation to generate unique Id based on client
178    * applications current namespace.
179    */
180   protected void setVorbaId(VorbaId newid) {
181     vorbaId = newid;
182   }
183
184   /**
185    * @return true if object is present in Vamsas Document.
186    */
187   public boolean is__stored_in_document() {
188     return __stored_in_document;
189   }
190
191   /**
192    * for use by Vorba agent to reflect state of vamsas object to client
193    * application.
194    * Setting stored_in_document on a registerable object without a 
195    * vorbaId will mean is will *never* get a vorbaId and 
196    * horrible things will happen.
197    * @param __stored_in_document true if object has been marshalled into current document.
198    */
199   protected void set__stored_in_document(boolean __stored_in_document) {
200     this.__stored_in_document = __stored_in_document;
201   }
202
203   /**
204    * __last_hash is the hash value computed when the object was last checked
205    * against a IClientDocument generated by the object's parent IClient
206    * instance.
207    * 
208    * @return Returns the __last_hash.
209    */
210   public long get__last_hash() {
211     return __last_hash;
212   }
213
214   /**
215    * @return true if object can have a vorbaId
216    */
217   public boolean isRegisterable() {
218     return registerable;
219   }
220
221   /**
222    * Called by __testInstanceForidField and the post-unmarshalling handler
223    * to indicate if object will have a vorbaId.
224    * @param registerable 
225    */
226   protected void setRegisterable(boolean registerable) {
227     this.registerable = registerable;
228   }
229   /**
230    * ensure's internal id field corresponds to vorbaId and
231    * cascade through all fields referring to an instance of object
232    * calling the same method on them.
233    * TODO: LATER: properly apply castors own field mechanisms to get at accessors
234    * TODO: FIX CYCLIC __ensure+instance_ids
235    * Implementation note for the todo:
236    * this works like a depth-first search over all vamsas objects in an vamsasDocument. 
237    * The doHash() function is used as the 'visited' flag - 
238    * this *is not* a valid heuristic, although it will work "most of the time". 
239    */
240   protected void __ensure_instance_ids() {
241     if (__vorba==null)
242       throw new Error("Improperly intialised org.vamsas.client.object - no VorbaFactory given.");
243     log.debug("doing "+this.getClass()+".__ensure_instance_ids()");
244     if (!__stored_in_document && registerable)
245       setInstanceIdField();
246     if (!doHash())
247       return; // nothing has changed in this object - probably visited it before.
248     Class descriptor = null;
249     XMLClassDescriptorImpl descimpl = null;
250     try {
251       // castor descriptor resolver magic
252       descriptor = this.getClass().getClassLoader().loadClass(this.getClass().getName()+"Descriptor");
253       descimpl = (XMLClassDescriptorImpl) descriptor.getConstructor(null).newInstance(null);
254     } catch (Exception e) {
255       log.fatal("Source Generation Error!: Couldn't resolve descriptor for "
256           +this.getClass().getName()
257           +" was 'generate descriptors' set for castorbuilder.properties?");
258       return;
259     }
260     FieldDescriptor fields[] = descimpl.getFields();
261     for (int i=0,j=fields.length; i<j; i++) {
262       Class type= fields[i].getFieldType();
263       if (type.isArray()) {
264         if (object[].class.isAssignableFrom(type)) {
265           try {
266             Object val = fields[i].getHandler().getValue(this);
267             if (val!=null) {
268               object vals[] = (object[]) val; 
269               for (int k=0, l=vals.length; k<l; k++) {
270                 if (vals[k].__vorba==null)
271                   vals[k].__vorba = __vorba; // propagate IVorbaIdFactory
272                 vals[k].__ensure_instance_ids();
273               }
274             }
275           }
276           catch (Exception e) {
277             log.error("Client error - could not access array "+type.getName()+" in "+this.getClass().getName(), e);
278           }
279         }
280       } else
281         if (object.class.isAssignableFrom(type)) {
282           try {
283             FieldHandler fh = fields[i].getHandler();
284             object rf = null;
285             if (fh != null) {
286               Object fval = fh.getValue(this);
287               if (fval!=null) {
288                 if (fval.getClass().isArray()) {
289                   //if (object[].class.isAssignableFrom(type)) {
290                     try {
291                       object vals[] = (object[]) fval; 
292                       for (int k=0, l=vals.length; k<l; k++) {
293                         if (vals[k].__vorba==null)
294                           vals[k].__vorba = __vorba; // propagate IVorbaIdFactory
295                         vals[k].__ensure_instance_ids();
296                       }
297                     }
298                     catch (Exception e) {
299                       log.error("Client error - could not access (fhval)array "+type.getName()+" in "+this.getClass().getName(), e);
300                     }
301                   //}
302                 } else {
303                   rf = (object) fval;
304                   log.debug("Got value for "+fields[i].getFieldName());
305                 }
306               }
307             } else {
308               // fuck around, fuck around, jump up jump up and get down! */
309               Object o = fields[i].getClassDescriptor();
310               if (o!=null) {
311                 // XMLClassDescriptorImpl fclasdes = (XMLClassDescriptorImpl) o;
312                 String methname = "get"+fields[i].getFieldName();
313                 Method fgetmeth = this.getClass().getMethod(methname,null);
314                 if (fgetmeth!=null) {
315                   Object fval = fgetmeth.invoke(this,null);
316                   if (fval!=null)
317                     rf = (object) fval;
318                 } else {
319                   log.warn("Couldn't find "+this.getClass().getName()+"."+methname);
320                 }
321               }
322             }
323             if (rf!=null) {
324               if (rf.__vorba==null)
325                 rf.__vorba = __vorba; // propagate IVorbaIdFactory
326               rf.__ensure_instance_ids();
327             }
328           }
329           catch (Exception e) {
330             log.error("Client error - could not access "+type.getName()+" in "+this.getClass().getName(), e);
331           }
332         }
333     }
334     
335   }
336 }