From 65a8158d49f3c25e78deba6ddd2f4a83fc695821 Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 16 Mar 2006 12:16:43 +0000 Subject: [PATCH] added simple semaphore file mechanism for session directory. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@183 be28352e-c001-0410-b1a7-c7978e42abec --- .../client/simpleclient/SessionFlagFile.java | 85 ++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/org/vamsas/client/simpleclient/SessionFlagFile.java diff --git a/src/org/vamsas/client/simpleclient/SessionFlagFile.java b/src/org/vamsas/client/simpleclient/SessionFlagFile.java new file mode 100644 index 0000000..7826fbc --- /dev/null +++ b/src/org/vamsas/client/simpleclient/SessionFlagFile.java @@ -0,0 +1,85 @@ +package org.vamsas.client.simpleclient; + +import java.io.File; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * methods for setting and checking + * binary flags in a vamsas session directory. + * all methods apart from the constructor will + * throw a fatal error if the flagFile is not + * a valid java.io.File object. + * LATER: extract SessionFlag interface for generalizing the vamsas session code + * @author jimp + * + */ +public class SessionFlagFile { + private static Log log = LogFactory.getLog(SessionFlagFile.class); + protected File flagFile=null; + private void checkFlagFile() { + if (flagFile==null) { + log.fatal("Implementation error - uninitialized SessionFlagFile", + new Error("Implementation error - uninitialized SessionFlagFile")); + } + } + /** + * will log a warning if exceptions occur during flag creation. + * @return true if flag was set successfully + */ + public boolean setFlag() { + checkFlagFile(); + try { + if (flagFile.createNewFile()) { + log.debug("Set session flag "+flagFile); + } else { + log.debug("Session flag already set "+flagFile); + } + return true; + } + catch (Exception e) { + log.warn("Couldn't set session flag "+flagFile, e); + } + return false; + } + /** + * + * @return true if flag was cleared successfully + */ + public boolean clearFlag() { + checkFlagFile(); + if (flagFile.exists()) { + log.debug("clearing session flag "+flagFile); + if (!flagFile.delete()) { + log.warn("failed to clear session flag "+flagFile); + return false; + } + } else { + log.debug("clearFlag called for already cleared flag "+flagFile); + } + return true; + } + /** + * + * @return state of session flag + */ + public boolean checkFlag() { + checkFlagFile(); + if (flagFile.exists()) { + if (log.isDebugEnabled()) + log.debug("Flag '"+flagFile+"' is set."); + return true; + } + if (log.isDebugEnabled()) + log.debug("Flag '"+flagFile+"' is not set."); + return false; + } + /** + * @param flagFile + */ + public SessionFlagFile(File flagFile) { + super(); + this.flagFile = flagFile; + }; +} -- 1.7.10.2