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.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    
021    import java.util.Date;
022    
023    /**
024     * @author Zsolt Berentey
025     */
026    public class LockProtectedAction<T> {
027    
028            public LockProtectedAction(
029                    Class<?> clazz, String lockKey, long timeout, long retryDelay) {
030    
031                    _className = clazz.getName();
032                    _lockKey = lockKey;
033                    _timeout = timeout;
034                    _retryDelay = retryDelay;
035            }
036    
037            public T getReturnValue() {
038                    return _returnValue;
039            }
040    
041            public void performAction() throws PortalException {
042                    Lock lock = null;
043    
044                    while (true) {
045                            try {
046                                    lock = LockManagerUtil.lock(_className, _lockKey, _lockKey);
047                            }
048                            catch (Exception e) {
049                                    if (_log.isWarnEnabled()) {
050                                            _log.warn("Unable to acquire lock. Retrying.");
051                                    }
052    
053                                    continue;
054                            }
055    
056                            if (lock.isNew()) {
057                                    try {
058                                            _returnValue = performProtectedAction();
059                                    }
060                                    finally {
061                                            LockManagerUtil.unlock(_className, _lockKey, _lockKey);
062                                    }
063    
064                                    break;
065                            }
066    
067                            Date createDate = lock.getCreateDate();
068    
069                            if ((System.currentTimeMillis() - createDate.getTime()) >=
070                                            _timeout) {
071    
072                                    LockManagerUtil.unlock(_className, _lockKey, lock.getOwner());
073    
074                                    if (_log.isWarnEnabled()) {
075                                            _log.warn("Removed lock " + lock + " due to timeout");
076                                    }
077                            }
078                            else {
079                                    try {
080                                            Thread.sleep(_retryDelay);
081                                    }
082                                    catch (InterruptedException ie) {
083                                            if (_log.isWarnEnabled()) {
084                                                    _log.warn(
085                                                            "Interrupted while waiting to reacquire lock", ie);
086                                            }
087                                    }
088                            }
089                    }
090            }
091    
092            @SuppressWarnings("unused")
093            protected T performProtectedAction() throws PortalException {
094                    return null;
095            }
096    
097            private static final Log _log = LogFactoryUtil.getLog(
098                    LockProtectedAction.class);
099    
100            private final String _className;
101            private final String _lockKey;
102            private final long _retryDelay;
103            private T _returnValue;
104            private final long _timeout;
105    
106    }