10 package uk.ac.vamsas.client.simpleclient;
14 import java.io.IOException;
15 import java.net.MalformedURLException;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
21 import uk.ac.vamsas.client.ClientHandle;
22 import uk.ac.vamsas.client.IClient;
23 import uk.ac.vamsas.client.IClientFactory;
24 import uk.ac.vamsas.client.InvalidSessionUrnException;
25 import uk.ac.vamsas.client.NoDefaultSessionException;
26 import uk.ac.vamsas.client.SessionHandle;
27 import uk.ac.vamsas.client.UserHandle;
31 * creates a session arena in the user home directory under .vamsas. Each
32 * session has its own subdirectory.
34 public class SimpleClientFactory implements IClientFactory {
36 private static Log log = LogFactory.getLog(SimpleClientFactory.class);
38 private File sessionArena = null;
40 private String vamsasSubdirectoryName = ".vamsas";
42 private SimpleSessionManager sessionManager = null;
43 private static final String SESSION_LIST="sessions.obj";
45 // private String[] currentlyAvailableDessions = null;
48 * default constructor - called by CreateClientFactory only.
50 * Inits the sessionarena to the directory .vamsas of the user home directory.
53 public SimpleClientFactory() throws IOException
57 // retrieves user home directory
58 String userHomeDirectory = System.getProperty("user.home");
59 if (userHomeDirectory == null || userHomeDirectory.length()<1)
61 new IOException("Unable to detect user home directory");
63 String sessionArenaPath = userHomeDirectory.concat(File.separator.concat(this.vamsasSubdirectoryName));
65 this.initSessionArena(sessionArenaPath);
66 // this.initFactoryObjects();
71 * Create a client factory that works with sessions at the given path.
74 * path to directory called session arena, where will be created
75 * session directories and session files.
77 public SimpleClientFactory(String path) throws IOException
79 this.initSessionArena(path);
82 * Inits sessionArena to a given path. checks if path is valid.
85 * path to a directory to use
87 * if the path is incorrect
89 private void initSessionArena (String path) throws IOException
91 // Check path is valid and read/writeable.
92 File arenaFile = new File (path);
93 if (!arenaFile.exists())
95 if (! arenaFile.mkdirs())
97 this.sessionArena = null;
98 throw(new IOException("Unable to create a directory called "+path));
101 if (arenaFile.exists() && arenaFile.isDirectory() && arenaFile.canRead() && arenaFile.canWrite())
103 this.sessionArena = arenaFile;
107 this.sessionArena = null;
108 throw(new IOException("Cannot read and write to a directory called "+path));
117 * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle)
119 * Creates a IClient object, using default UserHandle with system
120 * variables:"user.name" or "USERNAME")), "host.name" or "HOSTNAME"
122 public IClient getIClient(ClientHandle applicationHandle)
123 throws NoDefaultSessionException {
124 // create a new session
125 // register new ClientHandle in session
126 // create SimpleClient instance
127 return this.getIClient(applicationHandle, (UserHandle) null);
131 * the URN should be something like simpleclient:FILEPATH URL encoded
133 * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle,
136 public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) {
137 // locate session from Urn
138 // check that clientHandle is unique (with default user) - if not update the
139 // clientHandle urn to make it unique.
140 // wait for lock and attach to session
141 // create SimpleClient instance
142 log.debug("Trying to create session with URN "+sessionUrn);
143 return this.getIClient(applicationHandle, null, sessionUrn);
147 private File convertSessionUrnToFile(String sessionUrn) throws InvalidSessionUrnException
149 if (sessionUrn == null)
151 log.debug("Incorrect URN: can not open session.");
152 throw new InvalidSessionUrnException();
155 SessionUrn urn = new SessionUrn(sessionUrn);
161 * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle,
162 * uk.ac.vamsas.client.UserHandle, java.lang.String)
164 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId,
166 // locate session from Urn
167 // check Uniqueness of user + ClientHandle in the session. Update
168 // clientHandle urn accordingly.
169 // wait for lock, attach to session
170 // create client instance
171 IClient client = null;
174 File sessionDirectory = this.convertSessionUrnToFile(sessionUrn);
176 log.debug("found session directory "+sessionDirectory.getAbsolutePath());
177 VamsasSession vamsasSession = new VamsasSession(sessionDirectory);
180 * if (userId == null) { //create a default userHandle //with current OS
181 * user and hostname userId = new UserHandle(System.getProperty("user.name",
182 * System.getProperty("USERNAME","Joe Doe")),
183 * System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown")
184 * ));// clientName, clientVersion, sessionPath); }
187 * //create simple client client = new SimpleClient(userId,
188 * applicationHandle, vamsasSession);
190 client = this.initClient(sessionDirectory, userId, applicationHandle);
191 } catch (MalformedURLException e) {
192 log.error("error while creating new IClient: incorrect session urn",e);
195 } catch (InvalidSessionUrnException e) {
196 log.error("error while creating new IClient: incorrect session urn",e);
198 } catch (IOException e) {
199 log.error("error while creating new IClient: file access error",e);
206 private IClient initClient( File sessdir, UserHandle userId, ClientHandle clientHandle) throws IOException, InvalidSessionUrnException
208 IClient client = null;
210 VamsasSession vamsasSession = new VamsasSession(sessdir);
212 this.getSessionManager().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()));
215 // create a default userHandle
216 // userId = new UserHandle(System.getProperty("user.name",
217 // System.getProperty("USERNAME","Joe Doe")),
218 // System.getProperty("host.name",System.getProperty("HOSTNAME",
219 // "Unknown") ));// clientName, clientVersion, sessionPath);
220 userId = new UserHandle(null, null);
223 // FullName and organisation should not be null (otherwise UserHandle equals
224 // method raises an java.lang.NullPointerException )
225 // use current OS user and hostname, if null
226 if ( userId.getFullName() == null)
228 userId.setFullName(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")));
231 if (userId.getOrganization() == null)
233 userId.setOrganization( System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));
236 if (clientHandle == null)
237 clientHandle = new ClientHandle("SimpleVamsasClientApp","0.1");
240 if (clientHandle.getClientName() == null)
242 clientHandle.setClientName("SimpleVamsasClientApp");
244 if (clientHandle.getVersion() == null)
246 clientHandle.setVersion("0.1");
250 // create simple client
251 client = new SimpleClient(userId, clientHandle, vamsasSession);
252 vamsasSession.addClient((SimpleClient) client);
253 vamsasSession.setSessionManager(this.getSessionManager());
257 * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle,
258 * uk.ac.vamsas.client.UserHandle)
260 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId)
261 throws NoDefaultSessionException {
262 // create new session
263 // register SimpleClient and UserHandles in session
264 // create client instance
265 IClient client = null;
266 if (this.sessionArena==null)
267 throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena.");
269 ClientHandle clientHandle =applicationHandle;
270 // create default clientHandle with "SimpleVamsasClientApp","0.1",
271 if (clientHandle == null)
272 clientHandle = new ClientHandle("SimpleVamsasClientApp","0.1");
275 if (clientHandle.getClientName() == null)
277 clientHandle.setClientName("SimpleVamsasClientApp");
281 if (clientHandle.getVersion() == null)
283 clientHandle.setVersion("0.1");
286 // check if any available session(s)
287 String[] availableSessions = this.getCurrentSessions();
288 if (availableSessions != null)
289 {// there are available sessions
290 if (availableSessions.length>1)
291 {// more than one session if available... can not choose
293 // represents list of session as String
294 StringBuffer sessionURNs = new StringBuffer("");
295 for (int i = 0; i< availableSessions.length ; i++)
297 sessionURNs.append(availableSessions[i]+" ");
299 throw new NoDefaultSessionException("Several sessions available, please pick one: "+sessionURNs);
302 // check if only one session available. if yes, open it
303 if (availableSessions.length == 1)
305 // only one session available, open it.
306 return this.getIClient(clientHandle, availableSessions[0]);
309 log.debug("No active session found");
312 // no session available - create a new one
314 client = clientInNewSession(userId, clientHandle);
320 * @param clientHandle
323 private IClient clientInNewSession(UserHandle userId,
324 ClientHandle clientHandle) {
325 IClient client = null;
328 // create sessionDirectory
329 File sessdir = File.createTempFile("sess", ".simpleclient", this.sessionArena);
330 log.debug("Creating new session directory");
331 if (!(sessdir.delete() && sessdir.mkdir()))
332 throw new IOException("Could not make session directory "+sessdir);
333 client = this.initClient(sessdir, userId, clientHandle);
335 catch (IOException e)
337 log.error("error while creating new IClient",e);
339 catch (InvalidSessionUrnException e)
341 log.error("Unable to create new IClient. The session urn is incorrect ",e);
347 * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()
349 public String[] getCurrentSessions() {
350 String [] sessions = null;
352 sessions =this.getSessionManager().getCurrentSessions();
353 } catch (IOException e) {
354 log.error("Unable to get available sessions",e);
362 * @return the sessionFile
364 private SimpleSessionManager getSessionManager() throws IOException
366 if (this.sessionManager == null)
368 this.sessionManager = new SimpleSessionManager( new File (this.sessionArena, SESSION_LIST));
370 return this.sessionManager;
374 public IClient getNewSessionIClient(ClientHandle applicationHandle) {
375 return clientInNewSession(null, applicationHandle);
379 public IClient getNewSessionIClient(ClientHandle applicationHandle,
381 return clientInNewSession(userId, applicationHandle);