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.security.pacl.PACLClassLoaderUtil;
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
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 = PACLClassLoaderUtil.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 }