001    /**
002     * Copyright (c) 2000-2013 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.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                            db.updateIndexes(connection, tablesSQL, indexesSQL, dropIndexes);
094                    }
095                    catch (Exception e) {
096                            if (_log.isWarnEnabled()) {
097                                    _log.warn(e, e);
098                            }
099                    }
100            }
101    
102            public void upgradeProcess(int buildNumber) throws UpgradeException {
103                    if (buildNumber == ReleaseInfo.getParentBuildNumber()) {
104                            if (_log.isDebugEnabled()) {
105                                    _log.debug(
106                                            "Skipping upgrade process from " + buildNumber + " to " +
107                                                    ReleaseInfo.getParentBuildNumber());
108                            }
109    
110                            return;
111                    }
112    
113                    String[] upgradeProcessClassNames = getUpgradeProcessClassNames(
114                            PropsKeys.UPGRADE_PROCESSES);
115    
116                    if (upgradeProcessClassNames.length == 0) {
117                            upgradeProcessClassNames = getUpgradeProcessClassNames(
118                                    PropsKeys.UPGRADE_PROCESSES + StringPool.PERIOD + buildNumber);
119    
120                            if (upgradeProcessClassNames.length == 0) {
121                                    if (_log.isInfoEnabled()) {
122                                            _log.info(
123                                                    "Upgrading from " + buildNumber + " to " +
124                                                            ReleaseInfo.getParentBuildNumber() + " is not " +
125                                                                    "supported");
126                                    }
127    
128                                    System.exit(0);
129                            }
130                    }
131    
132                    _upgraded = UpgradeProcessUtil.upgradeProcess(
133                            buildNumber, upgradeProcessClassNames,
134                            ClassLoaderUtil.getPortalClassLoader());
135            }
136    
137            public void verifyProcess(boolean newBuildNumber, boolean verified)
138                    throws VerifyException {
139    
140                    _verified = VerifyProcessUtil.verifyProcess(
141                            _upgraded, newBuildNumber, verified);
142            }
143    
144            protected String[] getUpgradeProcessClassNames(String key) {
145    
146                    // We would normally call PropsUtil#getArray(String) to return a String
147                    // array based on a comma delimited value. However, there is a bug with
148                    // Apache Commons Configuration where multi-line comma delimited values
149                    // do not interpolate properly (i.e. cannot be referenced by other
150                    // properties). The workaround to the bug is to escape commas with a
151                    // back slash. To get the configured String array, we have to call
152                    // PropsUtil#get(String) and manually split the value into a String
153                    // array instead of simply calling PropsUtil#getArray(String).
154    
155                    return StringUtil.split(GetterUtil.getString(PropsUtil.get(key)));
156            }
157    
158            private static Log _log = LogFactoryUtil.getLog(StartupHelper.class);
159    
160            private boolean _dropIndexes;
161            private boolean _upgraded;
162            private boolean _verified;
163    
164    }