001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.SearchEngineUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeException;
021    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
022    import com.liferay.portal.util.PropsValues;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Alexander Chow
027     * @author Raymond Augé
028     */
029    public class UpgradeProcessUtil {
030    
031            public static boolean upgradeProcess(
032                            int buildNumber, String[] upgradeProcessClassNames,
033                            ClassLoader classLoader)
034                    throws UpgradeException {
035    
036                    boolean ranUpgradeProcess = false;
037    
038                    boolean tempIndexReadOnly = SearchEngineUtil.isIndexReadOnly();
039    
040                    if (PropsValues.INDEX_ON_UPGRADE) {
041                            SearchEngineUtil.setIndexReadOnly(true);
042                    }
043    
044                    try {
045                            for (String upgradeProcessClassName : upgradeProcessClassNames) {
046                                    boolean tempRanUpgradeProcess = _upgradeProcess(
047                                            buildNumber, upgradeProcessClassName, classLoader);
048    
049                                    if (tempRanUpgradeProcess) {
050                                            ranUpgradeProcess = true;
051                                    }
052                            }
053                    }
054                    finally {
055                            SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
056                    }
057    
058                    return ranUpgradeProcess;
059            }
060    
061            private static boolean _upgradeProcess(
062                            int buildNumber, String upgradeProcessClassName,
063                            ClassLoader classLoader)
064                    throws UpgradeException {
065    
066                    if (_log.isDebugEnabled()) {
067                            _log.debug("Initializing upgrade " + upgradeProcessClassName);
068                    }
069    
070                    UpgradeProcess upgradeProcess = null;
071    
072                    try {
073                            upgradeProcess = (UpgradeProcess)classLoader.loadClass(
074                                    upgradeProcessClassName).newInstance();
075                    }
076                    catch (Exception e) {
077                            _log.error(e, e);
078                    }
079    
080                    if (upgradeProcess == null) {
081                            _log.error(upgradeProcessClassName + " cannot be found");
082    
083                            return false;
084                    }
085    
086                    if ((upgradeProcess.getThreshold() == 0) ||
087                            (upgradeProcess.getThreshold() > buildNumber)) {
088    
089                            if (_log.isDebugEnabled()) {
090                                    _log.debug("Running upgrade " + upgradeProcessClassName);
091                            }
092    
093                            upgradeProcess.upgrade();
094    
095                            if (_log.isDebugEnabled()) {
096                                    _log.debug("Finished upgrade " + upgradeProcessClassName);
097                            }
098    
099                            return true;
100                    }
101                    else {
102                            if (_log.isDebugEnabled()) {
103                                    _log.debug(
104                                            "Upgrade threshold " + upgradeProcess.getThreshold() +
105                                                    " will not trigger upgrade");
106    
107                                    _log.debug("Skipping upgrade " + upgradeProcessClassName);
108                            }
109    
110                            return false;
111                    }
112            }
113    
114            private static Log _log = LogFactoryUtil.getLog(UpgradeProcessUtil.class);
115    
116    }