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.backgroundtask;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.lock.Lock;
020    import com.liferay.portal.kernel.lock.LockManagerUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.BackgroundTask;
023    
024    /**
025     * @author Daniel Kocsis
026     */
027    @ProviderType
028    public class BackgroundTaskLockHelperUtil {
029    
030            public static boolean isLockedBackgroundTask(
031                    BackgroundTask backgroundTask) {
032    
033                    return LockManagerUtil.isLocked(
034                            BackgroundTaskExecutor.class.getName(), getLockKey(backgroundTask));
035            }
036    
037            public static Lock lockBackgroundTask(BackgroundTask backgroundTask) {
038                    String owner =
039                            backgroundTask.getName() + StringPool.POUND +
040                                    backgroundTask.getBackgroundTaskId();
041    
042                    return LockManagerUtil.lock(
043                            BackgroundTaskExecutor.class.getName(), getLockKey(backgroundTask),
044                            owner);
045            }
046    
047            public static void unlockBackgroundTask(BackgroundTask backgroundTask) {
048                    String owner =
049                            backgroundTask.getName() + StringPool.POUND +
050                                    backgroundTask.getBackgroundTaskId();
051    
052                    LockManagerUtil.unlock(
053                            BackgroundTaskExecutor.class.getName(), getLockKey(backgroundTask),
054                            owner);
055            }
056    
057            protected static String getLockKey(BackgroundTask backgroundTask) {
058                    BackgroundTaskExecutor backgroundTaskExecutor =
059                            backgroundTask.getBackgroundTaskExecutor();
060    
061                    String lockKey = StringPool.BLANK;
062    
063                    if (backgroundTaskExecutor.getIsolationLevel() ==
064                                    BackgroundTaskConstants.ISOLATION_LEVEL_CLASS) {
065    
066                            lockKey = backgroundTask.getTaskExecutorClassName();
067                    }
068                    else if (backgroundTaskExecutor.getIsolationLevel() ==
069                                            BackgroundTaskConstants.ISOLATION_LEVEL_COMPANY) {
070    
071                            lockKey =
072                                    backgroundTask.getTaskExecutorClassName() +
073                                            StringPool.POUND + backgroundTask.getCompanyId();
074                    }
075                    else if (backgroundTaskExecutor.getIsolationLevel() ==
076                                            BackgroundTaskConstants.ISOLATION_LEVEL_GROUP) {
077    
078                            lockKey =
079                                    backgroundTask.getTaskExecutorClassName() +
080                                            StringPool.POUND + backgroundTask.getGroupId();
081                    }
082                    else {
083                            lockKey =
084                                    backgroundTask.getTaskExecutorClassName() +
085                                            StringPool.POUND +
086                                                    backgroundTaskExecutor.getIsolationLevel();
087                    }
088    
089                    return lockKey;
090            }
091    
092    }