c6d168708a076ce1cdf6d1f8e6d103b2b6cd6d1f
[vamsas.git] / src / org / vamsas / client / simpleclient / SimpleClientFactory.java
1 package org.vamsas.client.simpleclient;
2
3 import java.io.File;
4 import java.io.IOException;
5
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;
12
13 /**
14  * TODO document type SimpleClientFactory
15  * @author jimp
16  *
17  */
18 public class SimpleClientFactory implements IClientFactory {
19   private static Log log = LogFactory.getLog(SimpleClientFactory.class);
20
21   File sessionArena;
22   
23   /**
24    * default constructor - called by CreateClientFactory only.
25    *
26    */
27   public SimpleClientFactory() {
28     sessionArena = null;
29   }
30   /**
31    * Create a client factory that works with sessions at the given
32    * path.
33    * @param path
34    */
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;
40     } else {
41       sessionArena = null;
42       throw(new IOException("Cannot read and write to a directory called "+path));
43     }
44   }
45   /* (non-Javadoc)
46    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle)
47    */
48   public IClient getIClient(ClientHandle applicationHandle) {
49     // create a new session
50     // register new ClientHandle in session
51     // create SimpleClient instance
52     return null;
53   }
54
55   /* (non-Javadoc)
56    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, java.lang.String)
57    */
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
63     return null;
64   }
65
66   /* (non-Javadoc)
67    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle, java.lang.String)
68    */
69   public IClient getIClient(ClientHandle applicationHandle, UserHandle userId,
70       String sessionUrn) {
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
75     return null;
76   }
77
78   /* (non-Javadoc)
79    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle)
80    */
81   public IClient getIClient(ClientHandle applicationHandle, UserHandle userId) {
82     // create new session
83     // register SimpleClient and UserHandles in session
84     // create client instance
85     return null;
86   }
87   /**
88    * make a new vamsas session from the data in the archive vamsasdocument 
89    * @param applicationHandle
90    * @param userId
91    * @param vamsasdocument
92    * @return
93    */
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);
115     
116     return client;
117   }
118   public static void main(String[] args) {
119   }
120 }