View Javadoc

1   package com.atlassian.bonnie;
2   
3   import java.util.concurrent.locks.ReentrantLock;
4   
5   import com.atlassian.util.profiling.UtilTimerStack;
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   /**
10   * Adds logging and profiling to lock and unlock methods.
11   */
12  class LoggingReentrantLock extends ReentrantLock
13  {
14      private final static Logger log = LoggerFactory.getLogger(LoggingReentrantLock.class);
15      
16      private final String name;
17  
18      /**
19       * @param name the name of the lock, used for logging and a profiling key
20       */
21      public LoggingReentrantLock(String name)
22      {
23          this.name = name;
24      }
25  
26      @Override
27      public void lock()
28      {
29          if (log.isDebugEnabled())
30              log.debug("Acquiring lock: " + name);
31          UtilTimerStack.push(getClass().getName() + "." + name);
32          super.lock();
33      }
34  
35      @Override
36      public void unlock()
37      {
38          if (log.isDebugEnabled())
39              log.debug("Releasing lock: " + name);
40          super.unlock();
41          UtilTimerStack.pop(getClass().getName() + "." + name);
42      }
43  }