001    /**
002     * Copyright (c) 2000-2010 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.upgrade.UpgradeException;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Alexander Chow
025     * @author Raymond Augé
026     */
027    public class UpgradeProcessUtil {
028    
029            public static boolean upgradeProcess(
030                            int buildNumber, String[] upgradeProcessClassNames,
031                            ClassLoader classLoader)
032                    throws UpgradeException {
033    
034                    boolean ranUpgradeProcess = false;
035    
036                    for (String upgradeProcessClassName : upgradeProcessClassNames) {
037                            boolean tempRanUpgradeProcess = _upgradeProcess(
038                                    buildNumber, upgradeProcessClassName, classLoader);
039    
040                            if (tempRanUpgradeProcess) {
041                                    ranUpgradeProcess = true;
042                            }
043                    }
044    
045                    return ranUpgradeProcess;
046            }
047    
048            private static boolean _upgradeProcess(
049                            int buildNumber, String upgradeProcessClassName,
050                            ClassLoader classLoader)
051                    throws UpgradeException {
052    
053                    if (_log.isDebugEnabled()) {
054                            _log.debug("Initializing upgrade " + upgradeProcessClassName);
055                    }
056    
057                    UpgradeProcess upgradeProcess = null;
058    
059                    try {
060                            upgradeProcess = (UpgradeProcess)classLoader.loadClass(
061                                    upgradeProcessClassName).newInstance();
062                    }
063                    catch (Exception e) {
064                            _log.error(e, e);
065                    }
066    
067                    if (upgradeProcess == null) {
068                            _log.error(upgradeProcessClassName + " cannot be found");
069    
070                            return false;
071                    }
072    
073                    if ((upgradeProcess.getThreshold() == 0) ||
074                            (upgradeProcess.getThreshold() > buildNumber)) {
075    
076                            if (_log.isDebugEnabled()) {
077                                    _log.debug("Running upgrade " + upgradeProcessClassName);
078                            }
079    
080                            upgradeProcess.upgrade();
081    
082                            if (_log.isDebugEnabled()) {
083                                    _log.debug("Finished upgrade " + upgradeProcessClassName);
084                            }
085    
086                            return true;
087                    }
088                    else {
089                            if (_log.isDebugEnabled()) {
090                                    _log.debug(
091                                            "Upgrade threshold " + upgradeProcess.getThreshold() +
092                                                    " will not trigger upgrade");
093    
094                                    _log.debug("Skipping upgrade " + upgradeProcessClassName);
095                            }
096    
097                            return false;
098                    }
099            }
100    
101            private static Log _log = LogFactoryUtil.getLog(UpgradeProcessUtil.class);
102    
103    }