1 package org.vamsas.client.simpleclient;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.DataOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.io.OutputStream;
11 import java.io.OutputStreamWriter;
12 import java.io.PrintWriter;
13 import java.util.Hashtable;
14 import java.util.Iterator;
15 import java.util.Vector;
16 import java.util.jar.JarEntry;
17 import java.util.jar.JarOutputStream;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.vamsas.client.ClientHandle;
22 import org.vamsas.client.IVorbaIdFactory;
23 import org.vamsas.client.SessionHandle;
24 import org.vamsas.client.UserHandle;
25 import org.vamsas.client.VorbaIdFactory;
26 import org.vamsas.client.VorbaXmlBinder;
27 import org.vamsas.client.object;
28 import org.vamsas.objects.core.ApplicationData;
29 import org.vamsas.objects.core.VAMSAS;
30 import org.vamsas.objects.core.VamsasDocument;
31 import org.vamsas.objects.utils.AppDataReference;
32 import org.vamsas.objects.utils.DocumentStuff;
33 import org.vamsas.objects.utils.ProvenanceStuff;
34 import org.vamsas.objects.utils.document.VersionEntries;
37 * Class for high-level io and Jar manipulation involved in creating
38 * or updating a vamsas archive (with backups).
39 * Writes to a temporary file and then swaps new file for backup.
40 * uses the sessionFile locking mechanism for safe I/O
44 public class VamsasArchive {
45 private static Log log = LogFactory.getLog(VamsasArchive.class);
47 * destination of new archive data (tempfile if virginarchive=true, original archive location otherwise)
49 java.io.File archive=null;
51 * locked IO handler for new archive file
53 SessionFile rchive=null;
55 * original archive file to be updated (or null if virgin) where new data will finally reside
57 java.io.File original=null;
59 * original archive IO handler
61 SessionFile odoclock = null;
63 * Original archive reader class
65 VamsasArchiveReader odoc = null;
67 * true if a real vamsas document is being written.
69 boolean vamsasdocument=true;
71 * Output stream for archived data
73 JarOutputStream newarchive=null;
75 * JarEntries written to archive
77 Hashtable entries = null;
79 * true if we aren't just updating an archive
81 private boolean virginArchive=false;
83 * Create a new vamsas archive
84 * File locks are made immediately to avoid contention
86 * @param archive - file spec for new vamsas archive
87 * @param vamsasdocument true if archive is to be a fully fledged vamsas document archive
88 * @throws IOException if call to accessOriginal failed for updates, or openArchive failed.
90 public VamsasArchive(File archive, boolean vamsasdocument) throws IOException {
91 this(archive, vamsasdocument, null);
93 public VamsasArchive(File archive, boolean vamsasdocument, Lock extantLock) throws IOException {
96 if (archive==null || (archive!=null && !(archive.getParentFile().canWrite() && (!archive.exists() || archive.canWrite())))) {
97 log.fatal("Expect Badness! -- Invalid parameters for VamsasArchive constructor:"+((archive!=null)
98 ? "File cannot be overwritten." : "Null Object not valid constructor parameter"));
101 this.vamsasdocument = vamsasdocument;
102 if (archive.exists()) {
103 this.original = archive;
104 this.odoclock = new SessionFile(archive); // lock the file *immediatly*
105 odoclock.lockFile(extantLock);
106 this.archive = null; // archive will be a temp file when the open method is called
109 this.accessOriginal();
110 } catch (IOException e) {
111 throw new IOException("Lock failed for existing archive"+archive);
114 this.original = null;
115 this.archive = archive; // archive is written in place.
117 archive.createNewFile();
118 rchive=new SessionFile(archive); // lock the file *immediatly*
119 rchive.lockFile(extantLock);
121 virginArchive = true;
123 this.openArchive(); // open archive
126 * name of backup of existing archive that has been updated/overwritten.
127 * onlu one backup will be made - and this is it.
129 File originalBackup = null;
131 private void makeBackup() {
132 if (!virginArchive) {
133 if (originalBackup==null && original!=null && original.exists()) {
136 originalBackup = odoclock.backupSessionFile(null, original.getName(), ".bak", original.getParentFile());
138 catch (IOException e) {
139 log.warn("Problem whilst making a backup of original archive.",e);
146 * called after archive is written to put file in its final place
148 private void updateOriginal() {
149 if (!virginArchive) {
150 // make sure original document really is backed up and then overwrite it.
152 // try to shut the odoc reader.
156 // Make a backup if it isn't done already
159 // copy new Archive data that was writen to a temporary file
160 odoclock.updateFrom(null, rchive);
162 catch (IOException e) {
163 // TODO: LATER: decide if leaving nastily named backup files around is necessary.
164 log.error("Problem updating archive from temporary file! - backup left in '"
165 +backupFile().getAbsolutePath()+"'",e);
167 // Tidy up if necessary.
173 boolean donotdeletebackup=false;
175 * called by app to get name of backup if it was made.
176 * If this is called, the caller app *must* delete the backup themselves.
177 * @return null or a valid file object
179 public File backupFile() {
181 if (!virginArchive) {
183 donotdeletebackup=false; // external reference has been made.
184 return ((original!=null) ? originalBackup : null);
190 * @return JarEntry name for the vamsas XML stream in this archive
192 protected String getDocumentJarEntry() {
194 return VamsasArchiveReader.VAMSASDOC;
195 return VamsasArchiveReader.VAMSASXML;
199 * @return true if Vamsas Document has been written to archive
201 protected boolean isDocumentWritten() {
202 if (newarchive==null)
203 log.warn("isDocumentWritten() called for unopened archive.");
205 if (entries.containsKey(getDocumentJarEntry()))
211 * Add unique entry strings to internal JarEntries list.
213 * @return true if entry was unique and was added.
215 private boolean addEntry(String entry) {
217 entries=new Hashtable();
218 if (entries.containsKey(entry))
220 entries.put(entry, new Integer(entries.size()));
224 * adds named entry to newarchive or returns false.
226 * @return true if entry was unique and could be added
227 * @throws IOException if entry name was invalid or a new entry could not be made on newarchive
229 private boolean addValidEntry(String entry) throws IOException {
230 JarEntry je = new JarEntry(entry);
231 if (!addEntry(entry))
233 newarchive.putNextEntry(je);
238 * opens the new archive ready for writing. If the new archive is replacing an existing one,
239 * then the existing archive will be locked, and the new archive written to a temporary file.
240 * The new archive will be put in place once close() is called.
241 * @param doclock TODO
242 * @throws IOException
244 private void openArchive() throws IOException {
246 if (newarchive!=null) {
247 log.warn("openArchive() called multiple times.");
248 throw new IOException("Vamsas Archive '"+archive.getAbsolutePath()+"' is already open.");
250 if (archive==null && (virginArchive || original==null)) {
251 log.warn("openArchive called on uninitialised VamsasArchive object.");
252 throw new IOException("Badly initialised VamsasArchive object - no archive file specified.");
254 if (!virginArchive) {
257 // make a temporary file to write to
258 archive = File.createTempFile(original.getName(), ".new",original.getParentFile());
260 if (archive.exists())
261 log.warn("New archive file name already in use! Possible lock failure imminent?");
265 rchive = new SessionFile(archive);
266 if (!rchive.lockFile())
267 throw new IOException("Failed to get lock on file "+archive);
268 newarchive = new JarOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(archive)));
269 entries = new Hashtable();
272 * Safely initializes the VAMSAS XML document Jar Entry.
273 * @return Writer to pass to the marshalling function.
274 * @throws IOException if a document entry has already been written.
276 public PrintWriter getDocumentOutputStream() throws IOException {
277 if (newarchive==null)
279 if (!isDocumentWritten()) {
281 if (addValidEntry(getDocumentJarEntry()))
282 return new PrintWriter(new java.io.OutputStreamWriter(newarchive, "UTF-8"));
283 } catch (Exception e) {
284 log.warn("Problems opening XML document JarEntry stream",e);
287 throw new IOException("Vamsas Document output stream is already written.");
292 * Opens and returns the applicationData output stream for the appdataReference string.
293 * @param appdataReference
294 * @return Output stream to write to
295 * @throws IOException
297 public AppDataOutputStream getAppDataStream(String appdataReference) throws IOException {
298 if (newarchive==null)
299 throw new IOException("Attempt to write to closed VamsasArchive object.");
300 if (addValidEntry(appdataReference)) {
301 return new AppDataOutputStream(newarchive);
307 * Stops any current write to archive, and reverts to the backup if it exists.
308 * All existing locks on the original will be released. All backup files are removed.
310 public boolean cancelArchive() {
311 if (newarchive!=null) {
315 } catch (Exception e) {
316 log.debug("Whilst closing newarchive",e);
318 if (!virginArchive) {
319 // then there is something to recover.
323 catch (Exception e) {
324 log.warn("Problems when trying to cancel Archive "+archive.getAbsolutePath(), e);
330 log.warn("Client Error: cancelArchive called before archive("+original.getAbsolutePath()+") has been opened!");
332 closeAndReset(); // tidy up and release locks.
337 * recovers the original file's contents from the (temporary) backup.
338 * @throws Exception if any SessionFile or file removal operations fail.
340 private void recoverBackup() throws Exception {
341 if (originalBackup!=null) {
342 // backup has been made.
343 // revert from backup and delete it (changing backup filename)
345 rchive = new SessionFile(original);
347 SessionFile bckup = new SessionFile(originalBackup);
349 rchive.updateFrom(null, bckup); // recover from backup file.
356 * forget about any backup that was made - removing it first if it was only temporary.
358 private void removeBackup() {
359 if (originalBackup!=null) {
360 log.debug("Removing backup in "+originalBackup.getAbsolutePath());
361 if (!donotdeletebackup)
362 if (!originalBackup.delete())
363 log.info("VamsasArchive couldn't remove temporary backup "+originalBackup.getAbsolutePath());
368 * only do this if you want to destroy the current file output stream
371 private void closeAndReset() {
376 if (original!=null) {
383 if (odoclock!=null) {
384 odoclock.unlockFile();
394 private final int _TRANSFER_BUFFER=4096*4;
396 * open original archive file for exclusive (locked) reading.
397 * @throws IOException
399 private void accessOriginal() throws IOException {
400 if (original!=null && original.exists()) {
402 odoclock = new SessionFile(original);
405 odoc = new VamsasArchiveReader(original);
410 * Convenience method to copy over the referred entry from the backup to the new version.
411 * Warning messages are raised if no backup exists or the
412 * entry doesn't exist in the backed-up original.
413 * Duplicate writes return true - but a warning message will also be raised.
414 * @param AppDataReference
415 * @return true if AppDataReference now exists in the new document
416 * @throws IOException
418 public boolean transferAppDataEntry(String AppDataReference) throws IOException {
419 return transferAppDataEntry(AppDataReference, AppDataReference);
422 * Transfers an AppDataReference from old to new vamsas archive, with a name change.
423 * @see transferAppDataEntry(String AppDataReference)
424 * @param AppDataReference
425 * @param NewAppDataReference - AppDataReference in new Archive
427 * @throws IOException
429 public boolean transferAppDataEntry(String AppDataReference, String NewAppDataReference) throws IOException {
430 // TODO: Specify valid AppDataReference form in all VamsasArchive handlers
431 if (AppDataReference==null)
432 throw new IOException("null AppDataReference!");
433 if (original==null || !original.exists()) {
434 log.warn("No backup archive exists.");
437 if (entries.containsKey(NewAppDataReference)) {
438 log.warn("Attempt to write '"+NewAppDataReference+"' twice! - IGNORED");
444 java.io.InputStream adstream = odoc.getAppdataStream(AppDataReference);
446 if (adstream==null) {
447 log.warn("AppDataReference '"+AppDataReference+"' doesn't exist in backup archive.");
451 java.io.OutputStream adout = getAppDataStream(NewAppDataReference);
452 // copy over the bytes
455 byte[] buffer = new byte[_TRANSFER_BUFFER]; // conservative estimate of a sensible buffer
457 if ((written = adstream.read(buffer))>-1) {
458 adout.write(buffer, 0, written);
459 log.debug("Transferring "+written+".");
462 } while (written>-1);
463 log.debug("Sucessfully transferred AppData for '"
464 +AppDataReference+"' as '"+NewAppDataReference+"' ("+count+" bytes)");
468 * transfers any AppDataReferences existing in the old document
469 * that haven't already been transferred to the new one
470 * TODO: LATER: do the same for transfers requiring a namechange - more document dependent.
471 * @return true if data was transferred.
473 public boolean transferRemainingAppDatas() throws IOException {
474 boolean transfered=false;
475 if (original==null || !original.exists()) {
476 log.warn("No backup archive exists.");
481 if (getVorba()!=null) {
482 Vector originalRefs=null;
484 originalRefs = vorba.getReferencedEntries(getVamsasDocument(), getOriginalArchiveReader());
485 } catch (Exception e) {
486 log.warn("Problems accessing original document entries!",e);
488 if (originalRefs!=null) {
489 Iterator ref = originalRefs.iterator();
490 while (ref.hasNext()) {
491 String oldentry = (String) ref.next();
492 if (oldentry!=null && !entries.containsKey(oldentry)) {
493 log.debug("Transferring remaining entry '"+oldentry+"'");
494 transfered |= transferAppDataEntry(oldentry);
502 * Tidies up and closes archive, removing any backups that were created.
503 * NOTE: It is up to the caller to delete the original archive backup obtained from backupFile()
504 * TODO: ensure all extant AppDataReference jar entries are transferred to new Jar
505 * TODO: provide convenient mechanism for generating new unique AppDataReferences and adding them to the document
507 public void closeArchive() throws IOException {
508 if (newarchive!=null) {
509 newarchive.closeEntry();
510 if (!isDocumentWritten())
511 log.warn("Premature closure of archive '"+archive.getAbsolutePath()+"': No document has been written.");
516 log.warn("Attempt to close archive that has not been opened for writing.");
520 * Access original archive if it exists, pass the reader to the client
521 * Note: this is NOT thread safe and a call to closeArchive() will by necessity
522 * close and invalidate the VamsasArchiveReader object.
523 * @return null if no original archive exists.
525 public VamsasArchiveReader getOriginalArchiveReader() throws IOException {
526 if (!virginArchive) {
533 * returns original document's root vamsas elements.
535 * @throws IOException
536 * @throws org.exolab.castor.xml.MarshalException
537 * @throws org.exolab.castor.xml.ValidationException
539 public object[] getOriginalRoots() throws IOException,
540 org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
541 return VamsasArchive.getOriginalRoots(this);
544 * Access original document if it exists, and get VAMSAS root objects.
545 * @return vector of vamsas roots from original document
546 * @throws IOException
548 public static object[] getOriginalRoots(VamsasArchive ths) throws IOException,
549 org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
550 VamsasArchiveReader oReader = ths.getOriginalArchiveReader();
553 if (oReader.isValid()) {
554 InputStreamReader vdoc = new InputStreamReader(oReader.getVamsasDocumentStream());
555 VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
557 return doc.getVAMSAS();
558 // TODO ensure embedded appDatas are garbage collected to save memory
560 InputStream vxmlis = oReader.getVamsasXmlStream();
561 if (vxmlis!=null) { // Might be an old vamsas file.
562 BufferedInputStream ixml = new BufferedInputStream(oReader.getVamsasXmlStream());
563 InputStreamReader vxml = new InputStreamReader(ixml);
564 VAMSAS root[] = new VAMSAS[1];
565 root[0] = VAMSAS.unmarshal(vxml);
573 protected SimpleDocument vorba = null;
576 * @return Returns the current VorbaIdFactory for the archive.
578 public VorbaIdFactory getVorba() {
580 vorba = new SimpleDocument("simpleclient.VamsasArchive");
581 return vorba.getVorba();
585 * @param vorba the VorbaIdFactory to use for accessing vamsas objects.
587 public void setVorba(VorbaIdFactory Vorba) {
590 vorba = new SimpleDocument(Vorba);
592 vorba.setVorba(Vorba);
598 * Access and return current vamsas Document, if it exists, or create a new one
599 * (without affecting VamsasArchive object state - so is NOT THREAD SAFE)
600 * TODO: possibly modify internal state to lock low-level files
601 * (like the IClientDocument interface instance constructer would do)
602 * @see org.vamsas.simpleclient.VamsasArchive.getOriginalVamsasDocument for additional caveats
605 * @throws IOException
606 * @throws org.exolab.castor.xml.MarshalException
607 * @throws org.exolab.castor.xml.ValidationException
609 private VamsasDocument _doc=null;
610 public VamsasDocument getVamsasDocument() throws IOException,
611 org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
614 _doc = getOriginalVamsasDocument(this, getVorba());
617 // Create a new document and return it
618 _doc = DocumentStuff.newVamsasDocument(new VAMSAS[] { new VAMSAS()},
619 ProvenanceStuff.newProvenance("org.vamsas.simpleclient.VamsasArchive", "Created new empty document")
620 , VersionEntries.latestVersion());
624 * Access the original vamsas document for a VamsasArchive class, and return it.
625 * Users of the VamsasArchive class should use the getVamsasDocument method to retrieve
626 * the current document - only use this one if you want the 'backup' version.
627 * TODO: catch OutOfMemoryError - they are likely to occur here.
628 * NOTE: vamsas.xml datastreams are constructed as 'ALPHA_VERSION' vamsas documents.
630 * @return null if no document exists.
631 * @throws IOException
632 * @throws org.exolab.castor.xml.MarshalException
633 * @throws org.exolab.castor.xml.ValidationException
635 public static VamsasDocument getOriginalVamsasDocument(VamsasArchive ths) throws IOException,
636 org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
637 return VamsasArchive.getOriginalVamsasDocument(ths, null);
641 * Uses VorbaXmlBinder to retrieve the VamsasDocument from the original archive referred to by ths
645 * @throws IOException
646 * @throws org.exolab.castor.xml.MarshalException
647 * @throws org.exolab.castor.xml.ValidationException
649 public static VamsasDocument getOriginalVamsasDocument(VamsasArchive ths, VorbaIdFactory vorba) throws IOException,
650 org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
651 VamsasArchiveReader oReader = ths.getOriginalArchiveReader();
654 return ths.vorba.getVamsasDocument(oReader);
656 // otherwise - there was no valid original document to read.
660 public void putVamsasDocument(VamsasDocument doc) throws IOException,
661 org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
662 putVamsasDocument(doc, getVorba());
664 public void putVamsasDocument(VamsasDocument doc, VorbaIdFactory vorba) throws IOException,
665 org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
666 VorbaXmlBinder.putVamsasDocument(getDocumentOutputStream(), vorba, doc);