4 package uk.ac.vamsas.client.simpleclient;
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;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
25 import uk.ac.vamsas.client.IClientAppdata;
26 import uk.ac.vamsas.objects.core.AppData;
27 import uk.ac.vamsas.objects.core.ApplicationData;
28 import uk.ac.vamsas.objects.core.User;
29 import uk.ac.vamsas.objects.utils.AppDataReference;
33 * Access interface to data chunks read from a VamsasArchiveReader stream
34 * (or byte buffer input stream) or written to a VamsasArchive stream.
35 * // TODO: get VamsasArchiveReader from sclient
37 public class SimpleClientAppdata implements IClientAppdata {
38 private static Log log = LogFactory.getLog(SimpleClientAppdata.class);
40 * has the session's document been accessed to get the AppData entrys?
42 protected boolean accessedDocument = false;
44 * has the user datablock been modified ?
45 * temporary file containing new user specific application data chunk
47 SessionFile newUserData=null;
48 JarOutputStream newUserDataStream = null;
50 * has the apps global datablock been modified ?
51 * temporary file containing new global application data chunk
53 SessionFile newAppData=null;
54 JarOutputStream newAppDataStream=null;
56 * set by extractAppData
58 protected ApplicationData appsGlobal = null;
60 * set by extractAppData
62 protected User usersData = null;
64 ClientDocument clientdoc;
67 * - accessed ClientAppdata
68 * - accessed UserAppdata
69 * => inputStream from embedded xml or jar entry of backup has been created
72 * => an output stream has been created and written to - or a data chunk has been written.
73 * - need flag for switching between embedded and jar entry mode ? - always write a jar entry for a stream.
74 * - need code for rewind and overwriting if the set*Appdata methods are called more than once.
75 * - need flags for streams to except a call to set*Appdata when an output stream exists and is open.
77 * @param clientdoc The ClientDocument instance that this IClientAppData is accessing
79 protected SimpleClientAppdata(ClientDocument clientdoc) {
80 if (clientdoc==null) {
81 log.fatal("Implementation error - Null ClientDocument for SimpleClientAppdata construction.");
82 throw new Error("Implementation error - Null ClientDocument for SimpleClientAppdata construction.");
84 this.clientdoc = clientdoc;
87 * gets appropriate app data for the application, if it exists in this dataset
88 * Called by every accessor to ensure data has been retrieved from document.
90 private void extractAppData(uk.ac.vamsas.objects.core.VamsasDocument doc) {
92 log.debug("extractAppData called for null document object");
95 if (accessedDocument) {
98 Vector apldataset = AppDataReference.getUserandApplicationsData(
99 doc, clientdoc.sclient.getUserHandle(), clientdoc.sclient.getClientHandle());
100 accessedDocument = true;
101 if (apldataset!=null) {
102 if (apldataset.size()>0) {
103 AppData clientdat = (AppData) apldataset.get(0);
104 if (clientdat instanceof ApplicationData) {
105 appsGlobal = (ApplicationData) clientdat;
106 if (apldataset.size()>1) {
107 clientdat = (AppData) apldataset.get(1);
108 if (clientdat instanceof User) {
109 usersData = (User) clientdat;
111 if (apldataset.size()>2)
112 log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query.");
115 log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName());
117 apldataset.removeAllElements(); // destroy references.
122 * LATER: generalize this for different low-level session implementations (it may not always be a Jar)
127 private JarInputStream getAppDataStream(AppData appdata, VamsasArchiveReader docreader) {
128 String entryRef = appdata.getDataReference();
129 if (entryRef!=null) {
130 log.debug("Resolving appData reference +"+entryRef);
131 InputStream entry = docreader.getAppdataStream(entryRef);
133 if (entry instanceof JarInputStream) {
134 return (JarInputStream) entry;
136 log.warn("Implementation problem - docreader didn't return a JarInputStream entry.");
139 log.debug("GetAppDataStream called for an AppData without a data reference.");
144 * yuk - size of buffer used for slurping appData JarEntry into a byte array.
146 private final int _TRANSFER_BUFFER=4096*4;
149 * Resolve AppData object to a byte array.
151 * @param archiveReader
152 * @return null or the application data as a byte array
154 private byte[] getAppDataAsByteArray(AppData appdata, VamsasArchiveReader docreader) {
155 if (appdata.getData()==null) {
156 if (docreader==null) {
157 log.warn("Silently failing getAppDataAsByteArray with null docreader.",new Exception());
160 // resolve and load data
161 JarInputStream entry = getAppDataStream(appdata, docreader);
162 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
164 byte buff[] = new byte[_TRANSFER_BUFFER];
166 while (entry.available()>0) {
167 int len = entry.read(buff, olen, _TRANSFER_BUFFER);
168 bytes.write(buff, 0, len);
172 } catch (Exception e) {
173 log.warn("Unexpected exception - probable truncation when accessing VamsasDocument entry "+appdata.getDataReference(), e);
175 if (bytes.size()>0) {
176 // LATER: deal with probable OutOfMemoryErrors here
177 log.debug("Got "+bytes.size()+" bytes from AppDataReference "+appdata.getDataReference());
178 byte data[] = bytes.toByteArray();
184 log.debug("Returning inline AppData block for "+appdata.getVorbaId());
185 return appdata.getData();
189 * internal method for getting a DataInputStream from an AppData object.
192 * @return data in object or null if no data is accessible
194 private DataInput getAppDataAsDataInputStream(AppData appdata, VamsasArchiveReader docreader) {
195 if (appdata!=null && docreader!=null) {
196 String entryRef = appdata.getDataReference();
197 if (entryRef!=null) {
198 log.debug("Resolving AppData reference for "+entryRef);
199 InputStream jstrm = docreader.getAppdataStream(entryRef);
201 return new AppDataInputStream(jstrm);
203 log.debug("Returning null input stream for unresolved reference ("+entryRef+") id="+appdata.getVorbaId());
207 // return a byteArray input stream
208 byte[] data=appdata.getData();
210 ByteArrayInputStream stream = new ByteArrayInputStream(data);
211 return new DataInputStream(stream);
213 log.debug("Returning null input stream for empty Appdata data block in id="+appdata.getVorbaId());
218 log.debug("Returning null DataInputStream for appdata entry:"+appdata.getVorbaId());
224 * internal method for getting ByteArray from AppData object
225 * @param clientOrUser - true for returning userData, otherwise return Client AppData.
226 * @return null or byte array
228 private byte[] _getappdataByteArray(boolean clientOrUser) {
230 throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
234 appdName = "Client's Appdata";
236 appdName = "User's Appdata";
238 log.debug("getting "+appdName+" as a byte array");
239 extractAppData(clientdoc.getVamsasDocument());
247 log.debug("Trying to resolve "+appdName+" object to byte array.");
248 data = getAppDataAsByteArray(object, clientdoc.getVamsasArchiveReader());
251 log.debug("Returning null for "+appdName+"ClientAppdata byte[] array");
257 * common method for Client and User AppData->InputStream accessor
258 * @param clientOrUser - the appData to resolve - false for client, true for user appdata.
259 * @return null or the DataInputStream desired.
261 private DataInput _getappdataInputStream(boolean clientOrUser) {
263 throw new Error("Implementation error, Improperly initialized SimpleClientAppdata.");
266 appdName = "Client's Appdata";
268 appdName = "User's Appdata";
270 if (log.isDebugEnabled())
271 log.debug("getting "+appdName+" as an input stream.");
272 extractAppData(clientdoc.getVamsasDocument());
280 log.debug("Trying to resolve ClientAppdata object to an input stream.");
281 return getAppDataAsDataInputStream(object, clientdoc.getVamsasArchiveReader());
283 log.debug("getClientInputStream returning null.");
287 * @see uk.ac.vamsas.client.IClientAppdata#getClientAppdata()
289 public byte[] getClientAppdata() {
290 return _getappdataByteArray(false);
293 * @see uk.ac.vamsas.client.IClientAppdata#getClientInputStream()
295 public DataInput getClientInputStream() {
296 return _getappdataInputStream(false);
300 * @see uk.ac.vamsas.client.IClientAppdata#getUserAppdata()
302 public byte[] getUserAppdata() {
303 return _getappdataByteArray(true);
307 * @see uk.ac.vamsas.client.IClientAppdata#getUserInputStream()
309 public DataInput getUserInputStream() {
310 return _getappdataInputStream(true);
313 * methods for writing new AppData entries.
315 private DataOutput _getAppdataOutputStream(boolean clientOrUser) {
317 SessionFile apdfile=null;
319 apdname = "clientAppData";
320 apdfile = newAppData;
322 apdname = "userAppData";
323 apdfile = newUserData;
327 apdfile=clientdoc.sclient._session.getTempSessionFile(apdname,".jar");
328 log.debug("Successfully made temp appData file for "+apdname);
330 // truncate to remove existing data.
331 apdfile.fileLock.getRaFile().setLength(0);
332 log.debug("Successfully truncated existing temp appData for "+apdname);
334 } catch (Exception e) {
335 log.error("Whilst opening temp file in directory "+clientdoc.sclient._session.sessionDir, e);
337 // we do not make another file for the new entry if one exists already
339 newAppData = apdfile;
341 newUserData = apdfile;
345 // LATER: Refactor these local AppDatastream IO stuff to their own class.
346 JarOutputStream dstrm =
347 new JarOutputStream(apdfile.fileLock.getBufferedOutputStream(true));
349 newAppDataStream = dstrm;
351 newUserDataStream = dstrm;
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);
357 catch (Exception e) {
358 log.error("Whilst opening jar output stream for file "+apdfile.sessionFile);
360 // tidy up and return null
361 apdfile.unlockFile();
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
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.");
377 log.fatal("FATAL! NO DOCUMENT TO WRITE TO!");
378 throw new IOException("FATAL! NO DOCUMENT TO WRITE TO!");
380 log.debug("Recovering AppData entry from "+apdjar.sessionFile);
381 JarInputStream istrm = new JarInputStream(apdjar.getBufferedInputStream(true));
383 while (istrm.available()>0 && (je=istrm.getNextJarEntry())!=null && !je.getName().equals("appData_entry.dat")) {
385 log.debug("Ignoring extraneous entry "+je.getName());
387 if (istrm.available()>0 && je!=null) {
388 log.debug("Found appData_entry.dat in Jar");
389 String ref = appd.getDataReference();
391 throw new IOException("Null AppData.DataReference passed.");
393 if (vdoc.writeAppdataFromStream(ref, istrm)) {
394 log.debug("Entry updated successfully.");
396 throw new IOException("writeAppdataFromStream did not return true - expect future badness."); // LATER - verify why this might occur.
399 throw new IOException("Couldn't find appData_entry.dat in temporary jar file "+apdjar.sessionFile.getAbsolutePath());
404 * @see uk.ac.vamsas.client.IClientAppdata#getClientOutputStream()
406 public DataOutput getClientOutputStream() {
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);
415 * @see uk.ac.vamsas.client.IClientAppdata#getUserOutputStream()
417 public DataOutput getUserOutputStream() {
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);
427 * @see uk.ac.vamsas.client.IClientAppdata#hasClientAppdata()
429 public boolean hasClientAppdata() {
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))
440 * @see uk.ac.vamsas.client.IClientAppdata#hasUserAppdata()
442 public boolean hasUserAppdata() {
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))
451 private boolean _writeAppDataStream(JarOutputStream ostrm, byte[] data) {
453 if (data!=null && data.length>0)
458 catch (Exception e) {
459 log.error("Serious! - IO error when writing AppDataStream to file "+newAppData.sessionFile, e);
464 * @see uk.ac.vamsas.client.IClientAppdata#setClientAppdata(byte[])
466 public void setClientAppdata(byte[] data) {
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);
474 _writeAppDataStream(newAppDataStream, data);
475 // LATER: deal with error case - do we make session read only, or what ?
480 * @see uk.ac.vamsas.client.IClientAppdata#setUserAppdata(byte[])
482 public void setUserAppdata(byte[] data) {
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);
490 _writeAppDataStream(newUserDataStream, data);
491 // LATER: deal with error case - do we make session read only, or what ?
495 * flush and close outstanding output streams.
496 * - do this before checking data length.
497 * @throws IOException
499 protected void closeForWriting() throws IOException {
500 if (newAppDataStream!=null) {
501 newAppDataStream.flush();
502 newAppDataStream.closeEntry();
503 newAppDataStream.close();
505 if (newUserDataStream!=null) {
506 newUserDataStream.flush();
507 newUserDataStream.closeEntry();
508 newUserDataStream.close();
515 * @return true if any AppData blocks have to be updated in session Jar
517 protected boolean isModified() {
518 // LATER differentiate between core xml modification and Jar Entry modification.
519 if (newAppData.sessionFile.exists() || newUserData.sessionFile.exists())
524 * @see java.lang.Object#finalize()
526 protected void finalize() throws Throwable {
527 if (newAppDataStream!=null) {
528 newAppDataStream = null;
530 if (newAppDataStream!=null) {
531 newUserDataStream = null;
533 if (newAppData!=null) {
534 newAppData.eraseExistence();
537 if (newUserData!=null) {
538 newUserData.eraseExistence();