1 package uk.ac.ebi.intenz.webapp.utilities;
2
3 import org.apache.log4j.Logger;
4
5 import java.util.Hashtable;
6 import java.util.Enumeration;
7
8
9
10
11
12
13
14
15
16
17
18
19 public class EntryLockSingleton {
20
21 private Logger LOGGER = Logger.getLogger(EntryLockSingleton.class.getName());
22
23
24
25
26 private static final EntryLockSingleton instance = new EntryLockSingleton();
27
28 private static Hashtable entriesInProcess;
29
30
31
32
33 protected EntryLockSingleton() {
34 entriesInProcess = new Hashtable();
35 }
36
37
38
39
40
41
42 public static EntryLockSingleton getInstance() {
43 return instance;
44 }
45
46
47
48
49
50
51
52
53
54
55
56 public synchronized boolean setLock(String enzymeId, String user) {
57 if (entriesInProcess.containsKey(enzymeId)) {
58 if (entriesInProcess.get(enzymeId).equals(user)) {
59 return true;
60 } else {
61 return false;
62 }
63 } else
64 entriesInProcess.put(enzymeId, user);
65
66 LOGGER.debug("The enzyme (ID: " + enzymeId + ") has been locked by user " + user);
67 return true;
68 }
69
70
71
72
73
74
75 public synchronized void releaseLock(String enzymeId) {
76 entriesInProcess.remove(enzymeId);
77 LOGGER.debug("The enzyme (ID: " + enzymeId + ") has been released.");
78 }
79
80
81
82
83
84
85
86
87
88 public synchronized void invalidateLocks(String user) {
89 LOGGER.debug("All locks are being invalidated.");
90 Enumeration keys = entriesInProcess.keys();
91 while (keys.hasMoreElements()) {
92 String key = (String) keys.nextElement();
93 String value = (String) entriesInProcess.get(key);
94 if (value.equals(user)) {
95 entriesInProcess.remove(key);
96 }
97 }
98 }
99 }