+ // locate session from Urn
+ // check Uniqueness of user + ClientHandle in the session. Update
+ // clientHandle urn accordingly.
+ // wait for lock, attach to session
+ // create client instance
+ IClient client = null;
+
+ // TODO: implement 'opening stored session' opening mechanism
+ // 1. existing session document URL is vdoc://... ?
+ // 2. check for sessionUrn being of this form.
+ // 3. if it is - locate the file and pass to new VamsasSession
+
+ try {
+ File sessionDirectory = this.convertSessionUrnToFile(sessionUrn);
+ // create session
+ log
+ .debug("found session directory "
+ + sessionDirectory.getAbsolutePath());
+ VamsasSession vamsasSession = new VamsasSession(sessionDirectory);
+
+ /*
+ * 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,
+ * applicationHandle, vamsasSession);
+ */
+ 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;
+ } catch (IOException e) {
+ log.error("error while creating new IClient: file access error", 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, File vamsasDocument) throws IOException, InvalidSessionUrnException, InvalidSessionDocumentException {
+ IClient client = null;
+ // create session
+ 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()));
+ if (userId == null) {
+ // create a default userHandle
+ // userId = new UserHandle(System.getProperty("user.name",
+ // System.getProperty("USERNAME","Joe Doe")),
+ // System.getProperty("host.name",System.getProperty("HOSTNAME",
+ // "Unknown") ));// clientName, clientVersion, sessionPath);
+ userId = new UserHandle(null, null);
+ }
+
+ // FullName and organisation should not be null (otherwise UserHandle equals
+ // method raises an java.lang.NullPointerException )
+ // use current OS user and hostname, if null
+ if (userId.getFullName() == null) {
+ userId.setFullName(System.getProperty("user.name", System.getProperty(
+ "USERNAME", "Joe Doe")));
+ }
+
+ if (userId.getOrganization() == null) {
+ userId.setOrganization(System.getProperty("host.name", System
+ .getProperty("HOSTNAME", "Unknown")));
+ }
+
+ if (clientHandle == null)
+ clientHandle = new ClientHandle("SimpleVamsasClientApp", "0.1");
+ else {
+ if (clientHandle.getClientName() == null) {
+ clientHandle.setClientName("SimpleVamsasClientApp");
+ }
+ if (clientHandle.getVersion() == null) {
+ clientHandle.setVersion("0.1");
+ }
+ }
+
+ // create simple client
+ client = new SimpleClient(userId, clientHandle, vamsasSession);
+ vamsasSession.addClient((SimpleClient) client);
+ vamsasSession.setSessionManager(this.getSessionManager());
+ return client;