1 package uk.ac.vamsas.client.simpleclient;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.RandomAccessFile;
7 import java.nio.channels.FileChannel;
8 import java.nio.channels.OverlappingFileLockException;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.log4j.FileAppender;
13 import org.apache.log4j.Logger;
14 import org.apache.log4j.PatternLayout;
16 import uk.ac.vamsas.client.ClientHandle;
17 import uk.ac.vamsas.client.Events;
18 import uk.ac.vamsas.client.IClient;
19 import uk.ac.vamsas.client.SessionHandle;
20 import uk.ac.vamsas.client.UserHandle;
23 * Does all the IO operations for a SimpleClient instance accessing
24 * a SimpleClient vamsas session.
26 * Basically, it defines the various standard names for the files
27 * in the session directory (that maps to the sessionUrn),
28 * provides constructors for the file handlers and watchers of
29 * those file entities, and some higher level methods
30 * to check and change the state flags for the session.
32 * TODO: move the stuff below to the SimpleClientFactory documentation.
33 * much may not be valid now :
34 * Vamsas client is intialised with a path to create live session directories.
35 * This path may contain a vamsas.properties file
36 * that sets additional parameters (otherwise client
37 * just uses the one on the classpath).
39 * A vamsas session consists of :
40 * SessionDir - translates to urn of a live session.
41 * Contains: Vamsas Document (as a jar), Session client list file,
42 * both of which may be locked, and additional
43 * temporary versions of these files when write
44 * operations are taking place.
47 * - vamsasdocument.xml : core info
49 * - <applicationname>.version.sessionnumber.raw (string given in vamsasdocument.xml applicationData entry)
52 * - filename given in the vamsasdocument.xml. Should be checked for validity by any client and rewritten if necessary.
53 * The lockfile can point to the jar itself.
55 * Initially - documentHandler either:
56 * - creates a zip for a new session for the client
57 * - connect to an existing session zip
58 * 1. reads session urn file
60 * 3. examines session - decide whether to create new application data slice or connect to one stored in session.
61 * 4. writes info into session file
62 * 5. releases lock and generates local client events.
63 * 6. Creates Watcher thread to generate events.
66 * - Update watcher checks for file change -
68 * Procedures for file based session message exchange
69 * - session document modification flag
73 public class VamsasSession {
75 * indicator file for informing other processes that
76 * they should finalise their vamsas datasets for
77 * storing into a vamsas archive.
79 public static final String CLOSEANDSAVE_FILE = "stored.log";
82 * session file storing the last_stored_stat data
84 public static final String MODIFIEDDOC_FILE = "modified";
86 private SimpleSessionManager sessionManager = null;
89 * Count of cycles before considering the current client as the last one of the session (if no other client registered as active )
91 private final int watchCycleCountBeforeLastClient = 1220;
94 * time between checking
96 public int WATCH_SLEEP = 30;
98 protected String clientFileDirectory = "clients";
101 * called to clear update flag after a successful offline storage event
103 protected void clearUnsavedFlag() {
104 SessionFlagFile laststored = new SessionFlagFile(new File(sessionDir,
106 if (!laststored.clearFlag())
107 log.warn("Unsaved flag was not cleared for " + sessionDir);
111 * called to indicate session document has been modified.
114 protected void setUnsavedFlag() {
115 SessionFlagFile laststored = new SessionFlagFile(new File(sessionDir,
117 if (!laststored.setFlag())
118 log.warn("Couldn't set the Unsaved flag for " + sessionDir);
123 * @return true if session document has been modified since last offline storage event
125 protected boolean getUnsavedFlag() {
126 SessionFlagFile laststored = new SessionFlagFile(new File(sessionDir,
128 return laststored.checkFlag();
134 public static final String SESSION_LOG = "Log.txt";
136 private static Log log = LogFactory.getLog(VamsasSession.class);
138 protected Logger slog = Logger.getLogger("uk.ac.vamsas.client.SessionLog");
140 * the appender that writes to the log file inside the session's directory.
142 private FileAppender slogAppender=null;
144 * setup the sessionLog using Log4j.
145 * @throws IOException
147 private void initLog() throws IOException {
148 // TODO: fix session event logging
149 // LATER: make dedicated appender format for session log.
150 /*Appender app = slog.getAppender("log4j.appender.SESSIONLOG");
151 // slog.addAppender(new FileAppender(app.getLayout(), new File(sessionDir, SESSION_LOG).getAbsolutePath()));
152 // slog.addAppender(new FileAppender(app.getLayout(), new File(sessionDir, SESSION_LOG).getAbsolutePath()));
153 for (Enumeration e = slog.getAllAppenders() ; e.hasMoreElements() ;) {
154 System.out.println(e.nextElement());
159 File sessionLogFile = new File(this.sessionDir, SESSION_LOG);
160 slog.addAppender(slogAppender = new FileAppender(new PatternLayout(
161 "%-4r [%t] %-5p %c %x - %m%n"), sessionLogFile.getAbsolutePath(),
164 log.info("No appender for SessionLog");
167 private void closeSessionLog() {
170 if (slogAppender!=null)
172 slog.removeAppender(slogAppender);
173 slogAppender.close();
180 * the sessionDir is given as the session location for new clients.
182 protected File sessionDir;
185 * holds the list of attached clients
189 public static final String CLIENT_LIST = "Clients.obj";
194 VamsasFile vamArchive;
196 public static final String VAMSAS_OBJ = "VamDoc.jar";
199 * sets up the vamsas session files and watchers in sessionDir
202 protected VamsasSession(File sessionDir1) throws IOException {
203 if (sessionDir1 == null)
204 throw new Error("Null directory for VamsasSession.");
205 if (sessionDir1.exists()) {
206 if (!sessionDir1.isDirectory() || !sessionDir1.canWrite()
207 || !sessionDir1.canRead())
208 throw new IOException("Cannot access '" + sessionDir1
209 + "' as a read/writable Directory.");
210 if (!checkSessionFiles(sessionDir1))
212 .warn("checkSessionFiles() returned false. Possible client implementation error");
213 this.sessionDir = sessionDir1;
214 initSessionObjects();
215 slog.debug("Initialising additional VamsasSession instance");
216 log.debug("Attached to VamsasSession in " + sessionDir1);
219 // start from scratch
220 if (!sessionDir1.mkdir())
221 throw new IOException("Failed to make VamsasSession directory in "
223 createSessionFiles();
224 initSessionObjects();
225 slog.debug("Session directory created.");
226 log.debug("Initialised VamsasSession in " + sessionDir1);
231 * tests presence of existing sessionfiles files in dir
235 private boolean checkSessionFiles(File dir) throws IOException {
236 File c_file = new File(dir, CLIENT_LIST);
237 File v_doc = new File(dir, VAMSAS_OBJ);
238 if (c_file.exists() && v_doc.exists())
244 * create new empty files in dir
247 private void createSessionFiles() throws IOException {
248 if (sessionDir == null)
249 throw new IOException(
250 "Invalid call to createSessionFiles() with null sessionDir");
251 File c_file = new File(sessionDir, CLIENT_LIST);
252 File v_doc = new File(sessionDir, VAMSAS_OBJ);
253 if (!c_file.exists() && c_file.createNewFile())
254 log.debug("Created new ClientFile " + c_file); // don't care if this works or not
255 if (!v_doc.exists() && v_doc.createNewFile())
256 log.debug("Created new Vamsas Session Document File " + v_doc);
260 * construct SessionFile objects and watchers for each
262 private void initSessionObjects() throws IOException {
263 createSessionFiles();
264 if (clist != null || vamArchive != null)
265 throw new IOException(
266 "initSessionObjects called for initialised VamsasSession object.");
267 clist = new ClientsFile(new File(sessionDir, CLIENT_LIST));
268 vamArchive = new VamsasFile(new File(sessionDir, VAMSAS_OBJ));
269 storedocfile = new ClientsFile(new File(sessionDir, CLOSEANDSAVE_FILE));
274 * make a new watcher object for the clientFile
275 * @return new ClientFile watcher instance
277 public FileWatcher getClientWatcher() {
278 return new FileWatcher(clist.sessionFile);
282 * make a new watcher object for the vamsas Document
283 * @return new ClientFile watcher instance
285 public FileWatcher getDocWatcher() {
286 return new FileWatcher(vamArchive.sessionFile);
289 FileWatcher store_doc_file = null;
291 public ClientsFile storedocfile = null;
294 * make a new watcher object for the messages file
295 * @return new watcher instance
297 public FileWatcher getStoreWatcher() {
298 return new FileWatcher(new File(sessionDir, CLOSEANDSAVE_FILE));
303 * write to the StoreWatcher file to indicate that a storeDocumentRequest has been made.
304 * The local client's storeWatcher FileWatcher object is updated so the initial change is not registered.
309 public void addStoreDocumentRequest(ClientHandle client, UserHandle user)
311 // TODO: replace this with clientsFile mechanism
312 SessionFile sfw = new SessionFile(new File(sessionDir, CLOSEANDSAVE_FILE));
313 while (!sfw.lockFile())
314 log.debug("Trying to get lock for " + CLOSEANDSAVE_FILE);
315 RandomAccessFile sfwfile = sfw.fileLock.getRaFile();
316 sfwfile.setLength(0); // wipe out any old info.
317 // TODO: rationalise what gets written to this file (ie do we want other clients to read the id of the requestor?)
318 sfwfile.writeUTF(client.getClientUrn() + ":" + user.getFullName() + "@"
319 + user.getOrganization());
321 if (store_doc_file != null)
322 store_doc_file.setState();
323 slog.info("FinalizeAppData request from " + user.getFullName() + " using "
324 + client.getClientUrn() + "");
328 * create a new session with an existing vamsas Document - by copying it into the session.
331 public void setVamsasDocument(File archive) throws IOException {
332 log.debug("Transferring vamsas data from " + archive + " to session:"
333 + vamArchive.sessionFile);
334 SessionFile xtantdoc = new SessionFile(archive);
335 vamArchive.updateFrom(null, xtantdoc);
336 // LATER: decide if session archive provenance should be updated to reflect access.
337 // TODO: soon! do a proper import objects from external file
338 log.debug("Transfer complete.");
342 * write session as a new vamsas Document (this will overwrite any existing file without warning)
344 * TODO: verify that lock should be released for vamsas document.
347 protected void writeVamsasDocument(File destarchive, Lock extlock)
349 log.debug("Transferring vamsas data from " + vamArchive.sessionFile
350 + " to session:" + destarchive);
351 SessionFile newdoc = new SessionFile(destarchive);
352 if (extlock == null && !vamArchive.lockFile())
353 while (!vamArchive.lockFile())
354 log.info("Trying to get lock for " + vamArchive.sessionFile);
355 // TODO: LATER: decide if a provenance entry should be written in the exported document recording the export from the session
356 newdoc.updateFrom(extlock, vamArchive);
357 // LATER: LATER: fix use of updateFrom for file systems where locks cannot be made (because they don't have a lockManager, ie NFS/Unix, etc).
360 log.debug("Transfer complete.");
364 * extant archive IO handler
366 VamsasArchive _va = null;
369 * Creates a VamsasArchive Vobject for accessing and updating document
370 * Note: this will lock the Vamsas Document for exclusive access to the client.
371 * @return session vamsas document
372 * @throws IOException if locks fail or vamsas document read fails.
374 protected VamsasArchive getVamsasDocument() throws IOException {
375 // check we haven't already done this once - probably should be done by caller
378 // patiently wait for a lock on the document. (from ArchiveClient.getUpdateable())
380 while (vamArchive.getLock() == null && --tries > 0) {
382 log.debug("Trying to get a document lock for the " + tries + "'th time.");
385 throw new IOException("Failed to get lock for vamsas archive.");
387 VamsasArchive va = new VamsasArchive(vamArchive.sessionFile, false, true,
394 * Unlocks the vamsas archive session document after it has been closed.
395 * @throws IOException
397 protected void unlockVamsasDocument() throws IOException {
401 if (vamArchive != null)
407 * create a uniquely named uk.ac.vamsas.client.simpleclient.ClientsFile.addClient(ClientHandle)ile in the session Directory
408 * @see java.io.File.createTempFile
409 * @param pref Prefix for name
410 * @param suff Suffix for name
411 * @return SessionFile object configured for the new file (of length zero)
412 * @throws IOException
414 protected SessionFile getTempSessionFile(String pref, String suff)
416 File tfile = File.createTempFile(pref, suff, sessionDir);
417 SessionFile tempFile = new SessionFile(tfile);
422 * add a IClient to the session
424 * add the client to the client list file
425 * @param client client to add to the session
427 protected void addClient(SimpleClient client) {
429 slog.error("Try to add a null client to the session ");
431 log.debug("Adding client " + client.getClientHandle().getClientUrn());
432 getClientWatcherElement().haltWatch();
433 clist.addClient(client.getClientHandle());
436 log.debug("Register Client as Active.");
438 client.createActiveClientFile();
439 } catch (IOException e) {
440 log.debug("Error during active client file creation.");
442 //tracks modification to the client list and readds client to the list
443 getClientWatcherElement().setHandler(new AddClientWatchCallBack(client));
444 getClientWatcherElement().enableWatch();
450 * Handler for the client watcher.
452 * If (the current client is not in the client list, it is added again;)
454 private class AddClientWatchCallBack implements WatcherCallBack {
456 private SimpleClient client;
459 *Inits the handler with the client to check in the list
460 * @param client client to monitor in the client list
462 protected AddClientWatchCallBack(SimpleClient client) {
463 this.client = client;
467 * If the client list is modified, checks if the current is still in the list. otherwise, readds ti.
468 * @return true to enable watcher, or false to disable it in future WatcherThread cycles.
470 public boolean handleWatchEvent(WatcherElement watcher, Lock lock) {
471 boolean isWatchEnable = watcher.isWatchEnabled();
472 if (lock == null)//no update on the list
473 return isWatchEnable;
474 log.debug("change on the client list ");
475 if (client != null) {
477 //checks if the client is not already in the lists
478 ClientHandle[] cl = clist.retrieveClientList(lock);//clist.retrieveClientList();
479 boolean found = false;
481 for (int chi = cl.length - 1; !found && chi > -1; chi--) {
482 found = cl[chi].equals(this.client.getClientHandle());
487 log.debug("client not in the list ");
488 if (log.isDebugEnabled())
490 .debug("the client has not been found in the list. Adding it again :"
494 log.debug("client is in the list");
497 log.debug("isWatchEnable " + isWatchEnable);
498 return isWatchEnable;
504 * removes a client from the current session
505 * removes the client from the session client list
506 * if the client is the last one from the session (ClientList), the current session is removed
507 * from active session list.
509 * The active should add them self to the client list. To insure to close the session,when the current client is the lact active client,
510 * clears the list of clients and when two cycles to insure there is no more active client, that otherwise would have readd themself to the list
512 * @param client client to remove
514 protected void removeClient(SimpleClient client)//IClient client)
516 if (client == null) {
517 log.error("Null client passed to removeClient");
520 //ClientSessionFileWatcherElement cwe=getClientWatcherElement();
521 //if (cwe!=null && cwe.isWatchEnabled()) {
524 //set handler to check is the the last active client of the session
525 //Wait for several watchers cycle to see if the current client was the last client active in the session.
526 //if yes, close the session
528 // getClientWatcherElement().setHandler(new RemoveClientWatchCallBack (client));
529 // getClientWatcherElement().setTimeoutBeforeLastCycle(this.watchCycleCountBeforeLastClient);
530 log.info("remove client from list");
531 if (clistWatchElement!=null)
533 clistWatchElement.haltWatch();
534 clistWatchElement.watched.unlockFile();
537 //clist.unlockFile();
538 log.info("list cleared");
540 // cwe.enableWatch();
542 log.debug("Stopping EventGenerator..");
543 client.evgen.stopWatching();
544 // cwe.setHandler(null);
545 // ask to the client to copy application data into the document
546 client.evgen._raise(Events.DOCUMENT_FINALIZEAPPDATA, null, client, null);
547 boolean closeSession = isLastActiveClient(client);
549 log.debug("Raising request-to-save event");
550 client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client, null);
551 client.evgen._raise(Events.SESSION_SHUTDOWN, null, client
552 .getSessionHandle(), null);
555 client.evgen.stopWatching();
557 log.debug("Attempting to release active client locks");
558 client.releaseActiveClientFile();
559 } catch (IOException e) {
560 log.error("error during active file client release");
564 log.debug("Last active client: closing session");
565 log.info("Closing session");
566 getSessionManager().removeSession(client.getSessionHandle());
571 * close every file and stop.
573 private void tidyUp() {
577 storedocfile.unlockFile();
582 private boolean isLastActiveClient(SimpleClient client) {
583 log.debug("Testing if current client is the last one.");
585 .debug("current client lockfile is '" + client.getClientlockFile()
587 boolean noOtherActiveClient = true;
588 //create, if need, subdirectory to contain client files
589 File clientlockFileDir = new File(this.sessionDir, clientFileDirectory);
590 if (!clientlockFileDir.exists()) {
592 .error("Something wrong the active client file does not exits... should not happen");
598 //no check every file in the directory and try to get lock on it.
599 File[] clientFiles = clientlockFileDir.listFiles();
600 if (clientFiles == null || clientFiles.length == 0) {//there is not file on the directory. the current client should be the last one.
604 for (int i = clientFiles.length - 1; i > -1 && noOtherActiveClient; i--) {
605 File clientFile = clientFiles[i];
606 log.debug("testing file for lock: " + clientFile.getAbsolutePath());
607 if (client.getClientLock().isTargetLockFile(clientFile)) {
608 log.debug("current client file found");
611 if (clientFile != null && clientFile.exists()) {
613 log.debug("Try to acquire a lock on the file");
614 // Get a file channel for the file
615 FileChannel channel = new RandomAccessFile(clientFile, "rw")
618 // Use the file channel to create a lock on the file.
619 // This method blocks until it can retrieve the lock.
620 // java.nio.channels.FileLock activeClientFilelock = channel.lock();
622 // Try acquiring the lock without blocking. This method returns
623 // null or throws an exception if the file is already locked.
625 java.nio.channels.FileLock activeClientFilelock = channel
628 //the lock has been acquired.
629 //the file was not lock and so the corresponding application seems to have die
630 if (activeClientFilelock != null) {
632 .debug("lock obtained : file must be from a crashed application");
634 activeClientFilelock.release();
635 log.debug("lock released");
638 log.debug("channel closed");
642 log.debug("crashed application file deleted");
645 noOtherActiveClient = false;
646 log.debug("lock not obtained : another application is active");
648 } catch (OverlappingFileLockException e) {
649 // File is already locked in this thread or virtual machine
650 //that the expected behaviour
651 log.debug("lock not accessible ", e);
653 } catch (Exception e) {
654 log.debug("error during lock testing ", e);
659 } catch (Exception e) {
660 log.error("error during counting active clients");
662 return noOtherActiveClient;
666 * Handler for the client watcher. after a client have been removed
668 * Checks if the client is not the last active one.
670 * If (the current client is not in the client list readd it;)
672 private class RemoveClientWatchCallBack implements WatcherCallBack {
674 private SimpleClient client;
676 private boolean manualCheckOfClientCount = false;
679 *Inits the handler with the client to check in the list
680 * @param client client to monitor in the client list
682 protected RemoveClientWatchCallBack(SimpleClient client) {
683 this.client = client;
687 * If the client list is modified, checks if the current is still in the list. otherwise, readds ti.
688 * @return true to enable watcher, or false to disable it in future WatcherThread cycles.
690 public boolean handleWatchEvent(WatcherElement watcher, Lock lock) {
691 // if lock is null, no client has been added since last, clear.
692 //the client is then the last client
693 if (client != null) {
697 //checks if the client is not already in the lists
698 // ClientHandle[] cl = clist.retrieveClientList();//lock);//clist.retrieveClientList();
700 boolean islastClient = true;
701 if (manualCheckOfClientCount) {
702 log.debug("manual checking of count of client");
703 //checks if the client is not already in the lists
704 ClientHandle[] cl = clist.retrieveClientList();//lock);//clist.retrieveClientList();
705 if (cl == null || cl.length < 1)
706 // {//no client has registered as active
709 log.debug("list is empty");
711 islastClient = false;
712 log.debug("list is not empty");
714 // if(cl == null || cl.length<1 )
715 // {//no client has registered as active
717 //the client is the last one, so close current session
718 log.info("last client removed: closing session");
719 closeSession(client);
722 log.debug("not the last client found ");
723 // ask to the client to cpoy application data into the document
724 // client.evgen._raise(Events.DOCUMENT_FINALIZEAPPDATA, null, client,null);
729 log.debug("Stopping EventGenerator..");
730 // TODO: ensure ClientsFile lock is really released!! clist.unlockFile();
731 client.evgen.stopWatching();
733 watcher.setHandler(null);//Do not check if the client is the last client. watcher will shutdown anyway
734 // watcher.haltWatch();
741 * closes the current session,
742 * and send an event to the last client to close the document
743 * @param client the last client of the client
745 private void closeSession(SimpleClient client) {
747 client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client, null);
748 log.debug("close document request done");
749 closeSession(client.getSessionHandle());
753 * CLoses the current session
754 * @param sessionHandle sessionHandle of the session to remove
756 private void closeSession(SessionHandle sessionHandle) {
757 getSessionManager().removeSession(sessionHandle);
758 log.debug("Session removed");
762 * @return the sessionManager
764 protected SimpleSessionManager getSessionManager() {
765 return sessionManager;
769 * @param sessionManager the sessionManager to set
771 protected void setSessionManager(SimpleSessionManager sessionManager) {
772 this.sessionManager = sessionManager;
775 public ClientsFile getStoreDocFile() {
776 if (storedocfile == null) {
782 ClientSessionFileWatcherElement clistWatchElement = null;
785 * get or create a watcher on clist.
786 * @return the contents of clistWatchElement or initialise it
788 public ClientSessionFileWatcherElement getClientWatcherElement() {
789 if (clistWatchElement == null) {
790 clistWatchElement = new ClientSessionFileWatcherElement(clist, null);
792 return clistWatchElement;
795 * writes a vector of vorba Ids to the session.
797 public void setModObjectList(Vector modObjects) {
798 log.debug("Writing "+modObjects.size()+" ids to ModObjectList");
799 // TODO Auto-generated method stub
802 * get current list of modified objects.
803 * @return null or Vector of objects
805 public Vector getModObjectList() {
806 log.debug("Reading modObjectList");