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