001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.model.ReleaseConstants;
023    
024    import java.io.IOException;
025    
026    import java.sql.Connection;
027    import java.sql.PreparedStatement;
028    import java.sql.ResultSet;
029    import java.sql.SQLException;
030    
031    import javax.naming.NamingException;
032    
033    /**
034     * This abstract class should be extended for startup processes that verify the
035     * integrity of the database. They can be added as part of
036     * <code>com.liferay.portal.verify.VerifyProcessSuite</code> or be executed
037     * independently by being set in the portal.properties file. Each of these
038     * processes should not cause any problems if run multiple times.
039     *
040     * @author Alexander Chow
041     */
042    public abstract class VerifyProcess {
043    
044            public static final int ALWAYS = -1;
045    
046            public static final int NEVER = 0;
047    
048            public static final int ONCE = 1;
049    
050            public void runSQL(String template) throws IOException, SQLException {
051                    DB db = DBFactoryUtil.getDB();
052    
053                    db.runSQL(template);
054            }
055    
056            public void runSQL(String[] templates) throws IOException, SQLException {
057                    DB db = DBFactoryUtil.getDB();
058    
059                    db.runSQL(templates);
060            }
061    
062            public void runSQLTemplate(String path)
063                    throws IOException, NamingException, SQLException {
064    
065                    DB db = DBFactoryUtil.getDB();
066    
067                    db.runSQLTemplate(path);
068            }
069    
070            public void runSQLTemplate(String path, boolean failOnError)
071                    throws IOException, NamingException, SQLException {
072    
073                    DB db = DBFactoryUtil.getDB();
074    
075                    db.runSQLTemplate(path, failOnError);
076            }
077    
078            public void verify() throws VerifyException {
079                    try {
080                            if (_log.isInfoEnabled()) {
081                                    _log.info("Verifying " + getClass().getName());
082                            }
083    
084                            doVerify();
085                    }
086                    catch (Exception e) {
087                            throw new VerifyException(e);
088                    }
089            }
090    
091            public void verify(VerifyProcess verifyProcess) throws VerifyException {
092                    verifyProcess.verify();
093            }
094    
095            protected void doVerify() throws Exception {
096            }
097    
098            /**
099             * @return the portal build number before {@link
100             *         com.liferay.portal.tools.DBUpgrader} has a chance to update it to
101             *         the value in {@link ReleaseInfo#getBuildNumber}
102             */
103            protected int getBuildNumber() throws Exception {
104                    Connection con = null;
105                    PreparedStatement ps = null;
106                    ResultSet rs = null;
107    
108                    try {
109                            con = DataAccess.getUpgradeOptimizedConnection();
110    
111                            ps = con.prepareStatement(
112                                    "select buildNumber from Release_ where servletContextName " +
113                                            "= ?");
114    
115                            ps.setString(1, ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
116    
117                            rs = ps.executeQuery();
118    
119                            rs.next();
120    
121                            return rs.getInt(1);
122                    }
123                    finally {
124                            DataAccess.cleanUp(con, ps, rs);
125                    }
126            }
127    
128            private static Log _log = LogFactoryUtil.getLog(VerifyProcess.class);
129    
130    }