applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / LockFactory.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 import java.io.File;\r
25 \r
26 import org.apache.commons.logging.Log;\r
27 import org.apache.commons.logging.LogFactory;\r
28 \r
29 public class LockFactory {\r
30   protected static Log log = LogFactory.getLog(LockFactory.class);\r
31 \r
32   public static int locktype = 0; // use file lock by default\r
33 \r
34   public static String[] locktypes = { "file", "native" };\r
35   {\r
36     String lockt = System.getProperty("vamsas.locktype");\r
37     if (lockt != null) {\r
38       int i, j;\r
39       for (i = 0, j = locktypes.length; i < j\r
40           && locktypes[i].equalsIgnoreCase(lockt); i++)\r
41         ;\r
42       if (i >= j) {\r
43         String lt = "'" + locktypes[0] + "'";\r
44         for (i = 1; i < j; i++)\r
45           lt += ",'" + locktypes[i] + "'";\r
46         log.warn("System property vamsas.locktype takes one of " + lt);\r
47         log.warn("Defaulting to Locktype of " + locktypes[locktype]);\r
48       }\r
49     } else\r
50       log.debug("Defaulting to Locktype of " + locktypes[locktype]);\r
51   }\r
52 \r
53   /**\r
54    * lock target (blocks until lock is obtained)\r
55    * \r
56    * @param target\r
57    * @return lock\r
58    */\r
59   public static Lock getLock(java.io.File target) {\r
60     return getLock(target, true);\r
61   }\r
62 \r
63   public static Lock getLock(java.io.File target, boolean block) {\r
64     if (locktype == 0)\r
65       return new FileLock(target, block);\r
66     if (locktype == 1)\r
67       return new NativeLock(target, block);\r
68     log.fatal("Implementation Error! No valid Locktype value");\r
69     return null;\r
70   }\r
71 \r
72   /**\r
73    * try to lock target\r
74    * \r
75    * @param target\r
76    * @return null if lock was not possible\r
77    */\r
78   public static Lock tryLock(File target) {\r
79     return getLock(target, false);\r
80   }\r
81 }\r