001
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
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
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 }