*/
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;
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 {
/**
* 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;
vorbaId = thisid;
__vorba = factory;
__stored_in_document = stored;
+ return (__old_hash==0) || (__old_hash == __last_hash);
}
/**
* @return
*/
protected boolean merge(object laterCopy) {
+ log.warn(this.getClass().getName()+".merge() not implemented.");
return true;
}
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 {
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();
//}
} else {
rf = (object) fval;
- log.info("Got value for "+fields[i].getFieldName());
+ log.debug("Got value for "+fields[i].getFieldName());
}
}
} else {
*/
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()) {
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;
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
*
*/
odoclock = null;
}
}
+ removeBackup();
newarchive=null;
original=null;
entries=null;
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)
if (unmarsh==null)
log.fatal("Couldn't unmarshall document!");
-
+
VAMSAS root[]= new VAMSAS[] { null};
root[0] = (VAMSAS) unmarsh[0];
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;
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");
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)
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
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();
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);