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