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