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.NoDefaultSessionException;
12 import org.vamsas.client.UserHandle;
15 * TODO document type SimpleClientFactory
19 public class SimpleClientFactory implements IClientFactory {
20 private static Log log = LogFactory.getLog(SimpleClientFactory.class);
25 * default constructor - called by CreateClientFactory only.
28 public SimpleClientFactory() {
32 * Create a client factory that works with sessions at the given
36 public SimpleClientFactory(String path) throws IOException {
37 // Check path is valid and read/writeable.
38 File newarena = new File(path);
39 if (newarena.isDirectory() && newarena.canRead() && newarena.canWrite()) {
40 sessionArena = newarena;
43 throw(new IOException("Cannot read and write to a directory called "+path));
48 * @see org.vamsas.client.IClientFactory#getCurrentSessions()
50 public String[] getCurrentSessions() {
51 // TODO look in the arena and enumerate session handles for return.
52 return new String[] {};
55 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle)
57 public IClient getIClient(ClientHandle applicationHandle) throws NoDefaultSessionException {
58 // create a new session
59 // register new ClientHandle in session
60 // create SimpleClient instance
65 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, java.lang.String)
67 public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) {
68 // locate session from Urn
69 // check that clientHandle is unique (with default user) - if not update the clientHandle urn to make it unique.
70 // wait for lock and attach to session
71 // create SimpleClient instance
76 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle, java.lang.String)
78 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId,
80 // locate session from Urn
81 // check Uniqueness of user + ClientHandle in the session. Update clientHandle urn accordingly.
82 // wait for lock, attach to session
83 // create client instance
88 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle)
90 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId) throws NoDefaultSessionException {
92 // register SimpleClient and UserHandles in session
93 // create client instance
97 * make a new vamsas session from the data in the archive vamsasdocument
98 * @param applicationHandle
100 * @param vamsasdocument
103 public IClient openSession(ClientHandle applicationHandle, UserHandle userId, ArchiveUrn vamsasdocument) throws IOException {
104 // verify applicationHandle and userId
105 // TODO: verify applicationHandle and userId
106 // verify vamsasdocument is a valid document.
107 File vamdoc = vamsasdocument.asFile();
108 log.debug("Starting new session with data from "+vamsasdocument.getSessionUrn()+"(File is : "+vamdoc+")");
109 VamsasArchiveReader archive = new VamsasArchiveReader(vamdoc);
110 // TODO: a real validity test. The below just checks it can be read.
111 if (!archive.isValid())
112 throw new IOException(vamsasdocument.getSessionUrn()+" is not a valid vamsasDocument archive.");
113 // create new session directory
114 if (sessionArena==null)
115 throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena.");
116 File sessdir = File.createTempFile("sess", ".simpleclient", sessionArena);
117 if (!(sessdir.delete() && sessdir.mkdir()))
118 throw new IOException("Could not make session directory "+sessdir);
119 VamsasSession sess = new VamsasSession(sessdir);
120 // copy document into session directory
121 sess.setVamsasDocument(vamsasdocument.asFile());
122 // create client instance and return.
123 SimpleClient client = new SimpleClient(userId, applicationHandle, sess);
127 public static void main(String[] args) {