package org.vamsas.client.simpleclient; import java.io.File; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.vamsas.client.ClientHandle; import org.vamsas.client.IClient; import org.vamsas.client.IClientFactory; import org.vamsas.client.NoDefaultSessionException; import org.vamsas.client.UserHandle; /** * TODO document type SimpleClientFactory * @author jimp * */ public class SimpleClientFactory implements IClientFactory { private static Log log = LogFactory.getLog(SimpleClientFactory.class); File sessionArena; /** * default constructor - called by CreateClientFactory only. * */ public SimpleClientFactory() { sessionArena = null; } /** * Create a client factory that works with sessions at the given * path. * @param path */ public SimpleClientFactory(String path) throws IOException { // Check path is valid and read/writeable. File newarena = new File(path); if (newarena.isDirectory() && newarena.canRead() && newarena.canWrite()) { sessionArena = newarena; } else { sessionArena = null; throw(new IOException("Cannot read and write to a directory called "+path)); } } /* (non-Javadoc) * @see org.vamsas.client.IClientFactory#getCurrentSessions() */ public String[] getCurrentSessions() { // TODO look in the arena and enumerate session handles for return. return new String[] {}; } /* (non-Javadoc) * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle) */ public IClient getIClient(ClientHandle applicationHandle) throws NoDefaultSessionException { // create a new session // register new ClientHandle in session // create SimpleClient instance return null; } /* (non-Javadoc) * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, java.lang.String) */ public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) { // locate session from Urn // check that clientHandle is unique (with default user) - if not update the clientHandle urn to make it unique. // wait for lock and attach to session // create SimpleClient instance return null; } /* (non-Javadoc) * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle, java.lang.String) */ public IClient getIClient(ClientHandle applicationHandle, UserHandle userId, String sessionUrn) { // locate session from Urn // check Uniqueness of user + ClientHandle in the session. Update clientHandle urn accordingly. // wait for lock, attach to session // create client instance return null; } /* (non-Javadoc) * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle) */ public IClient getIClient(ClientHandle applicationHandle, UserHandle userId) throws NoDefaultSessionException { // create new session // register SimpleClient and UserHandles in session // create client instance return null; } /** * make a new vamsas session from the data in the archive vamsasdocument * @param applicationHandle * @param userId * @param vamsasdocument * @return */ public IClient openSession(ClientHandle applicationHandle, UserHandle userId, ArchiveUrn vamsasdocument) throws IOException { // verify applicationHandle and userId // TODO: verify applicationHandle and userId // verify vamsasdocument is a valid document. File vamdoc = vamsasdocument.asFile(); log.debug("Starting new session with data from "+vamsasdocument.getSessionUrn()+"(File is : "+vamdoc+")"); VamsasArchiveReader archive = new VamsasArchiveReader(vamdoc); // TODO: a real validity test. The below just checks it can be read. if (!archive.isValid()) throw new IOException(vamsasdocument.getSessionUrn()+" is not a valid vamsasDocument archive."); // create new session directory if (sessionArena==null) throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena."); File sessdir = File.createTempFile("sess", ".simpleclient", sessionArena); if (!(sessdir.delete() && sessdir.mkdir())) throw new IOException("Could not make session directory "+sessdir); VamsasSession sess = new VamsasSession(sessdir); // copy document into session directory sess.setVamsasDocument(vamsasdocument.asFile()); // create client instance and return. 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) { } }