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 = LockManagerUtil.lock(_className, _lockKey, _lockKey);
043    
044                    if (lock.isNew()) {
045                            try {
046                                    _returnValue = performProtectedAction();
047                            }
048                            finally {
049                                    LockManagerUtil.unlock(_className, _lockKey, _lockKey);
050                            }
051    
052                            return;
053                    }
054    
055                    Date createDate = lock.getCreateDate();
056    
057                    if ((System.currentTimeMillis() - createDate.getTime()) >= _timeout) {
058                            LockManagerUtil.unlock(_className, _lockKey, lock.getOwner());
059    
060                            if (_log.isWarnEnabled()) {
061                                    _log.warn("Removed lock " + lock + " due to timeout");
062                            }
063                    }
064                    else {
065                            try {
066                                    Thread.sleep(_retryDelay);
067                            }
068                            catch (InterruptedException ie) {
069                                    if (_log.isWarnEnabled()) {
070                                            _log.warn(
071                                                    "Interrupted while waiting to reacquire lock", ie);
072                                    }
073                            }
074                    }
075            }
076    
077            @SuppressWarnings("unused")
078            protected T performProtectedAction() throws PortalException {
079                    return null;
080            }
081    
082            private static final Log _log = LogFactoryUtil.getLog(
083                    LockProtectedAction.class);
084    
085            private final String _className;
086            private final String _lockKey;
087            private final long _retryDelay;
088            private T _returnValue;
089            private final long _timeout;
090    
091    }