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.events;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.upgrade.UpgradeException;
023    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
024    import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.ReleaseInfo;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.util.ClassLoaderUtil;
031    import com.liferay.portal.util.PropsUtil;
032    import com.liferay.portal.verify.VerifyException;
033    import com.liferay.portal.verify.VerifyProcessUtil;
034    
035    import java.sql.Connection;
036    
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Alexander Chow
042     * @author Raymond Aug??
043     */
044    public class StartupHelper {
045    
046            public boolean isStartupFinished() {
047                    return _startupFinished;
048            }
049    
050            public boolean isUpgraded() {
051                    return _upgraded;
052            }
053    
054            public boolean isUpgrading() {
055                    return _upgrading;
056            }
057    
058            public boolean isVerified() {
059                    return _verified;
060            }
061    
062            public void setDropIndexes(boolean dropIndexes) {
063                    _dropIndexes = dropIndexes;
064            }
065    
066            public void setStartupFinished(boolean startupFinished) {
067                    _startupFinished = startupFinished;
068            }
069    
070            public void updateIndexes() {
071                    updateIndexes(_dropIndexes);
072            }
073    
074            public void updateIndexes(boolean dropIndexes) {
075                    DB db = DBFactoryUtil.getDB();
076    
077                    Connection connection = null;
078    
079                    try {
080                            connection = DataAccess.getConnection();
081    
082                            updateIndexes(db, connection, dropIndexes);
083                    }
084                    catch (Exception e) {
085                            if (_log.isWarnEnabled()) {
086                                    _log.warn(e, e);
087                            }
088                    }
089                    finally {
090                            DataAccess.cleanUp(connection);
091                    }
092            }
093    
094            public void updateIndexes(
095                    DB db, Connection connection, boolean dropIndexes) {
096    
097                    try {
098                            ClassLoader classLoader = ClassLoaderUtil.getContextClassLoader();
099    
100                            String tablesSQL = StringUtil.read(
101                                    classLoader,
102                                    "com/liferay/portal/tools/sql/dependencies/portal-tables.sql");
103    
104                            String indexesSQL = StringUtil.read(
105                                    classLoader,
106                                    "com/liferay/portal/tools/sql/dependencies/indexes.sql");
107    
108                            db.updateIndexes(connection, tablesSQL, indexesSQL, dropIndexes);
109                    }
110                    catch (Exception e) {
111                            if (_log.isWarnEnabled()) {
112                                    _log.warn(e, e);
113                            }
114                    }
115            }
116    
117            public void upgradeProcess(int buildNumber) throws UpgradeException {
118                    _upgrading = true;
119    
120                    try {
121                            if (buildNumber == ReleaseInfo.getParentBuildNumber()) {
122                                    if (_log.isDebugEnabled()) {
123                                            _log.debug(
124                                                    "Skipping upgrade process from " + buildNumber +
125                                                            " to " + ReleaseInfo.getParentBuildNumber());
126                                    }
127    
128                                    return;
129                            }
130    
131                            String[] upgradeProcessClassNames = getUpgradeProcessClassNames(
132                                    PropsKeys.UPGRADE_PROCESSES);
133    
134                            if (upgradeProcessClassNames.length == 0) {
135                                    upgradeProcessClassNames = getUpgradeProcessClassNames(
136                                            PropsKeys.UPGRADE_PROCESSES + StringPool.PERIOD +
137                                                    buildNumber);
138    
139                                    if (upgradeProcessClassNames.length == 0) {
140                                            if (_log.isInfoEnabled()) {
141                                                    _log.info(
142                                                            "Upgrading from " + buildNumber + " to " +
143                                                                    ReleaseInfo.getParentBuildNumber() +
144                                                                            " is not supported");
145                                            }
146    
147                                            System.exit(0);
148                                    }
149                            }
150    
151                            List<UpgradeProcess> upgradeProcesses =
152                                    UpgradeProcessUtil.initUpgradeProcesses(
153                                            ClassLoaderUtil.getPortalClassLoader(),
154                                            upgradeProcessClassNames);
155    
156                            _upgraded = UpgradeProcessUtil.upgradeProcess(
157                                    buildNumber, upgradeProcesses);
158                    }
159                    finally {
160                            _upgrading = false;
161                    }
162            }
163    
164            public void verifyProcess(boolean newBuildNumber, boolean verified)
165                    throws VerifyException {
166    
167                    _verified = VerifyProcessUtil.verifyProcess(
168                            _upgraded, newBuildNumber, verified);
169            }
170    
171            protected String[] getUpgradeProcessClassNames(String key) {
172    
173                    // We would normally call PropsUtil#getArray(String) to return a String
174                    // array based on a comma delimited value. However, there is a bug with
175                    // Apache Commons Configuration where multi-line comma delimited values
176                    // do not interpolate properly (i.e. cannot be referenced by other
177                    // properties). The workaround to the bug is to escape commas with a
178                    // back slash. To get the configured String array, we have to call
179                    // PropsUtil#get(String) and manually split the value into a String
180                    // array instead of simply calling PropsUtil#getArray(String).
181    
182                    return StringUtil.split(GetterUtil.getString(PropsUtil.get(key)));
183            }
184    
185            private static final Log _log = LogFactoryUtil.getLog(StartupHelper.class);
186    
187            private boolean _dropIndexes;
188            private boolean _startupFinished;
189            private boolean _upgraded;
190            private boolean _upgrading;
191            private boolean _verified;
192    
193    }