d8098d77f686e595645a1ecabb339269b3475a6c
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / LockFactory.java
1 package uk.ac.vamsas.client.simpleclient;
2
3 import java.io.File;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 public class LockFactory {
9   protected static Log log = LogFactory.getLog(LockFactory.class);
10   public static int locktype=0; // use file lock by default
11   public static String[] locktypes = {"file","native"};
12   {
13     String lockt = System.getProperty("vamsas.locktype");
14     if (lockt!=null) {
15       int i,j;
16       for (i=0, j=locktypes.length; i<j && locktypes[i].equalsIgnoreCase(lockt); i++)
17         ;
18       if (i>=j) {
19         String lt = "'"+locktypes[0]+"'";
20         for (i=1; i<j; i++)
21           lt += ",'"+locktypes[i]+"'";
22         log.warn("System property vamsas.locktype takes one of "+lt);
23         log.warn("Defaulting to Locktype of "+locktypes[locktype]);
24       }
25     } else
26       log.debug("Defaulting to Locktype of "+locktypes[locktype]);
27   }
28   /**
29    * lock target (blocks until lock is obtained)
30    * @param target
31    * @return lock
32    */
33   public static Lock getLock(java.io.File target) {
34     return getLock(target, true);
35   }
36   public static Lock getLock(java.io.File target, boolean block) { 
37     if (locktype==0)
38       return new FileLock(target, block);
39     if (locktype==1)
40       return new NativeLock(target, block);
41     log.fatal("Implementation Error! No valid Locktype value");
42     return null;
43   }
44   /**
45    * try to lock target 
46    * @param target
47    * @return null if lock was not possible
48    */
49   public static Lock tryLock(File target) {
50     return getLock(target, false);
51   }
52 }