10 package org.vamsas.client.simpleclient;
14 import java.io.IOException;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.vamsas.client.ClientHandle;
20 import org.vamsas.client.IClient;
21 import org.vamsas.client.IClientFactory;
22 import org.vamsas.client.InvalidSessionUrnException;
23 import org.vamsas.client.NoDefaultSessionException;
24 import org.vamsas.client.SessionHandle;
25 import org.vamsas.client.UserHandle;
31 public class SimpleClientFactory implements IClientFactory {
33 private static Log log = LogFactory.getLog(SimpleClientFactory.class);
35 private File sessionArena = null;
37 private String vamsasSubdirectoryName = ".vamsas";
39 private SessionsFile sessionFile = null;
40 private static final String SESSION_LIST="sessions.obj";
42 //private String[] currentlyAvailableDessions = null;
45 * default constructor - called by CreateClientFactory only.
47 *Inits the sessionarena to the directory .vamsas of the user home directory.
50 public SimpleClientFactory() throws IOException
54 //retrieves user home directory
55 String userHomeDirectory = System.getProperty("user.home");
56 if (userHomeDirectory == null || userHomeDirectory.length()<1)
58 new IOException("Unable to detect user home directory");
60 String sessionArenaPath = userHomeDirectory.concat(File.separator.concat(this.vamsasSubdirectoryName));
62 this.initSessionArena(sessionArenaPath);
63 this.initFactoryObjects();
68 * Create a client factory that works with sessions at the given
70 * @param path path to directory called session arena, where will be created session directories and session files.
72 public SimpleClientFactory(String path) throws IOException
74 this.initSessionArena(path);
77 * Inits sessionArena to a given path.
78 * checks if path is valid.
80 * @param path path to a directory to use
81 * @throws IOException if the path is incorrect
83 private void initSessionArena (String path) throws IOException
85 // Check path is valid and read/writeable.
86 File arenaFile = new File (path);
87 if (!arenaFile.exists())
89 if (! arenaFile.mkdirs())
91 this.sessionArena = null;
92 throw(new IOException("Unable to create a directory called "+path));
95 if (arenaFile.exists() && arenaFile.isDirectory() && arenaFile.canRead() && arenaFile.canWrite())
97 this.sessionArena = arenaFile;
101 this.sessionArena = null;
102 throw(new IOException("Cannot read and write to a directory called "+path));
107 * construct SessionFile objects and watchers for each
109 private void initFactoryObjects() throws IOException {
110 if (this.sessionFile!=null )
111 throw new IOException("initFactoryObjects called for initialised ClientFactory object.");
112 this.sessionFile = new SessionsFile(new File(this.sessionArena,SESSION_LIST));
116 * @see org.vamsas.client.IClientFactory#getCurrentSessions()
118 public String[] getCurrentSessions()
120 String[] sessions = null;
121 if (this.sessionFile!=null )
123 SessionHandle[] sessionHandles = this.sessionFile.retrieveSessionsList();
124 if (sessionHandles != null)
126 sessions = new String[sessionHandles.length];
127 for (int i = sessionHandles.length -1; i > 0; i--)
129 SessionHandle sessionHandle = sessionHandles[i];
130 sessions [i] = sessionHandle.getSessionUrn();
138 private void discoverSession()
144 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle)
146 * Creates a IClient object, using default UserHandle with system variables:"user.name" or "USERNAME")),
147 "host.name" or "HOSTNAME"
149 public IClient getIClient(ClientHandle applicationHandle)
150 throws NoDefaultSessionException {
152 return this.getIClient(applicationHandle, (UserHandle) null);
156 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, java.lang.String)
158 public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) {
159 // TODO Auto-generated method stub
164 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle, java.lang.String)
166 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId,
168 // TODO Auto-generated method stub
173 * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle)
175 public IClient getIClient(ClientHandle applicationHandle, UserHandle userId)
176 throws NoDefaultSessionException {
177 SimpleClient client = null;
178 if (this.sessionArena==null)
179 throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena.");
181 ClientHandle clientHandle =applicationHandle;
182 //create default clientHandle with "SimpleVamsasClientApp","0.1",
183 if (clientHandle == null)
184 clientHandle = new ClientHandle("SimpleVamsasClientApp","0.1");
186 //check if any available session(s)
187 String[] availableSessions = this.getCurrentSessions();
188 if (availableSessions != null)
189 {//there are available sessions
190 if (availableSessions.length>1)
191 {//more than one session if available... can not choose
193 //represents list of session as String
194 StringBuffer sessionURNs = new StringBuffer("");
195 for (int i = 0; i< availableSessions.length ; i++)
197 sessionURNs.append(availableSessions[i]+" ");
199 throw new NoDefaultSessionException("Several sessions available, please pick one: "+sessionURNs);
202 //check if only one session available. if yes, open it
203 if (availableSessions.length == 1)
205 //only one session available, open it.
206 return this.getIClient(clientHandle, availableSessions[0]);
209 //no session available - create a new one
214 //create sessionDirectory
215 File sessdir = File.createTempFile("sess", ".simpleclient", this.sessionArena);
216 log.debug("Creating new session directory");
217 if (!(sessdir.delete() && sessdir.mkdir()))
218 throw new IOException("Could not make session directory "+sessdir);
220 VamsasSession vamsasSession = new VamsasSession(sessdir);
222 this.getSessionFile().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()), false);
225 //create a default userHandle
226 //with current OS user and hostname
227 userId = new UserHandle(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")),
228 System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));// clientName, clientVersion, sessionPath);
232 //create simple client
233 client = new SimpleClient(userId, clientHandle, vamsasSession);
235 catch (IOException e)
237 log.error("error while creating new IClient",e);
239 catch (InvalidSessionUrnException e)
241 log.error("Unable to create new IClient. The session urn is incorrect ",e);
249 * @return the sessionFile
251 private SessionsFile getSessionFile() throws IOException
253 if (this.sessionFile == null)
255 this.sessionFile = new SessionsFile( new File (this.sessionArena, SESSION_LIST));
257 return this.sessionFile;