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