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.kernel.backgroundtask;
016    
017    import com.liferay.portal.DuplicateLockException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.BackgroundTask;
023    import com.liferay.portal.model.Lock;
024    import com.liferay.portal.service.LockLocalServiceUtil;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class SerialBackgroundTaskExecutor
030            extends DelegatingBackgroundTaskExecutor {
031    
032            public SerialBackgroundTaskExecutor(
033                    BackgroundTaskExecutor backgroundTaskExecutor) {
034    
035                    super(backgroundTaskExecutor);
036            }
037    
038            @Override
039            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
040                    throws Exception {
041    
042                    Lock lock = null;
043    
044                    String owner =
045                            backgroundTask.getName() + StringPool.POUND +
046                                    backgroundTask.getBackgroundTaskId();
047    
048                    try {
049                            if (isSerial()) {
050                                    lock = acquireLock(backgroundTask, owner);
051                            }
052    
053                            BackgroundTaskExecutor backgroundTaskExecutor =
054                                    getBackgroundTaskExecutor();
055    
056                            return backgroundTaskExecutor.execute(backgroundTask);
057                    }
058                    finally {
059                            if (lock != null) {
060                                    LockLocalServiceUtil.unlock(
061                                            BackgroundTaskExecutor.class.getName(),
062                                            backgroundTask.getTaskExecutorClassName(), owner);
063                            }
064                    }
065            }
066    
067            protected Lock acquireLock(BackgroundTask backgroundTask, String owner)
068                    throws DuplicateLockException {
069    
070                    Lock lock = null;
071    
072                    while (true) {
073                            try {
074                                    lock = LockLocalServiceUtil.lock(
075                                            BackgroundTaskExecutor.class.getName(),
076                                            backgroundTask.getTaskExecutorClassName(), owner);
077    
078                                    break;
079                            }
080                            catch (SystemException se) {
081                                    if (_log.isDebugEnabled()) {
082                                            _log.debug("Unable to acquire acquiring lock", se);
083                                    }
084    
085                                    try {
086                                            Thread.sleep(50);
087                                    }
088                                    catch (InterruptedException ie) {
089                                    }
090                            }
091                    }
092    
093                    if (!lock.isNew()) {
094                            throw new DuplicateLockException(lock);
095                    }
096    
097                    return lock;
098            }
099    
100            private static Log _log = LogFactoryUtil.getLog(
101                    SerialBackgroundTaskExecutor.class);
102    
103    }