001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.lock;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.registry.Registry;
019    import com.liferay.registry.RegistryUtil;
020    import com.liferay.registry.ServiceTracker;
021    
022    /**
023     * @author Tina Tian
024     */
025    public class LockManagerUtil {
026    
027            public static void clear() {
028                    _instance._clear();
029            }
030    
031            public static Lock createLock(
032                    long lockId, long companyId, long userId, String userName) {
033    
034                    return _instance._createLock(lockId, companyId, userId, userName);
035            }
036    
037            public static Lock getLock(String className, long key)
038                    throws PortalException {
039    
040                    return _instance._getLock(className, key);
041            }
042    
043            public static Lock getLock(String className, String key)
044                    throws PortalException {
045    
046                    return _instance._getLock(className, key);
047            }
048    
049            public static Lock getLockByUuidAndCompanyId(String uuid, long companyId)
050                    throws PortalException {
051    
052                    return _instance._getLockByUuidAndCompanyId(uuid, companyId);
053            }
054    
055            public static boolean hasLock(long userId, String className, long key) {
056                    return _instance._hasLock(userId, className, key);
057            }
058    
059            public static boolean hasLock(long userId, String className, String key) {
060                    return _instance._hasLock(userId, className, key);
061            }
062    
063            public static boolean isLocked(String className, long key) {
064                    return _instance._isLocked(className, key);
065            }
066    
067            public static boolean isLocked(String className, String key) {
068                    return _instance._isLocked(className, key);
069            }
070    
071            public static Lock lock(
072                            long userId, String className, long key, String owner,
073                            boolean inheritable, long expirationTime)
074                    throws PortalException {
075    
076                    return _instance._lock(
077                            userId, className, key, owner, inheritable, expirationTime);
078            }
079    
080            public static Lock lock(
081                            long userId, String className, String key, String owner,
082                            boolean inheritable, long expirationTime)
083                    throws PortalException {
084    
085                    return _instance._lock(
086                            userId, className, key, owner, inheritable, expirationTime);
087            }
088    
089            public static Lock lock(String className, String key, String owner) {
090                    return _instance._lock(className, key, owner);
091            }
092    
093            public static Lock lock(
094                    String className, String key, String expectedOwner,
095                    String updatedOwner) {
096    
097                    return _instance._lock(className, key, expectedOwner, updatedOwner);
098            }
099    
100            public static Lock refresh(String uuid, long companyId, long expirationTime)
101                    throws PortalException {
102    
103                    return _instance._refresh(uuid, companyId, expirationTime);
104            }
105    
106            public static void unlock(String className, long key) {
107                    _instance._unlock(className, key);
108            }
109    
110            public static void unlock(String className, String key) {
111                    _instance._unlock(className, key);
112            }
113    
114            public static void unlock(String className, String key, String owner) {
115                    _instance._unlock(className, key, owner);
116            }
117    
118            private LockManagerUtil() {
119                    Registry registry = RegistryUtil.getRegistry();
120    
121                    ServiceTracker<LockManager, LockManager> serviceTracker =
122                            registry.trackServices(LockManager.class);
123    
124                    serviceTracker.open();
125    
126                    try {
127                            _lockHelper = serviceTracker.waitForService(0);
128                    }
129                    catch (Exception e) {
130                            throw new IllegalStateException(
131                                    "Unable to initialize lock manager util", e);
132                    }
133                    finally {
134                            serviceTracker.close();
135                    }
136            }
137    
138            private void _clear() {
139                    _lockHelper.clear();
140            }
141    
142            private Lock _createLock(
143                    long lockId, long companyId, long userId, String userName) {
144    
145                    return _lockHelper.createLock(lockId, companyId, userId, userName);
146            }
147    
148            private Lock _getLock(String className, long key) throws PortalException {
149                    return _lockHelper.getLock(className, key);
150            }
151    
152            private Lock _getLock(String className, String key) throws PortalException {
153                    return _lockHelper.getLock(className, key);
154            }
155    
156            private Lock _getLockByUuidAndCompanyId(String uuid, long companyId)
157                    throws PortalException {
158    
159                    return _lockHelper.getLockByUuidAndCompanyId(uuid, companyId);
160            }
161    
162            private boolean _hasLock(long userId, String className, long key) {
163                    return _lockHelper.hasLock(userId, className, key);
164            }
165    
166            private boolean _hasLock(long userId, String className, String key) {
167                    return _lockHelper.hasLock(userId, className, key);
168            }
169    
170            private boolean _isLocked(String className, long key) {
171                    return _lockHelper.isLocked(className, key);
172            }
173    
174            private boolean _isLocked(String className, String key) {
175                    return _lockHelper.isLocked(className, key);
176            }
177    
178            private Lock _lock(
179                            long userId, String className, long key, String owner,
180                            boolean inheritable, long expirationTime)
181                    throws PortalException {
182    
183                    return _lockHelper.lock(
184                            userId, className, key, owner, inheritable, expirationTime);
185            }
186    
187            private Lock _lock(
188                            long userId, String className, String key, String owner,
189                            boolean inheritable, long expirationTime)
190                    throws PortalException {
191    
192                    return _lockHelper.lock(
193                            userId, className, key, owner, inheritable, expirationTime);
194            }
195    
196            private Lock _lock(String className, String key, String owner) {
197                    return _lockHelper.lock(className, key, owner);
198            }
199    
200            private Lock _lock(
201                    String className, String key, String expectedOwner,
202                    String updatedOwner) {
203    
204                    return _lockHelper.lock(className, key, expectedOwner, updatedOwner);
205            }
206    
207            private Lock _refresh(String uuid, long companyId, long expirationTime)
208                    throws PortalException {
209    
210                    return _lockHelper.refresh(uuid, companyId, expirationTime);
211            }
212    
213            private void _unlock(String className, long key) {
214                    _lockHelper.unlock(className, key);
215            }
216    
217            private void _unlock(String className, String key) {
218                    _lockHelper.unlock(className, key);
219            }
220    
221            private void _unlock(String className, String key, String owner) {
222                    _lockHelper.unlock(className, key, owner);
223            }
224    
225            private static final LockManagerUtil _instance = new LockManagerUtil();
226    
227            private final LockManager _lockHelper;
228    
229    }