001    /**
002     * Copyright (c) 2000-2010 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.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    }