001    /**
002     * Copyright (c) 2000-2010 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.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.io.IOException;
023    
024    import java.sql.SQLException;
025    
026    import javax.naming.NamingException;
027    
028    /**
029     * This abstract class should be extended for startup processes that verify the
030     * integrity of the database. They can be added as part of
031     * <code>com.liferay.portal.verify.VerifyProcessSuite</code> or be executed
032     * independently by being set in the portal.properties file. Each of these
033     * processes should not cause any problems if run multiple times.
034     *
035     * @author Alexander Chow
036     */
037    public abstract class VerifyProcess {
038    
039            public static final int ALWAYS = -1;
040    
041            public static final int NEVER = 0;
042    
043            public static final int ONCE = 1;
044    
045            public void runSQL(String template) throws IOException, SQLException {
046                    DB db = DBFactoryUtil.getDB();
047    
048                    db.runSQL(template);
049            }
050    
051            public void runSQL(String[] templates) throws IOException, SQLException {
052                    DB db = DBFactoryUtil.getDB();
053    
054                    db.runSQL(templates);
055            }
056    
057            public void runSQLTemplate(String path)
058                    throws IOException, NamingException, SQLException {
059    
060                    DB db = DBFactoryUtil.getDB();
061    
062                    db.runSQLTemplate(path);
063            }
064    
065            public void runSQLTemplate(String path, boolean failOnError)
066                    throws IOException, NamingException, SQLException {
067    
068                    DB db = DBFactoryUtil.getDB();
069    
070                    db.runSQLTemplate(path, failOnError);
071            }
072    
073            public void verify() throws VerifyException {
074                    try {
075                            if (_log.isInfoEnabled()) {
076                                    _log.info("Verifying " + getClass().getName());
077                            }
078    
079                            doVerify();
080                    }
081                    catch (Exception e) {
082                            throw new VerifyException(e);
083                    }
084            }
085    
086            public void verify(VerifyProcess verifyProcess)
087                    throws VerifyException {
088    
089                    verifyProcess.verify();
090            }
091    
092            protected void doVerify() throws Exception {
093            }
094    
095            private static Log _log = LogFactoryUtil.getLog(VerifyProcess.class);
096    
097    }