001    /**
002     * Copyright (c) 2000-2013 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.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatus;
018    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatusRegistry;
019    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatusRegistryUtil;
020    import com.liferay.portal.kernel.cluster.ClusterMasterExecutorUtil;
021    import com.liferay.portal.kernel.cluster.ClusterNodeResponse;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.MethodHandler;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.concurrent.Future;
029    import java.util.concurrent.locks.Lock;
030    import java.util.concurrent.locks.ReadWriteLock;
031    import java.util.concurrent.locks.ReentrantReadWriteLock;
032    
033    /**
034     * @author Michael C. Han
035     */
036    public class BackgroundTaskStatusRegistryImpl
037            implements BackgroundTaskStatusRegistry {
038    
039            @Override
040            public BackgroundTaskStatus getBackgroundTaskStatus(long backgroundTaskId) {
041                    if (!ClusterMasterExecutorUtil.isMaster()) {
042                            return getMasterBackgroundTaskStatus(backgroundTaskId);
043                    }
044    
045                    Lock lock = _readWriteLock.readLock();
046    
047                    lock.lock();
048    
049                    try {
050                            return _backgroundTaskStatuses.get(backgroundTaskId);
051                    }
052                    finally {
053                            lock.unlock();
054                    }
055            }
056    
057            @Override
058            public BackgroundTaskStatus registerBackgroundTaskStatus(
059                    long backgroundTaskId) {
060    
061                    Lock lock = _readWriteLock.writeLock();
062    
063                    lock.lock();
064    
065                    try {
066                            BackgroundTaskStatus backgroundTaskStatus =
067                                    _backgroundTaskStatuses.get(backgroundTaskId);
068    
069                            if (backgroundTaskStatus == null) {
070                                    backgroundTaskStatus = new BackgroundTaskStatus();
071    
072                                    _backgroundTaskStatuses.put(
073                                            backgroundTaskId, backgroundTaskStatus);
074                            }
075    
076                            return backgroundTaskStatus;
077                    }
078                    finally {
079                            lock.unlock();
080                    }
081            }
082    
083            @Override
084            public BackgroundTaskStatus unregisterBackgroundTaskStatus(
085                    long backgroundTaskId) {
086    
087                    Lock lock = _readWriteLock.writeLock();
088    
089                    lock.lock();
090    
091                    try {
092                            BackgroundTaskStatus backgroundTaskStatus =
093                                    _backgroundTaskStatuses.remove(backgroundTaskId);
094    
095                            return backgroundTaskStatus;
096                    }
097                    finally {
098                            lock.unlock();
099                    }
100            }
101    
102            protected BackgroundTaskStatus getMasterBackgroundTaskStatus(
103                    long backgroundTaskId) {
104    
105                    try {
106                            MethodHandler methodHandler = new MethodHandler(
107                                    BackgroundTaskStatusRegistryUtil.class.getDeclaredMethod(
108                                            "getBackgroundTaskStatus", long.class),
109                                    backgroundTaskId);
110    
111                            Future<ClusterNodeResponse> future =
112                                    ClusterMasterExecutorUtil.executeOnMaster(methodHandler);
113    
114                            ClusterNodeResponse clusterNodeResponse = future.get();
115    
116                            return (BackgroundTaskStatus)clusterNodeResponse.getResult();
117                    }
118                    catch (Exception e) {
119                            _log.error("Unable to retrieve status from master node", e);
120                    }
121    
122                    return null;
123            }
124    
125            private static Log _log = LogFactoryUtil.getLog(
126                    BackgroundTaskStatusRegistryImpl.class);
127    
128            private final Map<Long, BackgroundTaskStatus> _backgroundTaskStatuses =
129                    new HashMap<Long, BackgroundTaskStatus>();
130            private ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
131    
132    }