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.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.spring.aop.Skip;
027 import com.liferay.portal.kernel.util.GetterUtil;
028 import com.liferay.portal.kernel.util.PropsKeys;
029 import com.liferay.portal.kernel.util.ReflectionUtil;
030 import com.liferay.portal.kernel.util.ReleaseInfo;
031 import com.liferay.portal.kernel.util.StringBundler;
032 import com.liferay.portal.kernel.util.Time;
033 import com.liferay.portal.model.Release;
034 import com.liferay.portal.model.ReleaseConstants;
035 import com.liferay.portal.service.ClassNameLocalServiceUtil;
036 import com.liferay.portal.service.ReleaseLocalServiceUtil;
037 import com.liferay.portal.service.ResourceActionLocalServiceUtil;
038 import com.liferay.portal.spring.aop.ServiceBeanAopCacheManager;
039 import com.liferay.portal.spring.aop.ServiceBeanAopCacheManagerUtil;
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(true);
073
074 upgrade();
075 verify();
076
077 System.out.println(
078 "\nCompleted upgrade and verify processes 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 if (_log.isDebugEnabled()) {
127 _log.debug("Update build " + buildNumber);
128 }
129
130 _checkPermissionAlgorithm();
131 _checkReleaseState(_getReleaseState());
132
133 if (PropsValues.UPGRADE_DATABASE_TRANSACTIONS_DISABLED) {
134 _disableTransactions();
135 }
136
137 try {
138 StartupHelperUtil.upgradeProcess(buildNumber);
139 }
140 catch (Exception e) {
141 _updateReleaseState(ReleaseConstants.STATE_UPGRADE_FAILURE);
142
143 throw e;
144 }
145 finally {
146 if (PropsValues.UPGRADE_DATABASE_TRANSACTIONS_DISABLED) {
147 _enableTransactions();
148 }
149 }
150
151
152
153 CustomSQLUtil.reloadCustomSQL();
154 SQLTransformer.reloadSQLTransformer();
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 = ReleaseLocalServiceUtil.fetchRelease(
206 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
207
208 if (release == null) {
209 release = ReleaseLocalServiceUtil.addRelease(
210 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
211 ReleaseInfo.getParentBuildNumber());
212 }
213
214 _checkReleaseState(release.getState());
215
216
217
218 if (PropsValues.DATABASE_INDEXES_UPDATE_ON_STARTUP) {
219 StartupHelperUtil.setDropIndexes(true);
220
221 StartupHelperUtil.updateIndexes();
222 }
223 else if (StartupHelperUtil.isUpgraded()) {
224 StartupHelperUtil.updateIndexes();
225 }
226
227
228
229 if (PropsValues.VERIFY_DATABASE_TRANSACTIONS_DISABLED) {
230 _disableTransactions();
231 }
232
233 boolean newBuildNumber = false;
234
235 if (ReleaseInfo.getBuildNumber() > release.getBuildNumber()) {
236 newBuildNumber = true;
237 }
238
239 try {
240 StartupHelperUtil.verifyProcess(
241 newBuildNumber, release.isVerified());
242 }
243 catch (Exception e) {
244 _updateReleaseState(ReleaseConstants.STATE_VERIFY_FAILURE);
245
246 throw e;
247 }
248 finally {
249 if (PropsValues.VERIFY_DATABASE_TRANSACTIONS_DISABLED) {
250 _enableTransactions();
251 }
252 }
253
254
255
256 if (PropsValues.DATABASE_INDEXES_UPDATE_ON_STARTUP ||
257 StartupHelperUtil.isUpgraded()) {
258
259 StartupHelperUtil.updateIndexes(false);
260 }
261
262
263
264 boolean verified = StartupHelperUtil.isVerified();
265
266 if (release.isVerified()) {
267 verified = true;
268 }
269
270 ReleaseLocalServiceUtil.updateRelease(
271 release.getReleaseId(), ReleaseInfo.getParentBuildNumber(),
272 ReleaseInfo.getBuildDate(), verified);
273
274
275
276 CacheRegistryUtil.setActive(true);
277 }
278
279 private static void _checkPermissionAlgorithm() throws Exception {
280 long count = _getResourceCodesCount();
281
282 if (count == 0) {
283 return;
284 }
285
286 StringBundler sb = new StringBundler(8);
287
288 sb.append("Permission conversion to algorithm 6 has not been ");
289 sb.append("completed. Please complete the conversion prior to ");
290 sb.append("starting the portal. The conversion process is ");
291 sb.append("available in portal versions starting with ");
292 sb.append(ReleaseInfo.RELEASE_5_2_3_BUILD_NUMBER);
293 sb.append(" and prior to ");
294 sb.append(ReleaseInfo.RELEASE_6_2_0_BUILD_NUMBER);
295 sb.append(".");
296
297 throw new IllegalStateException(sb.toString());
298 }
299
300 private static void _checkReleaseState(int state) throws Exception {
301 if (state == ReleaseConstants.STATE_GOOD) {
302 return;
303 }
304
305 StringBundler sb = new StringBundler(6);
306
307 sb.append("The database contains changes from a previous ");
308 sb.append("upgrade attempt that failed. Please restore the old ");
309 sb.append("database and file system and retry the upgrade. A ");
310 sb.append("patch may be required if the upgrade failed due to a");
311 sb.append(" bug or an unforeseen data permutation that resulted ");
312 sb.append("from a corrupt database.");
313
314 throw new IllegalStateException(sb.toString());
315 }
316
317 private static void _deleteTempImages() throws Exception {
318 DB db = DBFactoryUtil.getDB();
319
320 db.runSQL(_DELETE_TEMP_IMAGES_1);
321 db.runSQL(_DELETE_TEMP_IMAGES_2);
322 }
323
324 private static void _disableTransactions() throws Exception {
325 if (_log.isDebugEnabled()) {
326 _log.debug("Disable transactions");
327 }
328
329 PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = false;
330
331 Field field = ReflectionUtil.getDeclaredField(
332 ServiceBeanAopCacheManager.class, "_annotations");
333
334 field.set(
335 null,
336 new HashMap<MethodInvocation, Annotation[]>() {
337
338 @Override
339 public Annotation[] get(Object key) {
340 return _annotations;
341 }
342
343 private Annotation[] _annotations = new Annotation[] {
344 new Skip() {
345
346 @Override
347 public Class<? extends Annotation> annotationType() {
348 return Skip.class;
349 }
350
351 }
352 };
353
354 }
355 );
356 }
357
358 private static void _enableTransactions() throws Exception {
359 if (_log.isDebugEnabled()) {
360 _log.debug("Enable transactions");
361 }
362
363 PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = GetterUtil.getBoolean(
364 PropsUtil.get(PropsKeys.SPRING_HIBERNATE_SESSION_DELEGATED));
365
366 Field field = ReflectionUtil.getDeclaredField(
367 ServiceBeanAopCacheManager.class, "_annotations");
368
369 field.set(
370 null, new ConcurrentHashMap<MethodInvocation, Annotation[]>());
371
372 ServiceBeanAopCacheManagerUtil.reset();
373 }
374
375 private static int _getReleaseState() throws Exception {
376 Connection con = null;
377 PreparedStatement ps = null;
378 ResultSet rs = null;
379
380 try {
381 con = DataAccess.getConnection();
382
383 ps = con.prepareStatement(
384 "select state_ from Release_ where releaseId = ?");
385
386 ps.setLong(1, ReleaseConstants.DEFAULT_ID);
387
388 rs = ps.executeQuery();
389
390 if (rs.next()) {
391 return rs.getInt("state_");
392 }
393
394 throw new IllegalArgumentException(
395 "No Release exists with the primary key " +
396 ReleaseConstants.DEFAULT_ID);
397 }
398 finally {
399 DataAccess.cleanUp(con, ps, rs);
400 }
401 }
402
403 private static long _getResourceCodesCount() throws Exception {
404 Connection con = null;
405 PreparedStatement ps = null;
406 ResultSet rs = null;
407
408 try {
409 con = DataAccess.getConnection();
410
411 ps = con.prepareStatement("select count(*) from ResourceCode");
412
413 rs = ps.executeQuery();
414
415 if (rs.next()) {
416 int count = rs.getInt(1);
417
418 return count;
419 }
420
421 return 0;
422 }
423 catch (Exception e) {
424 return 0;
425 }
426 finally {
427 DataAccess.cleanUp(con, ps, rs);
428 }
429 }
430
431 private static void _updateCompanyKey() throws Exception {
432 DB db = DBFactoryUtil.getDB();
433
434 db.runSQL("update Company set key_ = null");
435 }
436
437 private static void _updateReleaseState(int state) throws Exception {
438 Connection con = null;
439 PreparedStatement ps = null;
440
441 try {
442 con = DataAccess.getConnection();
443
444 ps = con.prepareStatement(
445 "update Release_ set modifiedDate = ?, state_ = ? where " +
446 "releaseId = ?");
447
448 ps.setDate(1, new Date(System.currentTimeMillis()));
449 ps.setInt(2, state);
450 ps.setLong(3, ReleaseConstants.DEFAULT_ID);
451
452 ps.executeUpdate();
453 }
454 finally {
455 DataAccess.cleanUp(con, ps);
456 }
457 }
458
459 private static final String _DELETE_TEMP_IMAGES_1 =
460 "delete from Image where imageId IN (SELECT articleImageId FROM " +
461 "JournalArticleImage where tempImage = TRUE)";
462
463 private static final String _DELETE_TEMP_IMAGES_2 =
464 "delete from JournalArticleImage where tempImage = TRUE";
465
466 private static final Log _log = LogFactoryUtil.getLog(DBUpgrader.class);
467
468 }