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