X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FSimpleClientFactory.java;fp=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FSimpleClientFactory.java;h=6f5509a93946bbad64499f98a93728fb899daf64;hb=e15b38a5b95e0aa5b11707728abb539c073fe27c;hp=0000000000000000000000000000000000000000;hpb=bfd813ba38e1d432d49a311422ade5ba33203a43;p=vamsas.git diff --git a/src/org/vamsas/client/simpleclient/SimpleClientFactory.java b/src/org/vamsas/client/simpleclient/SimpleClientFactory.java new file mode 100644 index 0000000..6f5509a --- /dev/null +++ b/src/org/vamsas/client/simpleclient/SimpleClientFactory.java @@ -0,0 +1,262 @@ +/* +* VAMSAS Project +* + +* +* Dec 13, 2006 +* +*/ + +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.InvalidSessionUrnException; +import org.vamsas.client.NoDefaultSessionException; +import org.vamsas.client.SessionHandle; +import org.vamsas.client.UserHandle; + +/** + * + * + */ +public class SimpleClientFactory implements IClientFactory { + + private static Log log = LogFactory.getLog(SimpleClientFactory.class); + + private File sessionArena = null; + + private String vamsasSubdirectoryName = ".vamsas"; + + private SessionsFile sessionFile = null; + private static final String SESSION_LIST="sessions.obj"; + + //private String[] currentlyAvailableDessions = null; + + /** + * default constructor - called by CreateClientFactory only. + * + *Inits the sessionarena to the directory .vamsas of the user home directory. + * + */ + public SimpleClientFactory() throws IOException + { + // sessionArena + + //retrieves user home directory + String userHomeDirectory = System.getProperty("user.home"); + if (userHomeDirectory == null || userHomeDirectory.length()<1) + { + new IOException("Unable to detect user home directory"); + } + String sessionArenaPath = userHomeDirectory.concat(File.separator.concat(this.vamsasSubdirectoryName)); + + this.initSessionArena(sessionArenaPath); + this.initFactoryObjects(); + } + + + /** + * Create a client factory that works with sessions at the given + * path. + * @param path path to directory called session arena, where will be created session directories and session files. + */ + public SimpleClientFactory(String path) throws IOException + { + this.initSessionArena(path); + } + /** + * Inits sessionArena to a given path. + * checks if path is valid. + * + * @param path path to a directory to use + * @throws IOException if the path is incorrect + */ + private void initSessionArena (String path) throws IOException + { + // Check path is valid and read/writeable. + File arenaFile = new File (path); + if (!arenaFile.exists()) + { + if (! arenaFile.mkdirs()) + { + this.sessionArena = null; + throw(new IOException("Unable to create a directory called "+path)); + } + } + if (arenaFile.exists() && arenaFile.isDirectory() && arenaFile.canRead() && arenaFile.canWrite()) + { + this.sessionArena = arenaFile; + } + else + { + this.sessionArena = null; + throw(new IOException("Cannot read and write to a directory called "+path)); + } + } + + /** + * construct SessionFile objects and watchers for each + */ + private void initFactoryObjects() throws IOException { + if (this.sessionFile!=null ) + throw new IOException("initFactoryObjects called for initialised ClientFactory object."); + this.sessionFile = new SessionsFile(new File(this.sessionArena,SESSION_LIST)); + + } + /** + * @see org.vamsas.client.IClientFactory#getCurrentSessions() + */ + public String[] getCurrentSessions() + { + String[] sessions = null; + if (this.sessionFile!=null ) + { + SessionHandle[] sessionHandles = this.sessionFile.retrieveSessionsList(); + if (sessionHandles != null) + { + sessions = new String[sessionHandles.length]; + for (int i = sessionHandles.length -1; i > 0; i--) + { + SessionHandle sessionHandle = sessionHandles[i]; + sessions [i] = sessionHandle.getSessionUrn(); + } + } + } + return sessions; + } + + + private void discoverSession() + { + + } + + /** + * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle) + * + * Creates a IClient object, using default UserHandle with system variables:"user.name" or "USERNAME")), + "host.name" or "HOSTNAME" + */ + public IClient getIClient(ClientHandle applicationHandle) + throws NoDefaultSessionException { + + return this.getIClient(applicationHandle, (UserHandle) null); + } + + /** + * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, java.lang.String) + */ + public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) { + // TODO Auto-generated method stub + return null; + } + + /** + * @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) { + // TODO Auto-generated method stub + return null; + } + + /** + * @see org.vamsas.client.IClientFactory#getIClient(org.vamsas.client.ClientHandle, org.vamsas.client.UserHandle) + */ + public IClient getIClient(ClientHandle applicationHandle, UserHandle userId) + throws NoDefaultSessionException { + SimpleClient client = null; + if (this.sessionArena==null) + throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena."); + + ClientHandle clientHandle =applicationHandle; + //create default clientHandle with "SimpleVamsasClientApp","0.1", + if (clientHandle == null) + clientHandle = new ClientHandle("SimpleVamsasClientApp","0.1"); + + //check if any available session(s) + String[] availableSessions = this.getCurrentSessions(); + if (availableSessions != null) + {//there are available sessions + if (availableSessions.length>1) + {//more than one session if available... can not choose + + //represents list of session as String + StringBuffer sessionURNs = new StringBuffer(""); + for (int i = 0; i< availableSessions.length ; i++) + { + sessionURNs.append(availableSessions[i]+" "); + } + throw new NoDefaultSessionException("Several sessions available, please pick one: "+sessionURNs); + } + + //check if only one session available. if yes, open it + if (availableSessions.length == 1) + { + //only one session available, open it. + return this.getIClient(clientHandle, availableSessions[0]); + } + } + //no session available - create a new one + + + try + { + //create sessionDirectory + File sessdir = File.createTempFile("sess", ".simpleclient", this.sessionArena); + log.debug("Creating new session directory"); + if (!(sessdir.delete() && sessdir.mkdir())) + throw new IOException("Could not make session directory "+sessdir); + //create session + VamsasSession vamsasSession = new VamsasSession(sessdir); + + this.getSessionFile().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()), false); + if (userId == null) + { + //create a default userHandle + //with current OS user and hostname + userId = new UserHandle(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")), + System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));// clientName, clientVersion, sessionPath); + } + + + //create simple client + client = new SimpleClient(userId, clientHandle, vamsasSession); + } + catch (IOException e) + { + log.error("error while creating new IClient",e); + } + catch (InvalidSessionUrnException e) + { + log.error("Unable to create new IClient. The session urn is incorrect ",e); + } + + return client; + } + + + /** + * @return the sessionFile + */ + private SessionsFile getSessionFile() throws IOException + { + if (this.sessionFile == null) + { + this.sessionFile = new SessionsFile( new File (this.sessionArena, SESSION_LIST)); + } + return this.sessionFile; + } + + + +}