1 package org.vamsas.client.simpleclient;
4 import java.io.IOException;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.vamsas.client.ClientHandle;
9 import org.vamsas.client.IClient;
10 import org.vamsas.client.IClientFactory;
11 import org.vamsas.client.UserHandle;
14 * TODO document type SimpleClientFactory
18 public class SimpleClientFactory implements IClientFactory {
19 private static Log log = LogFactory.getLog(SimpleClientFactory.class);
24 * default constructor - called by CreateClientFactory only.
27 public SimpleClientFactory() {
31 * Create a client factory that works with sessions at the given
35 public SimpleClientFactory(String path) throws IOException {
36 // Check path is valid and read/writeable.
37 File newarena = new File(path);
38 if (newarena.isDirectory() && newarena.canRead() && newarena.canWrite()) {
39 sessionArena = newarena;
42 throw(new IOException("Cannot read and write to a directory called "+path));
46 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle)
48 public IClient getIClient(ClientHandle applicationHandle) {
49 // create a new session
50 // register new ClientHandle in session
51 // create SimpleClient instance
56 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, java.lang.String)
58 public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) {
59 // locate session from Urn
60 // check that clientHandle is unique (with default user) - if not update the clientHandle urn to make it unique.
61 // wait for lock and attach to session
62 // create SimpleClient instance
67 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle, java.lang.String)
69 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId,
71 // locate session from Urn
72 // check Uniqueness of user + ClientHandle in the session. Update clientHandle urn accordingly.
73 // wait for lock, attach to session
74 // create client instance
79 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle)
81 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId) {
83 // register SimpleClient and UserHandles in session
84 // create client instance
88 * make a new vamsas session from the data in the archive vamsasdocument
89 * @param applicationHandle
91 * @param vamsasdocument
94 public IClient openSession(ClientHandle applicationHandle, UserHandle userId, ArchiveUrn vamsasdocument) throws IOException {
95 // verify applicationHandle and userId
96 // TODO: verify applicationHandle and userId
97 // verify vamsasdocument is a valid document.
98 File vamdoc = vamsasdocument.asFile();
99 log.debug("Starting new session with data from "+vamsasdocument.getSessionUrn()+"(File is : "+vamdoc+")");
100 VamsasArchiveReader archive = new VamsasArchiveReader(vamdoc);
101 // TODO: a real validity test. The below just checks it can be read.
102 if (!archive.isValid())
103 throw new IOException(vamsasdocument.getSessionUrn()+" is not a valid vamsasDocument archive.");
104 // create new session directory
105 if (sessionArena==null)
106 throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena.");
107 File sessdir = File.createTempFile("sess", ".simpleclient", sessionArena);
108 if (!(sessdir.delete() && sessdir.mkdir()))
109 throw new IOException("Could not make session directory "+sessdir);
110 VamsasSession sess = new VamsasSession(sessdir);
111 // copy document into session directory
112 sess.setVamsasDocument(vamsasdocument.asFile());
113 // create client instance and return.
114 SimpleClient client = new SimpleClient(userId, applicationHandle, sess);
118 public static void main(String[] args) {