import uk.ac.vamsas.client.ClientHandle;
import uk.ac.vamsas.client.IClient;
import uk.ac.vamsas.client.IClientFactory;
+import uk.ac.vamsas.client.InvalidSessionDocumentException;
import uk.ac.vamsas.client.InvalidSessionUrnException;
import uk.ac.vamsas.client.NoDefaultSessionException;
import uk.ac.vamsas.client.SessionHandle;
* //create simple client client = new SimpleClient(userId,
* applicationHandle, vamsasSession);
*/
- client = this.initClient(sessionDirectory, userId, applicationHandle);
+ client = this.initClient(sessionDirectory, userId, applicationHandle, null);
} catch (MalformedURLException e) {
log.error("error while creating new IClient: incorrect session urn", e);
client = null;
-
+ } catch (InvalidSessionDocumentException e)
+ {
+ log.error("error while creating new IClient: invalid session document", e);
+ client = null;
} catch (InvalidSessionUrnException e) {
log.error("error while creating new IClient: incorrect session urn", e);
client = null;
}
return client;
}
-
+ /**
+ * initialise the vamsas session state and create a SimpleClient object to connect to it
+ * @param sessdir newly created or existing session directory
+ * @param userId
+ * @param clientHandle
+ * @param vamsasDocument null or a document to pass to SimpleCLient to write into the sessdir
+ * @return the client
+ * @throws IOException if there are problems in session or client creation or if the session already has a vamsasDocument
+ * @throws InvalidSessionUrnException for a malformed sessdir
+ */
private IClient initClient(File sessdir, UserHandle userId,
- ClientHandle clientHandle) throws IOException, InvalidSessionUrnException {
+ ClientHandle clientHandle, File vamsasDocument) throws IOException, InvalidSessionUrnException, InvalidSessionDocumentException {
IClient client = null;
// create session
- VamsasSession vamsasSession = new VamsasSession(sessdir);
+ VamsasSession vamsasSession = null;
+ if (vamsasDocument==null)
+ {
+ vamsasSession = new VamsasSession(sessdir);
+ } else {
+ vamsasSession = new VamsasSession(sessdir, vamsasDocument);
+ }
this.getSessionManager().addSession(
new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()));
}
}
// no session available - create a new one
-
- client = clientInNewSession(userId, clientHandle);
+ try {
+ client = clientInNewSession(userId, clientHandle, null);
+ } catch (Exception e)
+ {
+ throw new Error("IMPLEMENTATION ERROR: unexpected exception when creating a new session to connect to.",e);
+ }
return client;
}
/**
- *
+ * create a new session directory and possibly import an existing document into it
* @param userId
* @param clientHandle
- * @return
+ * @param vamsasDocument null or a document file to copy into the new session
+ * @return null or a valid IClient instance
*/
private IClient clientInNewSession(UserHandle userId,
- ClientHandle clientHandle) {
+ ClientHandle clientHandle, File vamsasDocument) throws InvalidSessionDocumentException, InvalidSessionUrnException{
IClient client = null;
try {
+ // try and make a friendly session name
+ String sesspref = "";
+ if (vamsasDocument!=null)
+ {
+ sesspref = vamsasDocument.getName().replaceAll("([^-A-Za-z0-9]|\\.vdj)", "");
+ }
+ sesspref += (new java.util.Date()).toString().replaceAll("[^-A-Za-z0-9]","_");
// create sessionDirectory
- File sessdir = File.createTempFile("sess", ".simpleclient",
- this.sessionArena);
+ File sessdir = new File(sessionArena, sesspref+".simpleclient");
+ if (sessdir.exists())
+ {
+ // make a unique session name
+ sessdir = File.createTempFile(sesspref, ".simpleclient",
+ sessionArena);
+ } else {
+ if (!sessdir.createNewFile())
+ {
+ throw new Error("VAMSAS Implementation error : sesspref friendly session name is invalid on this platform - please tell the authors!");
+ }
+ }
log.debug("Creating new session directory");
if (!(sessdir.delete() && sessdir.mkdir()))
throw new IOException("Could not make session directory " + sessdir);
- client = this.initClient(sessdir, userId, clientHandle);
+ client = initClient(sessdir, userId, clientHandle, vamsasDocument);
} 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 ",
+ log.error("Unable to create new IClient. The new session urn is malformed.",
e);
}
}
public IClient getNewSessionIClient(ClientHandle applicationHandle) {
- return clientInNewSession(null, applicationHandle);
+ try {
+ return clientInNewSession(null, applicationHandle, null);
+ }
+ catch (Exception e) {
+ log.error("Failed to create new session for app with default user.",e);
+ }
+ return null;
}
public IClient getNewSessionIClient(ClientHandle applicationHandle,
UserHandle userId) {
- return clientInNewSession(userId, applicationHandle);
+ try {
+ return clientInNewSession(userId, applicationHandle, null);
+ } catch (Exception e) {
+ log.error("Failed to create new session for app and user.",e);
+ }
+ return null;
+ }
+ private void checkImportedDocument(File vamsasDocument) throws InvalidSessionDocumentException
+ {
+ if (!vamsasDocument.exists())
+ {
+ throw new InvalidSessionDocumentException("File "+vamsasDocument+" does not exist");
+ }
+ if (!vamsasDocument.canRead())
+ {
+ throw new InvalidSessionDocumentException("File "+vamsasDocument+" does not exist");
+ }
+ }
+ public IClient openAsNewSessionIClient(ClientHandle applicationHandle,
+ File vamsasDocument) throws InvalidSessionDocumentException {
+ checkImportedDocument(vamsasDocument);
+ try {
+ return clientInNewSession(null, applicationHandle, vamsasDocument);
+ } catch (InvalidSessionUrnException e)
+ {
+ throw new InvalidSessionDocumentException("Unexpected exception", e);
+ }
+ }
+
+ public IClient openAsNewSessionIClient(ClientHandle applicationHandle,
+ UserHandle userId, File vamsasDocument) throws InvalidSessionDocumentException {
+ checkImportedDocument(vamsasDocument);
+ try {
+ return clientInNewSession(userId, applicationHandle, vamsasDocument);
+ } catch (InvalidSessionUrnException e)
+ {
+ throw new InvalidSessionDocumentException("Unexpected exception", e);
+ }
}
}