/**
* name of backup of existing archive that has been updated/overwritten.
- * onlu one backup will be made - and this is it.
+ * only one backup will be made - and this is it.
*/
File originalBackup = null;
private final int _TRANSFER_BUFFER=4096*4;
protected SimpleDocument vorba = null;
/**
+ * LATER: ? CUT'n'Paste error ?
* Access and return current vamsas Document, if it exists, or create a new one
* (without affecting VamsasArchive object state - so is NOT THREAD SAFE)
- * TODO: possibly modify internal state to lock low-level files
+ * _TODO: possibly modify internal state to lock low-level files
* (like the IClientDocument interface instance constructer would do)
* @see org.vamsas.simpleclient.VamsasArchive.getOriginalVamsasDocument for additional caveats
*
* @throws IOException
* @throws org.exolab.castor.xml.MarshalException
* @throws org.exolab.castor.xml.ValidationException
+ * ????? where does this live JBPNote ?
*/
private VamsasDocument _doc=null;
* opens the new archive ready for writing. If the new archive is replacing an existing one,
* then the existing archive will be locked, and the new archive written to a temporary file.
* The new archive will be put in place once close() is called.
- * @param doclock TODO
+ * @param doclock LATER - pass existing lock on document, if it exists.... no need yet?
* @throws IOException
*/
private void openArchive() throws IOException {
public boolean transferAppDataEntry(String AppDataReference) throws IOException {
return transferAppDataEntry(AppDataReference, AppDataReference);
}
-
+ /**
+ * Validates the AppDataReference: not null and not already written to archive.
+ * @param AppDataReference
+ * @return true if valid. false if not
+ * @throws IOException for really broken references!
+ */
+ protected boolean _validNewAppDataReference(String newAppDataReference) throws IOException {
+ // LATER: Specify valid AppDataReference form in all VamsasArchive handlers
+ if (newAppDataReference==null)
+ throw new IOException("null newAppDataReference!");
+ if (entries.containsKey(newAppDataReference)) {
+ log.warn("Attempt to write '"+newAppDataReference+"' twice! - IGNORED");
+ // LATER: fix me? warning message should raise an exception here.
+ return false;
+ }
+ return true;
+ }
/**
* Transfers an AppDataReference from old to new vamsas archive, with a name change.
* @see transferAppDataEntry(String AppDataReference)
* @throws IOException
*/
public boolean transferAppDataEntry(String AppDataReference, String NewAppDataReference) throws IOException {
- // TODO: Specify valid AppDataReference form in all VamsasArchive handlers
- if (AppDataReference==null)
- throw new IOException("null AppDataReference!");
if (original==null || !original.exists()) {
log.warn("No backup archive exists.");
return false;
}
- if (entries.containsKey(NewAppDataReference)) {
- log.warn("Attempt to write '"+NewAppDataReference+"' twice! - IGNORED");
- return true;
- }
+ if (AppDataReference==null)
+ throw new IOException("null AppDataReference!");
+
+ if (!_validNewAppDataReference(NewAppDataReference))
+ return false;
accessOriginal();
+AppDataReference+"' as '"+NewAppDataReference+"' ("+count+" bytes)");
return true;
}
-
+ /**
+ * write data from a stream into an appData reference.
+ * @param AppDataReference - New AppDataReference not already written to archive
+ * @param adstream Source of data for appData reference - read until .read(buffer) returns -1
+ * @return true on success.
+ * @throws IOException for file IO or invalid AppDataReference string
+ */
+ public boolean writeAppdataFromStream(String AppDataReference, java.io.InputStream adstream) throws IOException {
+ if (!_validNewAppDataReference(AppDataReference)) {
+ log.warn("Invalid AppDataReference passed to writeAppdataFromStream");
+ throw new IOException("Invalid AppDataReference! (null, or maybe non-unique)!");
+ }
+
+ if (AppDataReference==null) {
+ log.warn("null appdata passed.");
+ throw new IOException("Null AppDataReference");
+ }
+
+ java.io.OutputStream adout = getAppDataStream(AppDataReference);
+ // copy over the bytes
+ int written=-1;
+ long count=0;
+ byte[] buffer = new byte[_TRANSFER_BUFFER]; // conservative estimate of a sensible buffer
+ do {
+ if ((written = adstream.read(buffer))>-1) {
+ adout.write(buffer, 0, written);
+ log.debug("Transferring "+written+".");
+ count+=written;
+ }
+ } while (written>-1);
+ return true;
+ }
/**
* transfers any AppDataReferences existing in the old document
* that haven't already been transferred to the new one
- * TODO: LATER: do the same for transfers requiring a namechange - more document dependent.
+ * LATER: do the same for transfers requiring a namechange - more document dependent.
* @return true if data was transferred.
*/
public boolean transferRemainingAppDatas() throws IOException {
odoclock.updateFrom(null, rchive);
}
catch (IOException e) {
- // TODO: LATER: decide if leaving nastily named backup files around is necessary.
+ // 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);
}