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    
023    /**
024     * @author Daniel Kocsis
025     */
026    @ProviderType
027    public class BackgroundTaskLockHelperUtil {
028    
029            public static boolean isLockedBackgroundTask(
030                    BackgroundTask backgroundTask) {
031    
032                    return LockManagerUtil.isLocked(
033                            BackgroundTaskExecutor.class.getName(), getLockKey(backgroundTask));
034            }
035    
036            public static Lock lockBackgroundTask(BackgroundTask backgroundTask) {
037                    String owner =
038                            backgroundTask.getName() + StringPool.POUND +
039                                    backgroundTask.getBackgroundTaskId();
040    
041                    return LockManagerUtil.lock(
042                            BackgroundTaskExecutor.class.getName(), getLockKey(backgroundTask),
043                            owner);
044            }
045    
046            public static void unlockBackgroundTask(BackgroundTask backgroundTask) {
047                    String owner =
048                            backgroundTask.getName() + StringPool.POUND +
049                                    backgroundTask.getBackgroundTaskId();
050    
051                    LockManagerUtil.unlock(
052                            BackgroundTaskExecutor.class.getName(), getLockKey(backgroundTask),
053                            owner);
054            }
055    
056            protected static String getLockKey(BackgroundTask backgroundTask) {
057                    BackgroundTaskExecutor backgroundTaskExecutor =
058                            BackgroundTaskExecutorRegistryUtil.getBackgroundTaskExecutor(
059                                    backgroundTask.getTaskExecutorClassName());
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    }