001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.concurrent;
016    
017    import java.util.Map;
018    import java.util.concurrent.ConcurrentHashMap;
019    import java.util.concurrent.locks.Lock;
020    import java.util.concurrent.locks.ReentrantLock;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class LockRegistry {
026    
027            public static Lock allocateLock(String groupName, String key) {
028                    ConcurrentHashMap<String, Lock> lockGroup = _lockGroupMap.get(
029                            groupName);
030    
031                    if (lockGroup == null) {
032                            lockGroup = new ConcurrentHashMap<String, Lock>();
033    
034                            ConcurrentHashMap<String, Lock> oldLockGroup =
035                                    _lockGroupMap.putIfAbsent(groupName, lockGroup);
036    
037                            if (oldLockGroup != null) {
038                                    lockGroup = oldLockGroup;
039                            }
040                    }
041    
042                    Lock lock = lockGroup.get(key);
043    
044                    if (lock == null) {
045                            lock = new ReentrantLock();
046    
047                            Lock oldLock = lockGroup.putIfAbsent(key, lock);
048    
049                            if (oldLock != null) {
050                                    lock = oldLock;
051                            }
052                    }
053    
054                    return lock;
055            }
056    
057            public static void freeAllLock() {
058                    freeAllLock(false);
059            }
060    
061            public static void freeAllLock(boolean unlock) {
062                    if (unlock == true) {
063                            for (Map<String, Lock> lockGroup : _lockGroupMap.values()) {
064                                    for (Lock lock : lockGroup.values()) {
065                                            lock.unlock();
066                                    }
067                            }
068                    }
069    
070                    _lockGroupMap.clear();
071            }
072    
073            public static Map<String, Lock> freeLock(String groupName) {
074                    return freeLock(groupName, false);
075            }
076    
077            public static Map<String, Lock> freeLock(String groupName, boolean unlock) {
078                    Map<String, Lock> lockGroup = _lockGroupMap.remove(groupName);
079    
080                    if (lockGroup == null) {
081                            return null;
082                    }
083    
084                    if (unlock == true) {
085                            for (Lock lock : lockGroup.values()) {
086                                    lock.unlock();
087                            }
088                    }
089    
090                    return lockGroup;
091            }
092    
093            public static Lock freeLock(String groupName, String key) {
094                    return freeLock(groupName, key, false);
095            }
096    
097            public static Lock freeLock(String groupName, String key, boolean unlock) {
098                    Map<String, Lock> lockGroup = _lockGroupMap.get(groupName);
099    
100                    if (lockGroup == null) {
101                            return null;
102                    }
103    
104                    Lock lock = lockGroup.remove(key);
105    
106                    if (lock == null) {
107                            return null;
108                    }
109    
110                    if (unlock) {
111                            lock.unlock();
112                    }
113    
114                    return lock;
115            }
116    
117            private static ConcurrentHashMap<String, ConcurrentHashMap<String, Lock>>
118                    _lockGroupMap =
119                            new ConcurrentHashMap<String, ConcurrentHashMap<String, Lock>>();
120    
121    }