refactored org to uk
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / LockFactory.java
diff --git a/src/uk/ac/vamsas/client/simpleclient/LockFactory.java b/src/uk/ac/vamsas/client/simpleclient/LockFactory.java
new file mode 100644 (file)
index 0000000..d8098d7
--- /dev/null
@@ -0,0 +1,52 @@
+package uk.ac.vamsas.client.simpleclient;
+
+import java.io.File;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class LockFactory {
+  protected static Log log = LogFactory.getLog(LockFactory.class);
+  public static int locktype=0; // use file lock by default
+  public static String[] locktypes = {"file","native"};
+  {
+    String lockt = System.getProperty("vamsas.locktype");
+    if (lockt!=null) {
+      int i,j;
+      for (i=0, j=locktypes.length; i<j && locktypes[i].equalsIgnoreCase(lockt); i++)
+        ;
+      if (i>=j) {
+        String lt = "'"+locktypes[0]+"'";
+        for (i=1; i<j; i++)
+          lt += ",'"+locktypes[i]+"'";
+        log.warn("System property vamsas.locktype takes one of "+lt);
+        log.warn("Defaulting to Locktype of "+locktypes[locktype]);
+      }
+    } else
+      log.debug("Defaulting to Locktype of "+locktypes[locktype]);
+  }
+  /**
+   * lock target (blocks until lock is obtained)
+   * @param target
+   * @return lock
+   */
+  public static Lock getLock(java.io.File target) {
+    return getLock(target, true);
+  }
+  public static Lock getLock(java.io.File target, boolean block) { 
+    if (locktype==0)
+      return new FileLock(target, block);
+    if (locktype==1)
+      return new NativeLock(target, block);
+    log.fatal("Implementation Error! No valid Locktype value");
+    return null;
+  }
+  /**
+   * try to lock target 
+   * @param target
+   * @return null if lock was not possible
+   */
+  public static Lock tryLock(File target) {
+    return getLock(target, false);
+  }
+}