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.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 ReleaseInfo#getBuildNumber}
076             */
077            protected int getBuildNumber() throws Exception {
078                    Connection con = null;
079                    PreparedStatement ps = null;
080                    ResultSet rs = null;
081    
082                    try {
083                            con = DataAccess.getUpgradeOptimizedConnection();
084    
085                            ps = con.prepareStatement(
086                                    "select buildNumber from Release_ where servletContextName " +
087                                            "= ?");
088    
089                            ps.setString(1, ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
090    
091                            rs = ps.executeQuery();
092    
093                            rs.next();
094    
095                            return rs.getInt(1);
096                    }
097                    finally {
098                            DataAccess.cleanUp(con, ps, rs);
099                    }
100            }
101    
102            protected Set<String> getPortalTableNames() throws Exception {
103                    if (_portalTableNames != null) {
104                            return _portalTableNames;
105                    }
106    
107                    Pattern pattern = Pattern.compile("create table (\\S*) \\(");
108    
109                    ClassLoader classLoader = ClassLoaderUtil.getContextClassLoader();
110    
111                    String sql = StringUtil.read(
112                            classLoader,
113                            "com/liferay/portal/tools/sql/dependencies/portal-tables.sql");
114    
115                    Matcher matcher = pattern.matcher(sql);
116    
117                    Set<String> tableNames = new HashSet<String>();
118    
119                    while (matcher.find()) {
120                            String match = matcher.group(1);
121    
122                            tableNames.add(match.toLowerCase());
123                    }
124    
125                    _portalTableNames = tableNames;
126    
127                    return tableNames;
128            }
129    
130            protected boolean isPortalTableName(String tableName) throws Exception {
131                    Set<String> portalTableNames = getPortalTableNames();
132    
133                    return portalTableNames.contains(tableName.toLowerCase());
134            }
135    
136            private static Log _log = LogFactoryUtil.getLog(VerifyProcess.class);
137    
138            private Set<String> _portalTableNames;
139    
140    }