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