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 com.liferay.portal.kernel.lock.DuplicateLockException;
018    import com.liferay.portal.kernel.lock.Lock;
019    
020    /**
021     * @author Michael C. Han
022     */
023    public class SerialBackgroundTaskExecutor
024            extends DelegatingBackgroundTaskExecutor {
025    
026            public SerialBackgroundTaskExecutor(
027                    BackgroundTaskExecutor backgroundTaskExecutor) {
028    
029                    super(backgroundTaskExecutor);
030            }
031    
032            @Override
033            public BackgroundTaskExecutor clone() {
034                    BackgroundTaskExecutor backgroundTaskExecutor =
035                            new SerialBackgroundTaskExecutor(getBackgroundTaskExecutor());
036    
037                    return backgroundTaskExecutor;
038            }
039    
040            @Override
041            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
042                    throws Exception {
043    
044                    Lock lock = null;
045    
046                    try {
047                            if (isSerial()) {
048                                    lock = acquireLock(backgroundTask);
049                            }
050    
051                            BackgroundTaskExecutor backgroundTaskExecutor =
052                                    getBackgroundTaskExecutor();
053    
054                            return backgroundTaskExecutor.execute(backgroundTask);
055                    }
056                    finally {
057                            if (lock != null) {
058                                    BackgroundTaskLockHelperUtil.unlockBackgroundTask(
059                                            backgroundTask);
060                            }
061                    }
062            }
063    
064            protected Lock acquireLock(BackgroundTask backgroundTask)
065                    throws DuplicateLockException {
066    
067                    Lock lock = BackgroundTaskLockHelperUtil.lockBackgroundTask(
068                            backgroundTask);
069    
070                    if (!lock.isNew()) {
071                            throw new DuplicateLockException(lock);
072                    }
073    
074                    return lock;
075            }
076    
077    }