--- /dev/null
+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
+ }
+
+}
*/
package org.vamsas.client;
-import java.io.File;
import java.net.URI;
import java.util.Hashtable;
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
*/
/**
* 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
}
package org.vamsas.client.simpleclient;
import java.io.File;
+import java.net.MalformedURLException;
/**
* Vamsas Document URN for files understood by ArchiveReader and
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
}
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;
* @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());
- }
- }
- }
}
/*
// 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();
}
}
package org.vamsas.client.simpleclient;
import java.io.File;
+import java.net.MalformedURLException;
/**
* SessionUrn for simpleclient sessions:
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
}
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
+import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Vector;
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;
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.
* @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
* @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 ?
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
* (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()
// TODO Auto-generated method stub
}
+ /* (non-Javadoc)
+ * @see java.lang.Object#finalize()
+ */
+ protected void finalize() throws Throwable {
+ // TODO Auto-generated method stub
+ super.finalize();
+ }
}
// 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) {
}
}
// 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<sessions.length; s++)
+ System.err.println(sessions[s]);
+ System.exit(2);
+ }
addHandlers(vorbaclient);
try {
vorbaclient.joinSession();