From: jprocter Date: Tue, 17 Jan 2006 12:17:30 +0000 (+0000) Subject: debugged VamsasArchive and rationalised debugging messages. X-Git-Tag: Release_0.2~379 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=21443c783bddd1ab435889e7f160501b66406f3d;p=vamsas.git debugged VamsasArchive and rationalised debugging messages. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@141 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/org/vamsas/client/object.java b/src/org/vamsas/client/object.java index b570101..ea51068 100644 --- a/src/org/vamsas/client/object.java +++ b/src/org/vamsas/client/object.java @@ -31,7 +31,7 @@ public abstract class object { */ protected boolean __stored_in_document = false; - protected long __last_hash = 0; + protected long __last_hash = 0; // default is different so doHash always returns true initially. protected boolean registerable = false; @@ -76,23 +76,20 @@ public abstract class object { try { Method fd = this.getClass().getMethod("setId", new Class[] { String.class }); fd.invoke((Object) this, new Object[] {new String(this.getVorbaId().id)}); - log.info(this.getClass().getName()+" called setInstanceIdField!"); + log.debug(this.getClass().getName()+" called setInstanceIdField!"); } catch (InvocationTargetException e) { - System.err - .println("SourceGeneration of " + log.error("SourceGeneration of " + this.getClass().toString() - + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object."); - e.printStackTrace(System.err); + + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object.", e); } catch (IllegalAccessException e) { - System.err - .println("SourceGeneration of " + log.error("SourceGeneration of " + this.getClass().toString() - + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object."); - e.printStackTrace(System.err); + + "\n has resulted in an inaccessible 'setId' method!\nCannot set ID from the vorbaId object.", e); } catch (SecurityException e) { - e.printStackTrace(System.err); + log.error("Security access violation for "+this.getClass().toString(),e); } catch (NoSuchMethodException e) { + log.warn(this.getClass().toString()+" was erroneously marked as a Vorba object class (Implementation error?)"); this.setRegisterable(false); } } else { @@ -104,8 +101,10 @@ public abstract class object { /** * calculate a hash for the object with all housekeeping fields at standard * values. (isRegisterable is an immutable attribute property) + * @return true if new hash different to last hash */ - synchronized protected void doHash() { + synchronized protected boolean doHash() { + long __old_hash = __last_hash; __last_hash = 0; VorbaId thisid = vorbaId; IVorbaIdFactory factory = __vorba; @@ -116,6 +115,7 @@ public abstract class object { vorbaId = thisid; __vorba = factory; __stored_in_document = stored; + return (__old_hash==0) || (__old_hash == __last_hash); } /** @@ -128,6 +128,7 @@ public abstract class object { * @return */ protected boolean merge(object laterCopy) { + log.warn(this.getClass().getName()+".merge() not implemented."); return true; } @@ -223,9 +224,11 @@ public abstract class object { protected void __ensure_instance_ids() { if (__vorba==null) throw new Error("Improperly intialised org.vamsas.client.object - no VorbaFactory given."); - log.info(this.getClass()+".__ensure_instance_ids()"); + log.debug("doing "+this.getClass()+".__ensure_instance_ids()"); if (!__stored_in_document && registerable) setInstanceIdField(); + if (!doHash()) + return; // nothing has changed in this object - probably visited it before. Class descriptor = null; XMLClassDescriptorImpl descimpl = null; try { @@ -233,7 +236,9 @@ public abstract class object { descriptor = this.getClass().getClassLoader().loadClass(this.getClass().getName()+"Descriptor"); descimpl = (XMLClassDescriptorImpl) descriptor.getConstructor(null).newInstance(null); } catch (Exception e) { - log.error("Couldn't resolve descriptor for "+this.getClass().getName()); + log.fatal("Source Generation Error!: Couldn't resolve descriptor for " + +this.getClass().getName() + +" was 'generate descriptors' set for castorbuilder.properties?"); return; } FieldDescriptor fields[] = descimpl.getFields(); @@ -280,7 +285,7 @@ public abstract class object { //} } else { rf = (object) fval; - log.info("Got value for "+fields[i].getFieldName()); + log.debug("Got value for "+fields[i].getFieldName()); } } } else { diff --git a/src/org/vamsas/client/simpleclient/VamsasArchive.java b/src/org/vamsas/client/simpleclient/VamsasArchive.java index 39681ad..1924f02 100644 --- a/src/org/vamsas/client/simpleclient/VamsasArchive.java +++ b/src/org/vamsas/client/simpleclient/VamsasArchive.java @@ -86,9 +86,10 @@ public class VamsasArchive { */ public VamsasArchive(File archive, boolean vamsasdocument) throws IOException { super(); - if (archive==null || (archive!=null && !archive.canWrite())) { - log.fatal("Invalid parameters for VamsasArchive constructor:"+((archive!=null) + if (archive==null || (archive!=null && !(archive.getParentFile().canWrite() && (!archive.exists() || archive.canWrite())))) { + log.fatal("Expect Badness! -- Invalid parameters for VamsasArchive constructor:"+((archive!=null) ? "File cannot be overwritten." : "Null Object not valid constructor parameter")); + return; } this.vamsasdocument = vamsasdocument; if (archive.exists()) { @@ -140,28 +141,33 @@ public class VamsasArchive { odoc = null; } // Make a backup if it isn't done already - if (originalBackup==null) - makeBackup(); + makeBackup(); try { // copy new Archive data that was writen to a temporary file odoclock.updateFrom(null, rchive); } catch (IOException e) { - log.error("Problem updating archive from temporary file! - backup in '" + // TODO: LATER: decide if leaving nastily named backup files around is necessary. + log.error("Problem updating archive from temporary file! - backup left in '" +backupFile().getAbsolutePath()+"'",e); } + // Tidy up if necessary. + removeBackup(); } else { - // don't need to do anything. + } } + boolean donotdeletebackup=false; /** * called by app to get name of backup if it was made. + * If this is called, the caller app *must* delete the backup themselves. * @return null or a valid file object */ public File backupFile() { if (!virginArchive) { makeBackup(); + donotdeletebackup=false; // external reference has been made. return ((original!=null) ? originalBackup : null); } return null; @@ -290,38 +296,59 @@ public class VamsasArchive { try { newarchive.close(); - } catch (Exception e) {}; + } catch (Exception e) { + log.debug("Whilst closing newarchive",e); + }; if (!virginArchive) { // then there is something to recover. - if (originalBackup!=null) { - // backup has been made. - // revert from backup and delete it (changing backup filename) - if (rchive==null) { - rchive = new SessionFile(original); - } - SessionFile bckup = new SessionFile(originalBackup); - - try { - rchive.updateFrom(null, bckup); // recover from backup file. - bckup.unlockFile(); - bckup=null; - originalBackup.delete(); - originalBackup=null; - } - catch (Exception e) { - log.warn("Problems when trying to cancel Archive "+archive.getAbsolutePath(), e); - return false; - } + try { + recoverBackup(); + } + catch (Exception e) { + log.warn("Problems when trying to cancel Archive "+archive.getAbsolutePath(), e); + return false; } } + } else { - log.info("cancelArchive called before archive("+original.getAbsolutePath()+") has been opened!"); + log.warn("Client Error: cancelArchive called before archive("+original.getAbsolutePath()+") has been opened!"); } closeAndReset(); // tidy up and release locks. return true; } /** + * recovers the original file's contents from the (temporary) backup. + * @throws Exception if any SessionFile or file removal operations fail. + */ + private void recoverBackup() throws Exception { + if (originalBackup!=null) { + // backup has been made. + // revert from backup and delete it (changing backup filename) + if (rchive==null) { + rchive = new SessionFile(original); + } + SessionFile bckup = new SessionFile(originalBackup); + + rchive.updateFrom(null, bckup); // recover from backup file. + bckup.unlockFile(); + bckup=null; + removeBackup(); + } + } + /** + * forget about any backup that was made - removing it first if it was only temporary. + */ + private void removeBackup() { + if (originalBackup!=null) { + log.debug("Removing backup in "+originalBackup.getAbsolutePath()); + if (!donotdeletebackup) + if (!originalBackup.delete()) + log.info("VamsasArchive couldn't remove temporary backup "+originalBackup.getAbsolutePath()); + originalBackup=null; + } + } + /** * only do this if you want to destroy the current file output stream * */ @@ -342,6 +369,7 @@ public class VamsasArchive { odoclock = null; } } + removeBackup(); newarchive=null; original=null; entries=null; @@ -500,14 +528,14 @@ public class VamsasArchive { public VorbaIdFactory getVorba() { return vorba; } - + /** * @param vorba the VorbaIdFactory to use for accessing vamsas objects. */ public void setVorba(VorbaIdFactory vorba) { this.vorba = vorba; } - + /** * Access and return current vamsas Document, if it exists, or create a new one * (without affecting VamsasArchive object state - so is NOT THREAD SAFE) @@ -595,7 +623,7 @@ public class VamsasArchive { if (unmarsh==null) log.fatal("Couldn't unmarshall document!"); - + VAMSAS root[]= new VAMSAS[] { null}; root[0] = (VAMSAS) unmarsh[0]; diff --git a/src/org/vamsas/test/simpleclient/VamsasArchive.java b/src/org/vamsas/test/simpleclient/VamsasArchive.java index 91dc35e..c023a24 100644 --- a/src/org/vamsas/test/simpleclient/VamsasArchive.java +++ b/src/org/vamsas/test/simpleclient/VamsasArchive.java @@ -1,15 +1,11 @@ package org.vamsas.test.simpleclient; -import java.io.BufferedInputStream; import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.ObjectOutputStream; -import java.io.PrintWriter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.vamsas.client.simpleclient.*; +import org.vamsas.client.simpleclient.VamsasArchiveReader; import org.vamsas.objects.core.ApplicationData; import org.vamsas.objects.core.User; import org.vamsas.objects.core.VAMSAS; @@ -50,7 +46,9 @@ public class VamsasArchive { else log.info(av+" has been backed up as "+backup); if (backup!=null) { - log.info("Backup "+backup+" is being written to."); + File newf=new File(av.getAbsolutePath()+"_new.zip"); + log.info("Now writing new Archive "+newf.getAbsolutePath()); + org.vamsas.client.simpleclient.VamsasArchive va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true); // open another and... ApplicationData appdata = new ApplicationData(); appdata.setName("org.vamsas.test.simpleclient.VamsasArchive"); @@ -61,15 +59,10 @@ public class VamsasArchive { String appdata_ref = "vamsas:org.vamsas.test.simpleclient.VamsasArchive/"+apuser.getOrganization()+"/"+apuser.getFullname(); - File newf=new File(av.getAbsolutePath()+"_new.zip"); - org.vamsas.client.simpleclient.VamsasArchive va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true); - log.info("Getting the document from the backup-original."); - VamsasDocument vadoc; VamsasArchiveReader vread = va.getOriginalArchiveReader(); - vadoc = doc; // putting original data into new archive. - log.info("Got document. Adding stuff."); if (vread!=null) { + log.info("Reading (and avoiding references to) original data in "+newf); if (vread.getAppdataStream(appdata_ref)!=null) { int i=0; while (vread.getAppdataStream(appdata_ref+"/"+Integer.toString(++i))!=null) @@ -77,12 +70,13 @@ public class VamsasArchive { appdata_ref+="/"+Integer.toString(i); } } + + log.info("Adding new data stuff."); log.info("Writing an apdata reference using AppDataStream interface."); apuser.setDataReference(appdata_ref); appdata.addUser(apuser); appdata.setVersion("noggin"); appdata.setUrn("program:/the.nog/"); - // TODO: FIX schema - User.addUser is unsatisfiable! ObjectOutputStream apdos = new ObjectOutputStream(va.getAppDataStream(appdata_ref)); apdos.writeObject(appdata_ref); apdos.writeObject(apuser); // some random content @@ -91,7 +85,7 @@ public class VamsasArchive { log.info("Preparing to write new document."); doc.addApplicationData(appdata); doc.addVAMSAS(Core.getDemoVamsas()); - + // TODO: ensure all jar entries are transferred over (at least those that are referenced - make a flag for 'tidy zip manifest'. va.putVamsasDocument(doc); // gets stream and puts it. // TODO: verify that the vxml stream close method overridden ? va.closeArchive(); @@ -99,6 +93,8 @@ public class VamsasArchive { log.info("Dump of new vamsas document :"); va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true); // TODO - refactor Reader class to have deserializing helpers ArchiveReports.reportDocument(va.getVamsasDocument(), va.getOriginalArchiveReader(), true); + log.info("Cancelling write (which was necessary to dump new vamsas document!)"); + va.cancelArchive(); // backup.delete(); // tidy up } log.info("Now Cancelling write to original archive "+av);