From 235e04a76494db78a018b52e8ad2aa74515b3d85 Mon Sep 17 00:00:00 2001 From: jprocter Date: Mon, 20 Mar 2006 13:03:34 +0000 Subject: [PATCH] debugging the org.vamsas.client.SessionUrn selection mechanism (also partial bits for simpleclientDocument stuff) git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@188 be28352e-c001-0410-b1a7-c7978e42abec --- .../vamsas/client/InvalidSessionUrnException.java | 38 +++++++++++ src/org/vamsas/client/SessionUrn.java | 24 +++---- src/org/vamsas/client/simpleclient/ArchiveUrn.java | 14 ++++- .../vamsas/client/simpleclient/ClientDocument.java | 62 +++++++++--------- src/org/vamsas/client/simpleclient/SessionUrn.java | 17 +++-- .../vamsas/client/simpleclient/SimpleClient.java | 17 ++++- .../client/simpleclient/SimpleClientAppdata.java | 66 ++++++++++++++++++++ .../client/simpleclient/SimpleClientFactory.java | 10 ++- src/org/vamsas/test/ExampleApplication.java | 10 ++- 9 files changed, 200 insertions(+), 58 deletions(-) create mode 100644 src/org/vamsas/client/InvalidSessionUrnException.java diff --git a/src/org/vamsas/client/InvalidSessionUrnException.java b/src/org/vamsas/client/InvalidSessionUrnException.java new file mode 100644 index 0000000..d1863a8 --- /dev/null +++ b/src/org/vamsas/client/InvalidSessionUrnException.java @@ -0,0 +1,38 @@ +package org.vamsas.client; + +public class InvalidSessionUrnException extends Exception { + + /** + * + */ + public InvalidSessionUrnException() { + super("Invalid Vamsas Session URN."); + // TODO Auto-generated constructor stub + } + + /** + * @param message + * @param cause + */ + public InvalidSessionUrnException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + /** + * @param message + */ + public InvalidSessionUrnException(String message) { + super("Invalid Vamsas Session URN: "+message); + // TODO Auto-generated constructor stub + } + + /** + * @param cause + */ + public InvalidSessionUrnException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + +} diff --git a/src/org/vamsas/client/SessionUrn.java b/src/org/vamsas/client/SessionUrn.java index 52417f9..4eff113 100644 --- a/src/org/vamsas/client/SessionUrn.java +++ b/src/org/vamsas/client/SessionUrn.java @@ -3,7 +3,6 @@ */ package org.vamsas.client; -import java.io.File; import java.net.URI; import java.util.Hashtable; import java.util.Vector; @@ -14,7 +13,7 @@ import java.util.Vector; * uses java.net.URI internally for construction of URN */ public abstract class SessionUrn { - URI urn; + protected URI urn; /** * The types of URI protocols we understand */ @@ -23,22 +22,19 @@ public abstract class SessionUrn { /** * construct urn for a locally stored session file * @param type - * @param file + * @param url */ - protected SessionUrn(String type, File file) { + protected SessionUrn(String type, java.net.URL url) { if (!TYPES.containsKey(type.toLowerCase())) - throw new Error("Unknown "+this.getClass().getName()+" type '"+type+"' for file '"+file.getAbsolutePath()+"'"); - urn = URI.create(type+"://"+file.getAbsolutePath()); + throw new Error("Unknown "+this.getClass().getName()+" type '"+type+"' for URL '"+url+"'"); + try { + urn = URI.create(type+"://"+url.getPath()); + } catch (Exception e) { + // TODO: something better than throwing an error should be done here. + throw new Error(e); + } } public String getSessionUrn() { return urn.toString(); } - /** - * TODO: LATER: think about this again. - * @return File(urn.getPath()) - */ - public File asFile() { - return new File(urn.getPath()); - } - // TODO: add abstract 'handler' methods for resolving the URN to a particular class } diff --git a/src/org/vamsas/client/simpleclient/ArchiveUrn.java b/src/org/vamsas/client/simpleclient/ArchiveUrn.java index 93e889c..304e1f2 100644 --- a/src/org/vamsas/client/simpleclient/ArchiveUrn.java +++ b/src/org/vamsas/client/simpleclient/ArchiveUrn.java @@ -1,6 +1,7 @@ package org.vamsas.client.simpleclient; import java.io.File; +import java.net.MalformedURLException; /** * Vamsas Document URN for files understood by ArchiveReader and @@ -18,7 +19,16 @@ public class ArchiveUrn extends org.vamsas.client.SessionUrn { TYPES.put(ArchiveUrn.VAMSASDOCUMENT, ArchiveUrn.class); } - public ArchiveUrn(File docLocation) { - super(VAMSASDOCUMENT, docLocation); + public ArchiveUrn(File docLocation) throws MalformedURLException { + super(VAMSASDOCUMENT, docLocation.getAbsoluteFile().toURL()); } + + /** + * TODO: LATER: think about this again. + * @return File(urn.getPath()) + */ + public File asFile() { + return new File(urn.getPath()); + } + // TODO: add abstract 'handler' methods for resolving the URN to a particular class } diff --git a/src/org/vamsas/client/simpleclient/ClientDocument.java b/src/org/vamsas/client/simpleclient/ClientDocument.java index fade094..e6ef1f5 100644 --- a/src/org/vamsas/client/simpleclient/ClientDocument.java +++ b/src/org/vamsas/client/simpleclient/ClientDocument.java @@ -27,7 +27,7 @@ import org.vamsas.objects.utils.AppDataReference; public class ClientDocument extends org.vamsas.client.ClientDocument implements IClientDocument { private static Log log = LogFactory.getLog(ClientDocument.class); private VamsasDocument doc; - protected IClient vorba; + protected SimpleClient sclient; protected ApplicationData appsglobal=null; protected User usersdata=null; protected byte[] appData=null; @@ -38,43 +38,17 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements * @param doc - the dataset * @param docHandler - the sessionFile IO handler * @param Factory - the source of current and new vorbaIds - * @param vorba - the client instance + * @param sclient - the simpleclient instance */ - protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, IClient vorba) { + protected ClientDocument(VamsasDocument doc, VamsasArchive docHandler, IdFactory Factory, SimpleClient vorba) { super(Factory.getVorbaIdHash(), Factory); /** * prepare Application-side dataset from the vamsas Document archive */ - this.vorba = vorba; + this.sclient = vorba; archive = docHandler; this.doc = doc; - extractAppData(); - } - /** - * gets appropriate app data for the application, if it exists in this dataset - * - */ - private void extractAppData() { - Vector apldataset = AppDataReference.getUserandApplicationsData( - doc, vorbafactory.getUserHandle(), vorbafactory.getClientHandle()); - if (apldataset!=null) { - if (apldataset.size()>0) { - AppData clientdat = (AppData) apldataset.get(0); - if (clientdat instanceof ApplicationData) { - appsglobal = (ApplicationData) clientdat; - if (apldataset.size()>1) { - clientdat = (AppData) apldataset.get(1); - if (clientdat instanceof User) - usersdata = (User) clientdat; - if (apldataset.size()>2) - log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query."); - } - } else { - log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName()); - } - } - } } /* @@ -192,11 +166,35 @@ public class ClientDocument extends org.vamsas.client.ClientDocument implements // TODO: add provenance stuff to newly registered Vobject return _registerObject(unregistered); } + SimpleClientAppdata scappd = null; /* (non-Javadoc) * @see org.vamsas.client.IClientDocument#getClientAppdata() */ public IClientAppdata getClientAppdata() { - // TODO Auto-generated method stub - return null; + if (scappd==null) { + log.debug("Creating new SimpleClientAppdata instance for "+sclient.getSessionHandle()); + scappd = new SimpleClientAppdata(this); + if (scappd==null) { + // LATER: may not need this as a warning message. + log.warn("Null appdata object for "+sclient.getSessionHandle()); + } else { + log.debug("Created SimpleClientAppdata successfully."); + } + } else { + log.debug("Returning existing SimpleClientAppdata reference."); + } + return scappd; + } + /* (non-Javadoc) + * @see java.lang.Object#finalize() + */ + protected void finalize() throws Throwable { + log.debug("Garbage collecting on ClientDocument instance."); + if (scappd!=null) { + scappd.finalize(); + scappd = null; + } + + super.finalize(); } } diff --git a/src/org/vamsas/client/simpleclient/SessionUrn.java b/src/org/vamsas/client/simpleclient/SessionUrn.java index f96c74c..13b6f23 100644 --- a/src/org/vamsas/client/simpleclient/SessionUrn.java +++ b/src/org/vamsas/client/simpleclient/SessionUrn.java @@ -1,6 +1,7 @@ package org.vamsas.client.simpleclient; import java.io.File; +import java.net.MalformedURLException; /** * SessionUrn for simpleclient sessions: @@ -19,13 +20,21 @@ public class SessionUrn extends org.vamsas.client.SessionUrn { TYPES.put(SessionUrn.VAMSASDOCUMENT, SessionUrn.class); } - public SessionUrn(File sessionLocation) { + public SessionUrn(File sessionLocation) throws MalformedURLException { // TODO: LATER: implement switch to have vamsas document or simpleclient sessions for same constructor - super(SIMPLECLIENT, sessionLocation); + super(SIMPLECLIENT, sessionLocation.getAbsoluteFile().toURL()); //else // super(VAMSASDOCUMENT, sessionLocation); } - public SessionUrn(VamsasSession session) { - super(SIMPLECLIENT, session.sessionDir); + public SessionUrn(VamsasSession session) throws MalformedURLException { + super(SIMPLECLIENT, session.sessionDir.getAbsoluteFile().toURL()); } + /** + * TODO: LATER: think about this again. + * @return File(urn.getPath()) + */ + public File asFile() { + return new File(urn.getPath()); + } + // TODO: add abstract 'handler' methods for resolving the URN to a particular class } diff --git a/src/org/vamsas/client/simpleclient/SimpleClient.java b/src/org/vamsas/client/simpleclient/SimpleClient.java index 0445e2a..b15a3f5 100644 --- a/src/org/vamsas/client/simpleclient/SimpleClient.java +++ b/src/org/vamsas/client/simpleclient/SimpleClient.java @@ -13,6 +13,7 @@ import java.beans.PropertyChangeSupport; import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.util.Hashtable; import java.util.Vector; @@ -22,6 +23,7 @@ import org.vamsas.client.ClientHandle; import org.vamsas.client.Events; import org.vamsas.client.IClient; import org.vamsas.client.IClientDocument; +import org.vamsas.client.InvalidSessionUrnException; import org.vamsas.client.SessionHandle; import org.vamsas.client.UserHandle; import org.vamsas.objects.core.LockFile; @@ -42,6 +44,7 @@ public class SimpleClient implements IClient { protected VamsasSession _session; protected ClientHandle client = null; protected EventGeneratorThread evgen = null; + protected ClientDocument cdocument = null; /** * construct a transient IdFactory instance - this should last only as long as the * SimpleClient object holds the lock on the vamsas document being created/manipulated. @@ -58,12 +61,17 @@ public class SimpleClient implements IClient { * @param client * @param sess */ - protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess) { + protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess) throws InvalidSessionUrnException { // TODO: validate user/client/session _session = sess; this.user = user; this.client = client; - session = new SessionUrn(_session); + try { + session = new SessionUrn(_session); + } catch (MalformedURLException e) { + log.error("Couldn't form a valid SessionUrn object!",e); + throw new InvalidSessionUrnException(_session.toString()); + } } /** * construct new session by importing objects from an existing vamsas document @@ -192,7 +200,10 @@ public class SimpleClient implements IClient { * @see org.vamsas.client.IClient#getClientDocument() */ public IClientDocument getClientDocument() throws IOException { - + if (cdocument!=null) { + // cdocument is non-nill if the ClientDocument.finalise() method hasn't been called. + return cdocument; + } VamsasArchive va = null; try { // TODO: bail out if it takes too long to get the lock ? diff --git a/src/org/vamsas/client/simpleclient/SimpleClientAppdata.java b/src/org/vamsas/client/simpleclient/SimpleClientAppdata.java index 9a68c4b..9f6b27f 100644 --- a/src/org/vamsas/client/simpleclient/SimpleClientAppdata.java +++ b/src/org/vamsas/client/simpleclient/SimpleClientAppdata.java @@ -5,8 +5,15 @@ package org.vamsas.client.simpleclient; import java.io.DataInput; import java.io.DataOutput; +import java.util.Vector; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.vamsas.client.IClientAppdata; +import org.vamsas.objects.core.AppData; +import org.vamsas.objects.core.ApplicationData; +import org.vamsas.objects.core.User; +import org.vamsas.objects.utils.AppDataReference; /** * @author jimp @@ -14,6 +21,58 @@ import org.vamsas.client.IClientAppdata; * (or byte buffer input stream) or written to a VamsasArchive stream. */ public class SimpleClientAppdata implements IClientAppdata { + private static Log log = LogFactory.getLog(SimpleClientAppdata.class); + + ClientDocument clientdoc; + /** + * state flags + * - accessed ClientAppdata + * - accessed UserAppdata + * => inputStream from embedded xml or jar entry of backup has been created + * - set ClientAppdata + * - set UserAppdata + * => an output stream has been created and written to - or a data chunk has been written. + * - need flag for switching between embedded and jar entry mode ? - always write a jar entry for a stream. + * - need code for rewind and overwriting if the set*Appdata methods are called more than once. + * - need flags for streams to except a call to set*Appdata when an output stream exists and is open. + * - need + * @param clientdoc The ClientDocument instance that this IClientAppData is accessing + */ + protected SimpleClientAppdata(ClientDocument clientdoc) { + if (clientdoc==null) { + log.fatal("Implementation error - Null ClientDocument for SimpleClientAppdata construction."); + throw new Error("Implementation error - Null ClientDocument for SimpleClientAppdata construction."); + } + this.clientdoc = clientdoc; + } + /** + * gets appropriate app data for the application, if it exists in this dataset + * + */ + private void extractAppData() { + org.vamsas.objects.core.VamsasDocument doc = null; + Vector apldataset = AppDataReference.getUserandApplicationsData( + doc, clientdoc.sclient.getUserHandle(), clientdoc.sclient.getClientHandle()); + ApplicationData appsglobal=null; + User usersdata = null; + if (apldataset!=null) { + if (apldataset.size()>0) { + AppData clientdat = (AppData) apldataset.get(0); + if (clientdat instanceof ApplicationData) { + appsglobal = (ApplicationData) clientdat; + if (apldataset.size()>1) { + clientdat = (AppData) apldataset.get(1); + if (clientdat instanceof User) + usersdata = (User) clientdat; + if (apldataset.size()>2) + log.info("Ignoring additional ("+(apldataset.size()-2)+") AppDatas returned by document appdata query."); + } + } else { + log.warn("Unexpected entry in AppDataReference query: id="+clientdat.getVorbaId()+" type="+clientdat.getClass().getName()); + } + } + } + } /* (non-Javadoc) * @see org.vamsas.client.IClientAppdata#getClientAppdata() @@ -94,5 +153,12 @@ public class SimpleClientAppdata implements IClientAppdata { // TODO Auto-generated method stub } + /* (non-Javadoc) + * @see java.lang.Object#finalize() + */ + protected void finalize() throws Throwable { + // TODO Auto-generated method stub + super.finalize(); + } } diff --git a/src/org/vamsas/client/simpleclient/SimpleClientFactory.java b/src/org/vamsas/client/simpleclient/SimpleClientFactory.java index d3148d7..640ad78 100644 --- a/src/org/vamsas/client/simpleclient/SimpleClientFactory.java +++ b/src/org/vamsas/client/simpleclient/SimpleClientFactory.java @@ -120,10 +120,16 @@ public class SimpleClientFactory implements IClientFactory { // copy document into session directory sess.setVamsasDocument(vamsasdocument.asFile()); // create client instance and return. - SimpleClient client = new SimpleClient(userId, applicationHandle, sess); - + SimpleClient client=null; + try { + client = new SimpleClient(userId, applicationHandle, sess); + } catch (Exception e) { + log.error("Couldn't make a new SimpleClient instance.",e); + throw new IOException(e.getMessage()); + } return client; } + public static void main(String[] args) { } } diff --git a/src/org/vamsas/test/ExampleApplication.java b/src/org/vamsas/test/ExampleApplication.java index 8118295..e881f3a 100644 --- a/src/org/vamsas/test/ExampleApplication.java +++ b/src/org/vamsas/test/ExampleApplication.java @@ -114,7 +114,15 @@ public class ExampleApplication { // get an Iclient with session data app = new ClientHandle("org.vamsas.test.ExampleApplication","0.1"); user = new UserHandle("arnolduser","deathsdoor"); - vorbaclient = clientfactory.getIClient(app, user); + try { + vorbaclient = clientfactory.getIClient(app, user); + } catch (NoDefaultSessionException e) { + System.err.println("There appear to be several sessions to choose from :"); + String[] sessions = clientfactory.getCurrentSessions(); + for (int s=0;s