640ad7821baea51ae0bd24e355a962f834dc93ff
[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.NoDefaultSessionException;
12 import org.vamsas.client.UserHandle;
13
14 /**
15  * TODO document type SimpleClientFactory
16  * @author jimp
17  *
18  */
19 public class SimpleClientFactory implements IClientFactory {
20   private static Log log = LogFactory.getLog(SimpleClientFactory.class);
21
22   File sessionArena;
23   
24   /**
25    * default constructor - called by CreateClientFactory only.
26    *
27    */
28   public SimpleClientFactory() {
29     sessionArena = null;
30   }
31   /**
32    * Create a client factory that works with sessions at the given
33    * path.
34    * @param path
35    */
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;
41     } else {
42       sessionArena = null;
43       throw(new IOException("Cannot read and write to a directory called "+path));
44     }
45   }
46   
47   /* (non-Javadoc)
48    * @see org.vamsas.client.IClientFactory#getCurrentSessions()
49    */
50   public String[] getCurrentSessions() {
51     // TODO look in the arena and enumerate session handles for return.
52     return new String[] {};
53   }
54   /* (non-Javadoc)
55    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle)
56    */
57   public IClient getIClient(ClientHandle applicationHandle) throws NoDefaultSessionException {
58     // create a new session
59     // register new ClientHandle in session
60     // create SimpleClient instance
61     return null;
62   }
63
64   /* (non-Javadoc)
65    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, java.lang.String)
66    */
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
72     return null;
73   }
74
75   /* (non-Javadoc)
76    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle, java.lang.String)
77    */
78   public IClient getIClient(ClientHandle applicationHandle, UserHandle userId,
79       String sessionUrn) {
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
84     return null;
85   }
86
87   /* (non-Javadoc)
88    * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle)
89    */
90   public IClient getIClient(ClientHandle applicationHandle, UserHandle userId) throws NoDefaultSessionException {
91     // create new session
92     // register SimpleClient and UserHandles in session
93     // create client instance
94     return null;
95   }
96   /**
97    * make a new vamsas session from the data in the archive vamsasdocument 
98    * @param applicationHandle
99    * @param userId
100    * @param vamsasdocument
101    * @return
102    */
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=null;
124     try {
125       client = new SimpleClient(userId, applicationHandle, sess);
126     } catch (Exception e) {
127       log.error("Couldn't make a new SimpleClient instance.",e);
128       throw new IOException(e.getMessage());
129     }
130     return client;
131   }
132   
133   public static void main(String[] args) {
134   }
135 }