View Javadoc

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    * This class is responsible for locking entries in process.
10   * <p/>
11   * This will be guaranteed by a synchronized Hashtable which stores the enzyme id of the entry
12   * and the users name.
13   * If a user locks an entry but quits the application without finishing its modifications, the
14   * ModificationBindingListener will take care of releasing the lock.
15   *
16   * @author Michael Darsow
17   * @version $Revision: 1.2 $ $Date: 2008/01/28 12:33:09 $
18   */
19  public class EntryLockSingleton {
20  
21    private Logger LOGGER = Logger.getLogger(EntryLockSingleton.class.getName());
22  
23    /**
24     * Creates a single statically availble instance of this class, which cannot be redefined.
25     */
26    private static final EntryLockSingleton instance = new EntryLockSingleton();
27  
28    private static Hashtable entriesInProcess;
29  
30    /**
31     * Initialises the Hashtable for storing enzyme ids of currently modified entries.
32     */
33    protected EntryLockSingleton() {
34      entriesInProcess = new Hashtable();
35    }
36  
37    /**
38     * Returns the EntryLockSingleton instance.
39     *
40     * @return the one and only EntryLockSingleton instance.
41     */
42    public static EntryLockSingleton getInstance() {
43      return instance;
44    }
45  
46    /**
47     * Sets a lock for an entry in process.
48     * <p/>
49     * This method is synchronized to ensure, that only one user at the time can set a lock, hence only one
50     * user is able to modify one and the same entry.
51     *
52     * @param enzymeId The enzyme id of the entry to be modified.
53     * @param user     The user who is modifying the entry.
54     * @return <tt>true</tt>, if no-one else is modifying the entry.
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     * Removes a lock from an entry.
72     *
73     * @param enzymeId The enzyme id of the entry to be released.
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     * Removes all locks of entries modified by one and the same user.
82     * <p/>
83     * This (and the EntryLockListener) ensures that even if the user does not finish a modification
84     * and closes the browser, that all locked entries will be released.
85     *
86     * @param user The user name.
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  }