1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.concurrent;
16  
17  import java.util.Map;
18  import java.util.concurrent.ConcurrentHashMap;
19  import java.util.concurrent.locks.Lock;
20  import java.util.concurrent.locks.ReentrantLock;
21  
22  /**
23   * <a href="LockRegistry.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Shuyang Zhou
26   */
27  public class LockRegistry {
28  
29      public static Lock allocateLock(String groupName, String key) {
30          ConcurrentHashMap<String, Lock> lockGroup = _lockGroupMap.get(
31              groupName);
32  
33          if (lockGroup == null) {
34              lockGroup = new ConcurrentHashMap<String, Lock>();
35  
36              ConcurrentHashMap<String, Lock> oldLockGroup =
37                  _lockGroupMap.putIfAbsent(groupName, lockGroup);
38  
39              if (oldLockGroup != null) {
40                  lockGroup = oldLockGroup;
41              }
42          }
43  
44          Lock lock = lockGroup.get(key);
45  
46          if (lock == null) {
47              lock = new ReentrantLock();
48  
49              Lock oldLock = lockGroup.putIfAbsent(key, lock);
50  
51              if (oldLock != null) {
52                  lock = oldLock;
53              }
54          }
55  
56          return lock;
57      }
58  
59      public static void freeAllLock() {
60          freeAllLock(false);
61      }
62  
63      public static void freeAllLock(boolean unlock) {
64          if (unlock == true) {
65              for (Map<String, Lock> lockGroup : _lockGroupMap.values()) {
66                  for (Lock lock : lockGroup.values()) {
67                      lock.unlock();
68                  }
69              }
70          }
71  
72          _lockGroupMap.clear();
73      }
74  
75      public static Map<String, Lock> freeLock(String groupName) {
76          return freeLock(groupName, false);
77      }
78  
79      public static Map<String, Lock> freeLock(String groupName, boolean unlock) {
80          Map<String, Lock> lockGroup = _lockGroupMap.remove(groupName);
81  
82          if (lockGroup == null) {
83              return null;
84          }
85  
86          if (unlock == true) {
87              for (Lock lock : lockGroup.values()) {
88                  lock.unlock();
89              }
90          }
91  
92          return lockGroup;
93      }
94  
95      public static Lock freeLock(String groupName, String key) {
96          return freeLock(groupName, key, false);
97      }
98  
99      public static Lock freeLock(String groupName, String key, boolean unlock) {
100         Map<String, Lock> lockGroup = _lockGroupMap.get(groupName);
101 
102         if (lockGroup == null) {
103             return null;
104         }
105 
106         Lock lock = lockGroup.remove(key);
107 
108         if (lock == null) {
109             return null;
110         }
111 
112         if (unlock) {
113             lock.unlock();
114         }
115 
116         return lock;
117     }
118 
119     private static ConcurrentHashMap<String, ConcurrentHashMap<String, Lock>>
120         _lockGroupMap =
121             new ConcurrentHashMap<String, ConcurrentHashMap<String, Lock>>();
122 
123 }