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.dao.db.BaseDBProcess;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.model.ReleaseConstants;
023    import com.liferay.portal.util.ClassLoaderUtil;
024    
025    import java.sql.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    import java.util.HashSet;
030    import java.util.Set;
031    import java.util.regex.Matcher;
032    import java.util.regex.Pattern;
033    
034    /**
035     * This abstract class should be extended for startup processes that verify the
036     * integrity of the database. They can be added as part of
037     * <code>com.liferay.portal.verify.VerifyProcessSuite</code> or be executed
038     * independently by being set in the portal.properties file. Each of these
039     * processes should not cause any problems if run multiple times.
040     *
041     * @author Alexander Chow
042     * @author Hugo Huijser
043     */
044    public abstract class VerifyProcess extends BaseDBProcess {
045    
046            public static final int ALWAYS = -1;
047    
048            public static final int NEVER = 0;
049    
050            public static final int ONCE = 1;
051    
052            public void verify() throws VerifyException {
053                    try {
054                            if (_log.isInfoEnabled()) {
055                                    _log.info("Verifying " + getClass().getName());
056                            }
057    
058                            doVerify();
059                    }
060                    catch (Exception e) {
061                            throw new VerifyException(e);
062                    }
063            }
064    
065            public void verify(VerifyProcess verifyProcess) throws VerifyException {
066                    verifyProcess.verify();
067            }
068    
069            protected void doVerify() throws Exception {
070            }
071    
072            /**
073             * @return the portal build number before {@link
074             *         com.liferay.portal.tools.DBUpgrader} has a chance to update it to
075             *         the value in {@link
076             *         com.liferay.portal.kernel.util.ReleaseInfo#getBuildNumber}
077             */
078            protected int getBuildNumber() throws Exception {
079                    Connection con = null;
080                    PreparedStatement ps = null;
081                    ResultSet rs = null;
082    
083                    try {
084                            con = DataAccess.getUpgradeOptimizedConnection();
085    
086                            ps = con.prepareStatement(
087                                    "select buildNumber from Release_ where servletContextName " +
088                                            "= ?");
089    
090                            ps.setString(1, ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
091    
092                            rs = ps.executeQuery();
093    
094                            rs.next();
095    
096                            return rs.getInt(1);
097                    }
098                    finally {
099                            DataAccess.cleanUp(con, ps, rs);
100                    }
101            }
102    
103            protected Set<String> getPortalTableNames() throws Exception {
104                    if (_portalTableNames != null) {
105                            return _portalTableNames;
106                    }
107    
108                    Pattern pattern = Pattern.compile("create table (\\S*) \\(");
109    
110                    ClassLoader classLoader = ClassLoaderUtil.getContextClassLoader();
111    
112                    String sql = StringUtil.read(
113                            classLoader,
114                            "com/liferay/portal/tools/sql/dependencies/portal-tables.sql");
115    
116                    Matcher matcher = pattern.matcher(sql);
117    
118                    Set<String> tableNames = new HashSet<String>();
119    
120                    while (matcher.find()) {
121                            String match = matcher.group(1);
122    
123                            tableNames.add(match.toLowerCase());
124                    }
125    
126                    _portalTableNames = tableNames;
127    
128                    return tableNames;
129            }
130    
131            protected boolean isPortalTableName(String tableName) throws Exception {
132                    Set<String> portalTableNames = getPortalTableNames();
133    
134                    return portalTableNames.contains(tableName.toLowerCase());
135            }
136    
137            private static Log _log = LogFactoryUtil.getLog(VerifyProcess.class);
138    
139            private Set<String> _portalTableNames;
140    
141    }