From 9d184deaf6769baf57402335f95be259e7943311 Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 19 Jan 2006 18:17:04 +0000 Subject: [PATCH] started on ClientDocument implementation and Locked IO tests for VamsasArchive git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@149 be28352e-c001-0410-b1a7-c7978e42abec --- .../vamsas/client/simpleclient/ClientDocument.java | 42 +++++++------------- src/org/vamsas/client/simpleclient/IdFactory.java | 9 ++++- .../vamsas/client/simpleclient/VamsasArchive.java | 24 +++++++++-- .../client/simpleclient/VamsasArchiveReader.java | 19 +++++++++ src/org/vamsas/client/simpleclient/VamsasFile.java | 6 +++ 5 files changed, 67 insertions(+), 33 deletions(-) diff --git a/src/org/vamsas/client/simpleclient/ClientDocument.java b/src/org/vamsas/client/simpleclient/ClientDocument.java index 05fe494..3e5fcfa 100644 --- a/src/org/vamsas/client/simpleclient/ClientDocument.java +++ b/src/org/vamsas/client/simpleclient/ClientDocument.java @@ -16,16 +16,11 @@ import org.vamsas.objects.core.VamsasDocument; * @author jimp Contains a collection of vamsas objects and reference to a * specified ClientHandle's information. */ -public class ClientDocument implements IClientDocument { +public class ClientDocument extends org.vamsas.client.ClientDocument implements IClientDocument { protected IClient vorba; protected org.vamsas.objects.core.VAMSAS[] roots; protected byte[] appData; - - /** - * collection of org.vamsas.client.object references - */ - protected Hashtable vamsasObjects; - + protected VamsasArchive archive = null; /* * (non-Javadoc) * @@ -33,18 +28,12 @@ public class ClientDocument implements IClientDocument { */ /** - * - * @param data - * @param doc - * @param objects - * @param vorba */ - protected ClientDocument(byte[] data, VAMSAS[] roots, Hashtable objects, IClient vorba) { - // TODO Auto-generated constructor stub - appData = data; - this.roots =roots; - vamsasObjects = objects; + protected ClientDocument(VamsasArchive document, IdFactory Factory, IClient vorba) { + super(Factory.getVorbaIdHash(), Factory); this.vorba = vorba; + archive = document; + roots = null; } public byte[] getApplicationData() { @@ -148,12 +137,10 @@ public class ClientDocument implements IClientDocument { /* * (non-Javadoc) * - * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object) + * @see org.vamsas.client.IClientDocument#setApplicationData(byte[]) */ -public VorbaId registerObject(object unregistered) { - if (!unregistered.isRegistered()) - unregistered.setVorbaId(unregistered.__vorba.makeVorbaId()); - return unregistered.getVorbaId(); + public void setApplicationData(byte[] newData) { + appData = newData; } /* * (non-Javadoc) @@ -170,12 +157,11 @@ public VorbaId registerObject(object unregistered) { return null; } - /* - * (non-Javadoc) - * - * @see org.vamsas.client.IClientDocument#setApplicationData(byte[]) + /* (non-Javadoc) + * @see org.vamsas.client.IClientDocument#registerObject(org.vamsas.client.object) */ - public void setApplicationData(byte[] newData) { - appData = newData; + public VorbaId registerObject(object unregistered) { + // TODO: add provenance stuff to newly registered object + return _registerObject(unregistered); } } diff --git a/src/org/vamsas/client/simpleclient/IdFactory.java b/src/org/vamsas/client/simpleclient/IdFactory.java index 36fd3bc..543730e 100644 --- a/src/org/vamsas/client/simpleclient/IdFactory.java +++ b/src/org/vamsas/client/simpleclient/IdFactory.java @@ -51,7 +51,14 @@ public class IdFactory extends VorbaIdFactory { idstring = client.getClientUrn()+":"+unique.getValue()+"/"; extantids=new Hashtable(); } - + /** + * values for keys in this hash can be used to reference the org.vamsas.client.object instance for the VorbaId string. + * @return the hash of all VorbaIds + */ + protected Hashtable getVorbaIdHash() { + return extantids; + } + /* (non-Javadoc) * @see org.vamsas.client.VorbaIdFactory#makeVorbaId() */ diff --git a/src/org/vamsas/client/simpleclient/VamsasArchive.java b/src/org/vamsas/client/simpleclient/VamsasArchive.java index fb7ddd8..f55d3d8 100644 --- a/src/org/vamsas/client/simpleclient/VamsasArchive.java +++ b/src/org/vamsas/client/simpleclient/VamsasArchive.java @@ -88,7 +88,11 @@ public class VamsasArchive { * @throws IOException if call to accessOriginal failed for updates, or openArchive failed. */ public VamsasArchive(File archive, boolean vamsasdocument) throws IOException { + this(archive, vamsasdocument, null); + } + public VamsasArchive(File archive, boolean vamsasdocument, Lock extantLock) throws IOException { super(); + 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")); @@ -97,6 +101,8 @@ public class VamsasArchive { this.vamsasdocument = vamsasdocument; if (archive.exists()) { this.original = archive; + this.odoclock = new SessionFile(archive); // lock the file *immediatly* + odoclock.lockFile(extantLock); this.archive = null; // archive will be a temp file when the open method is called virginArchive=false; try { @@ -107,6 +113,11 @@ public class VamsasArchive { } else { this.original = null; this.archive = archive; // archive is written in place. + if (archive!=null) { + archive.createNewFile(); + rchive=new SessionFile(archive); // lock the file *immediatly* + rchive.lockFile(extantLock); + } virginArchive = true; } this.openArchive(); // open archive @@ -227,6 +238,7 @@ public class VamsasArchive { * 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 * @throws IOException */ private void openArchive() throws IOException { @@ -249,8 +261,12 @@ public class VamsasArchive { log.warn("New archive file name already in use! Possible lock failure imminent?"); } - rchive = new SessionFile(archive); - rchive.lockFile(); + if (rchive==null) + rchive = new SessionFile(archive); + archive.createNewFile(); + if (!rchive.lockFile()) + throw new IOException("Failed to get lock on file "+archive); + newarchive = new JarOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(archive))); entries = new Hashtable(); } @@ -282,7 +298,7 @@ public class VamsasArchive { */ public AppDataOutputStream getAppDataStream(String appdataReference) throws IOException { if (newarchive==null) - openArchive(); + throw new IOException("Attempt to write to closed VamsasArchive object."); if (addValidEntry(appdataReference)) { return new AppDataOutputStream(newarchive); } @@ -357,7 +373,7 @@ public class VamsasArchive { private void closeAndReset() { if (rchive!=null) { rchive.unlockFile(); - rchive = null; + rchive=null; } if (original!=null) { if (odoc!=null) { diff --git a/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java b/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java index b00ebce..3b03137 100644 --- a/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java +++ b/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java @@ -3,6 +3,7 @@ package org.vamsas.client.simpleclient; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.RandomAccessFile; import java.util.Enumeration; import java.util.Iterator; import java.util.Vector; @@ -23,6 +24,9 @@ import org.apache.commons.logging.LogFactory; public class VamsasArchiveReader { private static Log log = LogFactory.getLog(VamsasArchiveReader.class); JarFile jfile; + boolean stream=false; // true if we are seeking on the stream. + RandomAccessFile rfile; + JarInputStream jstream; public VamsasArchiveReader(File vamsasfile) { jfile=null; if (vamsasfile.exists()) { @@ -36,6 +40,21 @@ public class VamsasArchiveReader { } /** + * in an ideal world - this constructor will create a reader object + * for the locked file's random access stream. + * + * @param vamsaslock + */ + public VamsasArchiveReader(Lock vamsaslock) { + rfile = vamsaslock.rafile; + stream = true; + // TODO: Implement stream based JarFile access + log.error("NOT IMPLEMENTED STREAM-BASED JAR ACCESS"); + throw new Error("Can't access a locked VamsasArchive file as a random access stream yet."); + // rfile.seek(0); + + } + /** * the vamsas document version(s) handled by this Reader */ final public static String DOCUMENT_VERSION="0.1"; diff --git a/src/org/vamsas/client/simpleclient/VamsasFile.java b/src/org/vamsas/client/simpleclient/VamsasFile.java index 6ad634b..4199dd2 100644 --- a/src/org/vamsas/client/simpleclient/VamsasFile.java +++ b/src/org/vamsas/client/simpleclient/VamsasFile.java @@ -63,6 +63,12 @@ public class VamsasFile extends SessionFile { * gets a locked Reader for the vamsas document. * @return reader for vamsasdocument.xml enrty */ + public Lock getLock() { + while (!lockFile()) + ; + return fileLock; + } + public java.io.Reader getDocumentReader() { try { -- 1.7.10.2