a2d89125f6c85ff122aac5425543607a7f02f781
[vamsas.git] / src / org / vamsas / client / simpleclient / SimpleClientAppdata.java
1 /**
2  * 
3  */
4 package org.vamsas.client.simpleclient;
5
6 import java.io.BufferedInputStream;
7 import java.io.BufferedOutputStream;
8 import java.io.ByteArrayInputStream;
9 import java.io.ByteArrayOutputStream;
10 import java.io.DataInput;
11 import java.io.DataInputStream;
12 import java.io.DataOutput;
13 import java.io.DataOutputStream;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Vector;
18 import java.util.jar.JarEntry;
19 import java.util.jar.JarInputStream;
20 import java.util.jar.JarOutputStream;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.vamsas.client.IClientAppdata;
25 import org.vamsas.objects.core.AppData;
26 import org.vamsas.objects.core.ApplicationData;
27 import org.vamsas.objects.core.User;
28 import org.vamsas.objects.utils.AppDataReference;
29
30 /**
31  * @author jimp
32  * Access interface to data chunks read from a VamsasArchiveReader stream 
33  * (or byte buffer input stream) or written to a VamsasArchive stream.
34  * // TODO: get VamsasArchiveReader from sclient
35  */
36 public class SimpleClientAppdata implements IClientAppdata {
37   private static Log log = LogFactory.getLog(SimpleClientAppdata.class);
38   /**
39    * has the session's document been accessed to get the AppData entrys?
40    */
41   protected boolean accessedDocument = false;
42   /**
43    * has the user datablock been modified ? 
44    * temporary file containing new user specific application data chunk
45    */
46   SessionFile newUserData=null;
47   JarOutputStream newUserDataStream = null;
48   /**
49    * has the apps global datablock been modified ?
50    * temporary file containing new global application data chunk
51    */
52   SessionFile newAppData=null;
53   JarOutputStream newAppDataStream=null;
54   /**
55    * set by extractAppData
56    */
57   protected ApplicationData appsGlobal = null;
58   /**
59    * set by extractAppData
60    */
61   protected User usersData = null;
62   
63   ClientDocument clientdoc;
64   /**
65    * state flags
66    * - accessed ClientAppdata
67    * - accessed UserAppdata
68    * => inputStream from embedded xml or jar entry of backup has been created
69    * - set ClientAppdata
70    * - set UserAppdata
71    * => an output stream has been created and written to - or a data chunk has been written.
72    *  - need flag for switching between embedded and jar entry mode ? - always write a jar entry for a stream.
73    *  - need code for rewind and overwriting if the set*Appdata methods are called more than once.
74    *  - need flags for streams to except a call to set*Appdata when an output stream exists and is open.
75    *  - need 
76    * @param clientdoc The ClientDocument instance that this IClientAppData is accessing
77    */
78   protected SimpleClientAppdata(ClientDocument clientdoc) {
79     if (clientdoc==null) {
80       log.fatal("Implementation error - Null ClientDocument for SimpleClientAppdata construction.");
81       throw new Error("Implementation error - Null ClientDocument for SimpleClientAppdata construction.");
82     }
83     this.clientdoc = clientdoc;
84   }
85   /**
86    * gets appropriate app data for the application, if it exists in this dataset
87    * Called by every accessor to ensure data has been retrieved from document.
88    */
89   private void extractAppData(org.vamsas.objects.core.VamsasDocument doc) {
90     if (doc==null) {
91       log.debug("extractAppData called for null document object");
92       return;
93     }
94     if (accessedDocument) {
95       return;
96     }
97     Vector apldataset = AppDataReference.getUserandApplicationsData(
98         doc, clientdoc.sclient.getUserHandle(), clientdoc.sclient.getClientHandle());
99     accessedDocument = true;
100     if (apldataset!=null) {
101       if (apldataset.size()>0) {
102         AppData clientdat = (AppData) apldataset.get(0);
103         if (clientdat instanceof ApplicationData) {
104           appsGlobal = (ApplicationData) clientdat;
105           if (apldataset.size()>1) {
106             clientdat = (AppData) apldataset.get(1);
107             if (clientdat instanceof User) {
108               usersData = (User) clientdat;
109             }
110             if (apldataset.size()>2)
111               log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
112           } 
113         } else {
114           log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
115         }
116         apldataset.removeAllElements(); // destroy references.
117       }
118     }
119   }
120   /**
121    * LATER: generalize this for different low-level session implementations (it may not always be a Jar)
122    * @param appdata
123    * @param docreader
124    * @return
125    */
126   private JarInputStream getAppDataStream(AppData appdata, VamsasArchiveReader docreader) {
127     String entryRef = appdata.getDataReference();
128     if (entryRef!=null) {
129       log.debug("Resolving appData reference +"+entryRef);
130       InputStream entry = docreader.getAppdataStream(entryRef);
131       if (entry!=null) {
132         if (entry instanceof JarInputStream) {
133           return (JarInputStream) entry;
134         }
135         log.warn("Implementation problem - docreader didn't return a JarInputStream entry.");
136       }
137     } else {
138       log.debug("GetAppDataStream called for an AppData without a data reference.");
139     }
140     return null;
141   }
142   /**
143    * yuk - size of buffer used for slurping appData JarEntry into a byte array.
144    */
145   private final int _TRANSFER_BUFFER=4096*4;
146
147   /**
148    * Resolve AppData object to a byte array.
149    * @param appdata
150    * @param archiveReader
151    * @return null or the application data as a byte array
152    */
153   private byte[] getAppDataAsByteArray(AppData appdata, VamsasArchiveReader docreader) {
154     if (appdata.getData()==null) {
155       if (docreader==null) {
156         log.warn("Silently failing getAppDataAsByteArray with null docreader.",new Exception());
157         return null;
158       }
159       // resolve and load data
160       JarInputStream entry = getAppDataStream(appdata, docreader); 
161       ByteArrayOutputStream bytes = new ByteArrayOutputStream();
162       try {
163         byte buff[] = new byte[_TRANSFER_BUFFER];
164         int olen=0;
165         while (entry.available()>0) {
166           int len = entry.read(buff, olen, _TRANSFER_BUFFER);
167           bytes.write(buff, 0, len);
168           olen+=len;
169         }
170         buff=null;
171       } catch (Exception e) {
172         log.warn("Unexpected exception - probable truncation when accessing VamsasDocument entry "+appdata.getDataReference(), e);
173       }
174       if (bytes.size()>0) {
175         // LATER: deal with probable OutOfMemoryErrors here
176         log.debug("Got "+bytes.size()+" bytes from AppDataReference "+appdata.getDataReference());
177         byte data[] = bytes.toByteArray();
178         bytes = null;
179         return data;
180       }
181       return null;
182     } else {
183       log.debug("Returning inline AppData block for "+appdata.getVorbaId());
184       return appdata.getData();
185     }
186   }
187   /**
188    * internal method for getting a DataInputStream from an AppData object.
189    * @param appdata
190    * @param docreader
191    * @return data in object or null if no data is accessible
192    */
193   private DataInput getAppDataAsDataInputStream(AppData appdata, VamsasArchiveReader docreader) {
194     if (appdata!=null && docreader!=null) {
195       String entryRef = appdata.getDataReference();
196       if (entryRef!=null) {
197         log.debug("Resolving AppData reference for "+entryRef);
198         InputStream jstrm = docreader.getAppdataStream(entryRef);
199         if (jstrm!=null)
200           return new AppDataInputStream(jstrm);
201         else {
202           log.debug("Returning null input stream for unresolved reference ("+entryRef+") id="+appdata.getVorbaId());
203           return null;
204         }
205       } else {
206         // return a byteArray input stream
207         byte[] data=appdata.getData();
208         if (data.length>0) {
209           ByteArrayInputStream stream = new ByteArrayInputStream(data);
210           return new DataInputStream(stream);
211         } else {
212           log.debug("Returning null input stream for empty Appdata data block in id="+appdata.getVorbaId());
213           return null;
214         }
215       }
216     } else {
217       log.debug("Returning null DataInputStream for appdata entry:"+appdata.getVorbaId());
218     }
219     return null;
220   }
221
222   /**
223    * internal method for getting ByteArray from AppData object
224    * @param clientOrUser - true for returning userData, otherwise return Client AppData.
225    * @return null or byte array
226    */
227   private byte[] _getappdataByteArray(boolean clientOrUser) {
228     if (clientdoc==null)
229       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
230     byte[] data=null;
231     String appdName;
232     if (!clientOrUser) {
233       appdName = "Client's Appdata";
234     } else {
235       appdName = "User's Appdata";
236     }    
237     log.debug("getting "+appdName+" as a byte array");
238     extractAppData(clientdoc.getVamsasDocument());
239     AppData object;
240     if (!clientOrUser) {
241       object = appsGlobal;
242     } else {
243       object = usersData;
244     }
245     if (object!=null) {
246       log.debug("Trying to resolve "+appdName+" object to byte array.");
247       data = getAppDataAsByteArray(object, clientdoc.getVamsasArchiveReader());
248     }
249     if (data == null)
250       log.debug("Returning null for "+appdName+"ClientAppdata byte[] array");
251     return data;
252     
253   }
254   
255   /**
256    * common method for Client and User AppData->InputStream accessor
257    * @param clientOrUser - the appData to resolve - false for client, true for user appdata.
258    * @return null or the DataInputStream desired.
259    */
260   private DataInput _getappdataInputStream(boolean clientOrUser) {
261     if (clientdoc==null)
262       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
263     String appdName;
264     if (!clientOrUser) {
265       appdName = "Client's Appdata";
266     } else {
267       appdName = "User's Appdata";
268     }
269     if (log.isDebugEnabled())
270       log.debug("getting "+appdName+" as an input stream.");
271     extractAppData(clientdoc.getVamsasDocument());
272     AppData object;
273     if (!clientOrUser) {
274       object = appsGlobal;
275     } else {
276       object = usersData;
277     }
278     if (object!=null) {
279       log.debug("Trying to resolve ClientAppdata object to an input stream.");
280       return getAppDataAsDataInputStream(object, clientdoc.getVamsasArchiveReader());
281     }
282     log.debug("getClientInputStream returning null.");
283     return null;
284   }
285   /* (non-Javadoc)
286    * @see org.vamsas.client.IClientAppdata#getClientAppdata()
287    */
288   public byte[] getClientAppdata() {
289     return _getappdataByteArray(false);
290   }
291   /* (non-Javadoc)
292    * @see org.vamsas.client.IClientAppdata#getClientInputStream()
293    */
294   public DataInput getClientInputStream() {
295     return _getappdataInputStream(false);
296   }
297
298   /* (non-Javadoc)
299    * @see org.vamsas.client.IClientAppdata#getUserAppdata()
300    */
301   public byte[] getUserAppdata() {
302     return _getappdataByteArray(true);
303   }
304
305   /* (non-Javadoc)
306    * @see org.vamsas.client.IClientAppdata#getUserInputStream()
307    */
308   public DataInput getUserInputStream() {
309     return _getappdataInputStream(true);
310   }
311   /**
312    * methods for writing new AppData entries.
313    */
314   private DataOutput _getAppdataOutputStream(boolean clientOrUser) {
315     String apdname;
316     SessionFile apdfile=null;
317     if (!clientOrUser) {
318       apdname = "clientAppData";
319       apdfile = newAppData;
320     } else {
321       apdname = "userAppData";
322       apdfile = newUserData;
323     }
324     try {
325       if (apdfile==null) {
326         apdfile=clientdoc.sclient._session.getTempSessionFile(apdname,".jar");
327         log.debug("Successfully made temp appData file for "+apdname);
328       } else {
329         // truncate to remove existing data.
330         apdfile.fileLock.rafile.setLength(0);
331         log.debug("Successfully truncated existing temp appData for "+apdname);
332       }
333       } catch (Exception e) {
334       log.error("Whilst opening temp file in directory "+clientdoc.sclient._session.sessionDir, e);
335     }
336     // we do not make another file for the new entry if one exists already
337     if (!clientOrUser) {
338       newAppData = apdfile;
339     } else {
340       newUserData = apdfile;
341     }
342     try {
343       apdfile.lockFile();
344       // LATER: Refactor these local AppDatastream IO stuff to their own class.
345       JarOutputStream dstrm = 
346         new JarOutputStream(
347             new BufferedOutputStream(new java.io.FileOutputStream(apdfile.sessionFile)));
348       if (!clientOrUser) {
349         newAppDataStream = dstrm;
350       } else {
351         newUserDataStream = dstrm;
352       }
353       dstrm.putNextEntry(new JarEntry("appData_entry.dat"));
354       // LATER: there may be trouble ahead if an AppDataOutputStream is written to by one thread when another truncates the file. This situation should be prevented if possible
355       return new AppDataOutputStream(dstrm);
356     }
357     catch (Exception e) {
358       log.error("Whilst opening jar output stream for file "+apdfile.sessionFile);
359     }
360     // tidy up and return null
361     apdfile.unlockFile();
362     return null;
363    }
364   /**
365    * copy data from the appData jar file to an appropriately 
366    * referenced jar or Data entry for the given ApplicationData
367    * Assumes the JarFile is properly closed. 
368    * @param vdoc session Document handler
369    * @param appd the AppData whose block is being updated
370    * @param apdjar the new data in a Jar written by this class
371    */
372   protected void updateAnAppdataEntry(VamsasArchive vdoc, AppData appd, SessionFile apdjar) throws IOException {
373     if (apdjar==null || apdjar.sessionFile==null || !apdjar.sessionFile.exists()) {
374       throw new IOException("No temporary Appdata to recover and transfer.");
375     }
376     if (vdoc==null) {
377       log.fatal("FATAL! NO DOCUMENT TO WRITE TO!");
378       throw new IOException("FATAL! NO DOCUMENT TO WRITE TO!");
379     }
380     log.debug("Recovering AppData entry from "+apdjar.sessionFile);
381     JarInputStream istrm = new JarInputStream(new BufferedInputStream(new FileInputStream(apdjar.sessionFile)));
382     JarEntry je=null;
383     while (istrm.available()>0 && (je=istrm.getNextJarEntry())!=null && !je.getName().equals("appData_entry.dat")) {
384       if (je!=null)
385         log.debug("Ignoring extraneous entry "+je.getName());
386     }
387     if (istrm.available()>0 && je!=null) {
388       log.debug("Found appData_entry.dat in Jar");
389       String ref = appd.getDataReference();
390       if (ref==null) {
391         throw new IOException("Null AppData.DataReference passed.");
392       }
393       if (vdoc.writeAppdataFromStream(ref, istrm)) {
394         log.debug("Entry updated successfully.");
395       } else {
396         throw new IOException("writeAppdataFromStream did not return true - expect future badness."); // LATER - verify why this might occur.
397       }
398     } else {
399       throw new IOException("Couldn't find appData_entry.dat in temporary jar file "+apdjar.sessionFile.getAbsolutePath());
400     }
401     istrm.close();
402   }
403   /* (non-Javadoc)
404    * @see org.vamsas.client.IClientAppdata#getClientOutputStream()
405    */
406   public DataOutput getClientOutputStream() {
407     if (clientdoc==null)
408       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
409     if (log.isDebugEnabled())
410       log.debug("trying to getClientOutputStream for "+clientdoc.sclient.client.getClientUrn());   
411     return _getAppdataOutputStream(false);
412   }
413
414   /* (non-Javadoc)
415    * @see org.vamsas.client.IClientAppdata#getUserOutputStream()
416    */
417   public DataOutput getUserOutputStream() {
418     if (clientdoc==null)
419       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
420     if (log.isDebugEnabled())
421       log.debug("trying to getUserOutputStream for ("
422           +clientdoc.sclient.getUserHandle().getFullName()+")"+clientdoc.sclient.client.getClientUrn());   
423     return _getAppdataOutputStream(true);
424   }
425
426   /* (non-Javadoc)
427    * @see org.vamsas.client.IClientAppdata#hasClientAppdata()
428    */
429   public boolean hasClientAppdata() {
430     if (clientdoc==null)
431       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
432     extractAppData(clientdoc.getVamsasDocument());
433     // LATER - check validity of a DataReference before we return true
434     if ((appsGlobal!=null) && (appsGlobal.getDataReference()!=null || appsGlobal.getData()!=null))
435       return true;
436     return false;
437   }
438
439   /* (non-Javadoc)
440    * @see org.vamsas.client.IClientAppdata#hasUserAppdata()
441    */
442   public boolean hasUserAppdata() {
443     if (clientdoc==null)
444       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
445     extractAppData(clientdoc.getVamsasDocument());
446     // LATER - check validity of a DataReference before we return true
447     if ((appsGlobal!=null) && (appsGlobal.getDataReference()!=null || appsGlobal.getData()!=null))
448       return true;
449     return false;
450   }
451   private boolean _writeAppDataStream(JarOutputStream ostrm, byte[] data) {
452     try {
453       if (data!=null && data.length>0) 
454         ostrm.write(data);
455       ostrm.closeEntry();
456       return true;
457     }
458     catch (Exception e) {
459       log.error("Serious! - IO error when writing AppDataStream to file "+newAppData.sessionFile, e);
460     }
461     return false;
462   }
463   /* (non-Javadoc)
464    * @see org.vamsas.client.IClientAppdata#setClientAppdata(byte[])
465    */
466   public void setClientAppdata(byte[] data) {
467     if (clientdoc==null)
468       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
469     _getAppdataOutputStream(false);
470     if (newAppDataStream==null) {
471       // LATER: define an exception for this ? - operation may fail even if file i/o not involved
472       log.error("Serious! - couldn't open new AppDataStream in session directory "+clientdoc.sclient._session.sessionDir);
473     } else {
474       _writeAppDataStream(newAppDataStream, data);
475       // LATER: deal with error case - do we make session read only, or what ?
476     }
477   }
478
479   /* (non-Javadoc)
480    * @see org.vamsas.client.IClientAppdata#setUserAppdata(byte[])
481    */
482   public void setUserAppdata(byte[] data) {
483     if (clientdoc==null)
484       throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
485     _getAppdataOutputStream(true);
486     if (newUserDataStream==null) {
487       // LATER: define an exception for this ? - operation may fail even if file i/o not involved
488       log.error("Serious! - couldn't open new UserDataStream in session directory "+clientdoc.sclient._session.sessionDir);
489     } else {
490       _writeAppDataStream(newUserDataStream, data);
491       // LATER: deal with error case - do we make session read only, or what ?
492     }
493   }
494   /**
495    * flush and close outstanding output streams. 
496    *  - do this before checking data length.
497    * @throws IOException
498    */
499   protected void closeForWriting() throws IOException {
500     if (newAppDataStream!=null) {
501       newAppDataStream.flush();
502       newAppDataStream.closeEntry();
503       newAppDataStream.close();
504     }
505     if (newUserDataStream!=null) {
506       newUserDataStream.flush();
507       newUserDataStream.closeEntry();
508       newUserDataStream.close();
509     }
510   }
511
512
513   /**
514    * 
515    * @return true if any AppData blocks have to be updated in session Jar
516    */
517   protected boolean isModified() {
518     // LATER differentiate between core xml modification and Jar Entry modification.
519     if (newAppData.sessionFile.exists() || newUserData.sessionFile.exists())
520       return true;
521     return false;
522   }
523   /* (non-Javadoc)
524    * @see java.lang.Object#finalize()
525    */
526   protected void finalize() throws Throwable {
527     if (newAppDataStream!=null) {
528       newAppDataStream = null;
529     }
530     if (newAppDataStream!=null) {
531       newUserDataStream = null;
532     }
533     if (newAppData!=null) {
534       newAppData.eraseExistence();
535       newAppData = null;
536     }
537     if (newUserData!=null) {
538       newUserData.eraseExistence();
539       newUserData = null;
540     }
541     super.finalize();
542   }
543
544 }