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