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.verify;
016    
017    import com.liferay.portal.kernel.concurrent.ThrowableAwareRunnable;
018    import com.liferay.portal.kernel.concurrent.ThrowableAwareRunnablesExecutorUtil;
019    import com.liferay.portal.kernel.dao.db.BaseDBProcess;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.exception.BulkException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.model.ReleaseConstants;
025    import com.liferay.portal.kernel.util.ClassLoaderUtil;
026    import com.liferay.portal.kernel.util.ClassUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.sql.Connection;
031    import java.sql.PreparedStatement;
032    import java.sql.ResultSet;
033    
034    import java.util.ArrayList;
035    import java.util.Collection;
036    import java.util.HashSet;
037    import java.util.List;
038    import java.util.Set;
039    import java.util.regex.Matcher;
040    import java.util.regex.Pattern;
041    
042    /**
043     * This abstract class should be extended for startup processes that verify the
044     * integrity of the database. They can be added as part of
045     * <code>VerifyProcessSuite</code> or be executed independently by being set in
046     * the portal.properties file. Each of these processes should not cause any
047     * problems if run multiple times.
048     *
049     * @author Alexander Chow
050     * @author Hugo Huijser
051     */
052    public abstract class VerifyProcess extends BaseDBProcess {
053    
054            public static final int ALWAYS = -1;
055    
056            public static final int NEVER = 0;
057    
058            public static final int ONCE = 1;
059    
060            public void verify() throws VerifyException {
061                    long start = System.currentTimeMillis();
062    
063                    if (_log.isInfoEnabled()) {
064                            _log.info("Verifying " + ClassUtil.getClassName(this));
065                    }
066    
067                    try (Connection con = DataAccess.getUpgradeOptimizedConnection()) {
068                            connection = con;
069    
070                            doVerify();
071                    }
072                    catch (Exception e) {
073                            throw new VerifyException(e);
074                    }
075                    finally {
076                            connection = null;
077    
078                            if (_log.isInfoEnabled()) {
079                                    _log.info(
080                                            "Completed verification process " +
081                                                    ClassUtil.getClassName(this) + " in " +
082                                                            (System.currentTimeMillis() - start) + "ms");
083                            }
084                    }
085            }
086    
087            public void verify(VerifyProcess verifyProcess) throws VerifyException {
088                    verifyProcess.verify();
089            }
090    
091            protected void doVerify() throws Exception {
092            }
093    
094            protected void doVerify(
095                            Collection<? extends ThrowableAwareRunnable>
096                                    throwableAwareRunnables)
097                    throws Exception {
098    
099                    if ((throwableAwareRunnables.size() <
100                                    PropsValues.VERIFY_PROCESS_CONCURRENCY_THRESHOLD) &&
101                            !isForceConcurrent(throwableAwareRunnables)) {
102    
103                            for (ThrowableAwareRunnable throwableAwareRunnable :
104                                            throwableAwareRunnables) {
105    
106                                    throwableAwareRunnable.run();
107                            }
108    
109                            List<Throwable> throwables = new ArrayList<>();
110    
111                            for (ThrowableAwareRunnable throwableAwareRunnable :
112                                            throwableAwareRunnables) {
113    
114                                    if (throwableAwareRunnable.hasException()) {
115                                            throwables.add(throwableAwareRunnable.getThrowable());
116                                    }
117                            }
118    
119                            if (!throwables.isEmpty()) {
120                                    Class<?> clazz = getClass();
121    
122                                    throw new BulkException(
123                                            "Verification error: " + clazz.getName(), throwables);
124                            }
125                    }
126                    else {
127                            ThrowableAwareRunnablesExecutorUtil.execute(
128                                    throwableAwareRunnables);
129                    }
130            }
131    
132            /**
133             * @return the portal build number before {@link
134             *         com.liferay.portal.tools.DBUpgrader} has a chance to update it to
135             *         the value in {@link
136             *         com.liferay.portal.kernel.util.ReleaseInfo#getBuildNumber}
137             */
138            protected int getBuildNumber() throws Exception {
139                    try (PreparedStatement ps = connection.prepareStatement(
140                                    "select buildNumber from Release_ where servletContextName " +
141                                            "= ?")) {
142    
143                            ps.setString(1, ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
144    
145                            try (ResultSet rs = ps.executeQuery()) {
146                                    rs.next();
147    
148                                    return rs.getInt(1);
149                            }
150                    }
151            }
152    
153            protected Set<String> getPortalTableNames() throws Exception {
154                    if (_portalTableNames != null) {
155                            return _portalTableNames;
156                    }
157    
158                    ClassLoader classLoader = ClassLoaderUtil.getContextClassLoader();
159    
160                    String sql = StringUtil.read(
161                            classLoader,
162                            "com/liferay/portal/tools/sql/dependencies/portal-tables.sql");
163    
164                    Matcher matcher = _createTablePattern.matcher(sql);
165    
166                    Set<String> tableNames = new HashSet<>();
167    
168                    while (matcher.find()) {
169                            String match = matcher.group(1);
170    
171                            tableNames.add(StringUtil.toLowerCase(match));
172                    }
173    
174                    _portalTableNames = tableNames;
175    
176                    return tableNames;
177            }
178    
179            protected boolean isForceConcurrent(
180                    Collection<? extends ThrowableAwareRunnable> throwableAwareRunnables) {
181    
182                    return false;
183            }
184    
185            protected boolean isPortalTableName(String tableName) throws Exception {
186                    Set<String> portalTableNames = getPortalTableNames();
187    
188                    return portalTableNames.contains(StringUtil.toLowerCase(tableName));
189            }
190    
191            private static final Log _log = LogFactoryUtil.getLog(VerifyProcess.class);
192    
193            private final Pattern _createTablePattern = Pattern.compile(
194                    "create table (\\S*) \\(");
195            private Set<String> _portalTableNames;
196    
197    }