001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.dao.orm.common.SQLTransformer;
018 import com.liferay.portal.events.StartupHelperUtil;
019 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
021 import com.liferay.portal.kernel.dao.db.DB;
022 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
023 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
024 import com.liferay.portal.kernel.exception.PortalException;
025 import com.liferay.portal.kernel.log.Log;
026 import com.liferay.portal.kernel.log.LogFactoryUtil;
027 import com.liferay.portal.kernel.spring.aop.Skip;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.PropsKeys;
030 import com.liferay.portal.kernel.util.ReflectionUtil;
031 import com.liferay.portal.kernel.util.ReleaseInfo;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.Time;
034 import com.liferay.portal.model.Release;
035 import com.liferay.portal.model.ReleaseConstants;
036 import com.liferay.portal.service.ClassNameLocalServiceUtil;
037 import com.liferay.portal.service.ReleaseLocalServiceUtil;
038 import com.liferay.portal.service.ResourceActionLocalServiceUtil;
039 import com.liferay.portal.spring.aop.ServiceBeanAopCacheManager;
040 import com.liferay.portal.util.InitUtil;
041 import com.liferay.portal.util.PropsUtil;
042 import com.liferay.portal.util.PropsValues;
043 import com.liferay.util.dao.orm.CustomSQLUtil;
044
045 import java.lang.annotation.Annotation;
046 import java.lang.reflect.Field;
047
048 import java.sql.Connection;
049 import java.sql.Date;
050 import java.sql.PreparedStatement;
051 import java.sql.ResultSet;
052
053 import java.util.HashMap;
054 import java.util.concurrent.ConcurrentHashMap;
055
056 import org.aopalliance.intercept.MethodInvocation;
057
058 import org.apache.commons.lang.time.StopWatch;
059
060
064 public class DBUpgrader {
065
066 public static void main(String[] args) {
067 try {
068 StopWatch stopWatch = new StopWatch();
069
070 stopWatch.start();
071
072 InitUtil.initWithSpring();
073
074 upgrade();
075 verify();
076
077 System.out.println(
078 "\nSuccessfully completed upgrade process in " +
079 (stopWatch.getTime() / Time.SECOND) + " seconds.");
080
081 System.exit(0);
082 }
083 catch (Exception e) {
084 e.printStackTrace();
085
086 System.exit(1);
087 }
088 }
089
090 public static void upgrade() throws Exception {
091
092
093
094 if (_log.isDebugEnabled()) {
095 _log.debug("Disable cache registry");
096 }
097
098 CacheRegistryUtil.setActive(false);
099
100
101
102 int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
103
104 if (buildNumber > ReleaseInfo.getParentBuildNumber()) {
105 StringBundler sb = new StringBundler(6);
106
107 sb.append("Attempting to deploy an older Liferay Portal version. ");
108 sb.append("Current build version is ");
109 sb.append(buildNumber);
110 sb.append(" and attempting to deploy version ");
111 sb.append(ReleaseInfo.getParentBuildNumber());
112 sb.append(".");
113
114 throw new IllegalStateException(sb.toString());
115 }
116 else if (buildNumber < ReleaseInfo.RELEASE_5_2_3_BUILD_NUMBER) {
117 String msg = "You must first upgrade to Liferay Portal 5.2.3";
118
119 System.out.println(msg);
120
121 throw new RuntimeException(msg);
122 }
123
124
125
126 CustomSQLUtil.reloadCustomSQL();
127 SQLTransformer.reloadSQLTransformer();
128
129
130
131 if (_log.isDebugEnabled()) {
132 _log.debug("Update build " + buildNumber);
133 }
134
135 _checkPermissionAlgorithm();
136 _checkReleaseState();
137
138 if (PropsValues.UPGRADE_DATABASE_TRANSACTIONS_DISABLED) {
139 _disableTransactions();
140 }
141
142 try {
143 StartupHelperUtil.upgradeProcess(buildNumber);
144 }
145 catch (Exception e) {
146 _updateReleaseState(ReleaseConstants.STATE_UPGRADE_FAILURE);
147
148 throw e;
149 }
150 finally {
151 if (PropsValues.UPGRADE_DATABASE_TRANSACTIONS_DISABLED) {
152 _enableTransactions();
153 }
154 }
155
156
157
158 if (StartupHelperUtil.isUpgraded()) {
159 if (_log.isDebugEnabled()) {
160 _log.debug("Update company key");
161 }
162
163 _updateCompanyKey();
164 }
165
166
167
168 if (_log.isDebugEnabled()) {
169 _log.debug("Check class names");
170 }
171
172 ClassNameLocalServiceUtil.checkClassNames();
173
174
175
176 if (_log.isDebugEnabled()) {
177 _log.debug("Check resource actions");
178 }
179
180 ResourceActionLocalServiceUtil.checkResourceActions();
181
182
183
184 if (_log.isDebugEnabled()) {
185 _log.debug("Delete temporary images");
186 }
187
188 _deleteTempImages();
189
190
191
192 if (_log.isDebugEnabled()) {
193 _log.debug("Clear cache if upgrade process was run");
194 }
195
196 if (StartupHelperUtil.isUpgraded()) {
197 MultiVMPoolUtil.clear();
198 }
199 }
200
201 public static void verify() throws Exception {
202
203
204
205 Release release = null;
206
207 try {
208 release = ReleaseLocalServiceUtil.getRelease(
209 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
210 }
211 catch (PortalException pe) {
212 release = ReleaseLocalServiceUtil.addRelease(
213 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
214 ReleaseInfo.getParentBuildNumber());
215 }
216
217 _checkReleaseState();
218
219
220
221 if (PropsValues.DATABASE_INDEXES_UPDATE_ON_STARTUP) {
222 StartupHelperUtil.setDropIndexes(true);
223
224 StartupHelperUtil.updateIndexes();
225 }
226 else if (StartupHelperUtil.isUpgraded()) {
227 StartupHelperUtil.updateIndexes();
228 }
229
230
231
232 if (PropsValues.VERIFY_DATABASE_TRANSACTIONS_DISABLED) {
233 _disableTransactions();
234 }
235
236 try {
237 StartupHelperUtil.verifyProcess(release.isVerified());
238 }
239 catch (Exception e) {
240 _updateReleaseState(ReleaseConstants.STATE_VERIFY_FAILURE);
241
242 throw e;
243 }
244 finally {
245 if (PropsValues.VERIFY_DATABASE_TRANSACTIONS_DISABLED) {
246 _enableTransactions();
247 }
248 }
249
250
251
252 if (PropsValues.DATABASE_INDEXES_UPDATE_ON_STARTUP ||
253 StartupHelperUtil.isUpgraded()) {
254
255 StartupHelperUtil.updateIndexes(false);
256 }
257
258
259
260 boolean verified = StartupHelperUtil.isVerified();
261
262 if (release.isVerified()) {
263 verified = true;
264 }
265
266 ReleaseLocalServiceUtil.updateRelease(
267 release.getReleaseId(), ReleaseInfo.getParentBuildNumber(),
268 ReleaseInfo.getBuildDate(), verified);
269
270
271
272 CacheRegistryUtil.setActive(true);
273 }
274
275 private static void _checkPermissionAlgorithm() throws Exception {
276 long count = _getResourceCodesCount();
277
278 if (count == 0) {
279 return;
280 }
281
282 StringBundler sb = new StringBundler(8);
283
284 sb.append("Permission conversion to algorithm 6 has not been ");
285 sb.append("completed. Please complete the conversion prior to ");
286 sb.append("starting the portal. The conversion process is ");
287 sb.append("available in portal versions starting with ");
288 sb.append(ReleaseInfo.RELEASE_5_2_3_BUILD_NUMBER);
289 sb.append(" and prior to ");
290 sb.append(ReleaseInfo.RELEASE_6_2_0_BUILD_NUMBER);
291 sb.append(".");
292
293 throw new IllegalStateException(sb.toString());
294 }
295
296 private static void _checkReleaseState() throws Exception {
297 int state = _getReleaseState();
298
299 if (state == ReleaseConstants.STATE_GOOD) {
300 return;
301 }
302
303 StringBundler sb = new StringBundler(6);
304
305 sb.append("The database contains changes from a previous ");
306 sb.append("upgrade attempt that failed. Please restore the old ");
307 sb.append("database and file system and retry the upgrade. A ");
308 sb.append("patch may be required if the upgrade failed due to a");
309 sb.append(" bug or an unforeseen data permutation that resulted ");
310 sb.append("from a corrupt database.");
311
312 throw new IllegalStateException(sb.toString());
313 }
314
315 private static void _deleteTempImages() throws Exception {
316 DB db = DBFactoryUtil.getDB();
317
318 db.runSQL(_DELETE_TEMP_IMAGES_1);
319 db.runSQL(_DELETE_TEMP_IMAGES_2);
320 }
321
322 private static void _disableTransactions() throws Exception {
323 if (_log.isDebugEnabled()) {
324 _log.debug("Disable transactions");
325 }
326
327 PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = false;
328
329 Field field = ReflectionUtil.getDeclaredField(
330 ServiceBeanAopCacheManager.class, "_annotations");
331
332 field.set(
333 null,
334 new HashMap<MethodInvocation, Annotation[]>() {
335
336 @Override
337 public Annotation[] get(Object key) {
338 return _annotations;
339 }
340
341 private Annotation[] _annotations = new Annotation[] {
342 new Skip() {
343
344 public Class<? extends Annotation> annotationType() {
345 return Skip.class;
346 }
347
348 }
349 };
350
351 }
352 );
353 }
354
355 private static void _enableTransactions() throws Exception {
356 if (_log.isDebugEnabled()) {
357 _log.debug("Enable transactions");
358 }
359
360 PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = GetterUtil.getBoolean(
361 PropsUtil.get(PropsKeys.SPRING_HIBERNATE_SESSION_DELEGATED));
362
363 Field field = ReflectionUtil.getDeclaredField(
364 ServiceBeanAopCacheManager.class, "_annotations");
365
366 field.set(
367 null, new ConcurrentHashMap<MethodInvocation, Annotation[]>());
368 }
369
370 private static int _getReleaseState() throws Exception {
371 Connection con = null;
372 PreparedStatement ps = null;
373 ResultSet rs = null;
374
375 try {
376 con = DataAccess.getConnection();
377
378 ps = con.prepareStatement(
379 "select state_ from Release_ where releaseId = ?");
380
381 ps.setLong(1, ReleaseConstants.DEFAULT_ID);
382
383 rs = ps.executeQuery();
384
385 if (rs.next()) {
386 return rs.getInt("state_");
387 }
388
389 throw new IllegalArgumentException(
390 "No Release exists with the primary key " +
391 ReleaseConstants.DEFAULT_ID);
392 }
393 finally {
394 DataAccess.cleanUp(con, ps, rs);
395 }
396 }
397
398 private static long _getResourceCodesCount() throws Exception {
399 Connection con = null;
400 PreparedStatement ps = null;
401 ResultSet rs = null;
402
403 try {
404 con = DataAccess.getConnection();
405
406 ps = con.prepareStatement("select count(*) from ResourceCode");
407
408 rs = ps.executeQuery();
409
410 if (rs.next()) {
411 int count = rs.getInt(1);
412
413 return count;
414 }
415
416 return 0;
417 }
418 catch (Exception e) {
419 return 0;
420 }
421 finally {
422 DataAccess.cleanUp(con, ps, rs);
423 }
424 }
425
426 private static void _updateCompanyKey() throws Exception {
427 DB db = DBFactoryUtil.getDB();
428
429 db.runSQL("update Company set key_ = null");
430 }
431
432 private static void _updateReleaseState(int state) throws Exception {
433 Connection con = null;
434 PreparedStatement ps = null;
435
436 try {
437 con = DataAccess.getConnection();
438
439 ps = con.prepareStatement(
440 "update Release_ set modifiedDate = ?, state_ = ? where " +
441 "releaseId = ?");
442
443 ps.setDate(1, new Date(System.currentTimeMillis()));
444 ps.setInt(2, state);
445 ps.setLong(3, ReleaseConstants.DEFAULT_ID);
446
447 ps.executeUpdate();
448 }
449 finally {
450 DataAccess.cleanUp(con, ps);
451 }
452 }
453
454 private static final String _DELETE_TEMP_IMAGES_1 =
455 "delete from Image where imageId IN (SELECT articleImageId FROM " +
456 "JournalArticleImage where tempImage = TRUE)";
457
458 private static final String _DELETE_TEMP_IMAGES_2 =
459 "delete from JournalArticleImage where tempImage = TRUE";
460
461 private static Log _log = LogFactoryUtil.getLog(DBUpgrader.class);
462
463 }