001
014
015 package com.liferay.portal.verify;
016
017 import com.liferay.portal.kernel.concurrent.ThrowableAwareRunnable;
018 import com.liferay.portal.kernel.dao.db.BaseDBProcess;
019 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020 import com.liferay.portal.kernel.exception.BulkException;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.ClassLoaderUtil;
024 import com.liferay.portal.kernel.util.ClassUtil;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.model.ReleaseConstants;
027 import com.liferay.portal.util.PropsValues;
028
029 import java.sql.Connection;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032
033 import java.util.ArrayList;
034 import java.util.Collection;
035 import java.util.HashSet;
036 import java.util.List;
037 import java.util.Set;
038 import java.util.concurrent.Callable;
039 import java.util.concurrent.ExecutorService;
040 import java.util.concurrent.Executors;
041 import java.util.concurrent.Future;
042 import java.util.regex.Matcher;
043 import java.util.regex.Pattern;
044
045
055 public abstract class VerifyProcess extends BaseDBProcess {
056
057 public static final int ALWAYS = -1;
058
059 public static final int NEVER = 0;
060
061 public static final int ONCE = 1;
062
063 public void verify() throws VerifyException {
064 long start = System.currentTimeMillis();
065
066 try {
067 if (_log.isInfoEnabled()) {
068 _log.info("Verifying " + ClassUtil.getClassName(this));
069 }
070
071 doVerify();
072 }
073 catch (Exception e) {
074 throw new VerifyException(e);
075 }
076 finally {
077 if (_log.isInfoEnabled()) {
078 _log.info(
079 "Completed verification process " +
080 ClassUtil.getClassName(this) + " in " +
081 (System.currentTimeMillis() - start) + "ms");
082 }
083 }
084 }
085
086 public void verify(VerifyProcess verifyProcess) throws VerifyException {
087 verifyProcess.verify();
088 }
089
090 protected void doVerify() throws Exception {
091 }
092
093 protected void doVerify(
094 Collection<? extends ThrowableAwareRunnable>
095 throwableAwareRunnables)
096 throws Exception {
097
098 if (throwableAwareRunnables.size() <
099 PropsValues.VERIFY_PROCESS_CONCURRENCY_THRESHOLD) {
100
101 for (ThrowableAwareRunnable throwableAwareRunnable :
102 throwableAwareRunnables) {
103
104 throwableAwareRunnable.run();
105 }
106 }
107 else {
108 ExecutorService executorService = Executors.newFixedThreadPool(
109 throwableAwareRunnables.size());
110
111 List<Callable<Object>> jobs = new ArrayList<>(
112 throwableAwareRunnables.size());
113
114 for (Runnable runnable : throwableAwareRunnables) {
115 jobs.add(Executors.callable(runnable));
116 }
117
118 try {
119 List<Future<Object>> futures = executorService.invokeAll(jobs);
120
121 for (Future<Object> future : futures) {
122 future.get();
123 }
124 }
125 finally {
126 executorService.shutdown();
127 }
128 }
129
130 List<Throwable> throwables = new ArrayList<>();
131
132 for (ThrowableAwareRunnable throwableAwareRunnable :
133 throwableAwareRunnables) {
134
135 if (throwableAwareRunnable.hasException()) {
136 throwables.add(throwableAwareRunnable.getThrowable());
137 }
138 }
139
140 if (!throwables.isEmpty()) {
141 throw new BulkException(
142 "Verification error: " + getClass().getName(), throwables);
143 }
144 }
145
146
152 protected int getBuildNumber() throws Exception {
153 Connection con = null;
154 PreparedStatement ps = null;
155 ResultSet rs = null;
156
157 try {
158 con = DataAccess.getUpgradeOptimizedConnection();
159
160 ps = con.prepareStatement(
161 "select buildNumber from Release_ where servletContextName " +
162 "= ?");
163
164 ps.setString(1, ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
165
166 rs = ps.executeQuery();
167
168 rs.next();
169
170 return rs.getInt(1);
171 }
172 finally {
173 DataAccess.cleanUp(con, ps, rs);
174 }
175 }
176
177 protected Set<String> getPortalTableNames() throws Exception {
178 if (_portalTableNames != null) {
179 return _portalTableNames;
180 }
181
182 ClassLoader classLoader = ClassLoaderUtil.getContextClassLoader();
183
184 String sql = StringUtil.read(
185 classLoader,
186 "com/liferay/portal/tools/sql/dependencies/portal-tables.sql");
187
188 Matcher matcher = _createTablePattern.matcher(sql);
189
190 Set<String> tableNames = new HashSet<>();
191
192 while (matcher.find()) {
193 String match = matcher.group(1);
194
195 tableNames.add(StringUtil.toLowerCase(match));
196 }
197
198 _portalTableNames = tableNames;
199
200 return tableNames;
201 }
202
203 protected boolean isPortalTableName(String tableName) throws Exception {
204 Set<String> portalTableNames = getPortalTableNames();
205
206 return portalTableNames.contains(StringUtil.toLowerCase(tableName));
207 }
208
209 private static final Log _log = LogFactoryUtil.getLog(VerifyProcess.class);
210
211 private final Pattern _createTablePattern = Pattern.compile(
212 "create table (\\S*) \\(");
213 private Set<String> _portalTableNames;
214
215 }