1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.events.StartupHelperUtil;
26  import com.liferay.portal.kernel.cache.CacheRegistry;
27  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
28  import com.liferay.portal.kernel.util.ReleaseInfo;
29  import com.liferay.portal.kernel.util.Time;
30  import com.liferay.portal.model.Release;
31  import com.liferay.portal.security.permission.ResourceActionsUtil;
32  import com.liferay.portal.service.ClassNameLocalServiceUtil;
33  import com.liferay.portal.service.ReleaseLocalServiceUtil;
34  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
35  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
36  import com.liferay.portal.util.InitUtil;
37  
38  import org.apache.commons.lang.time.StopWatch;
39  
40  /**
41   * <a href="DBUpgrader.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Michael C. Han
44   * @author Brian Wing Shun Chan
45   */
46  public class DBUpgrader {
47  
48      public static void main(String[] args) {
49          try {
50              StopWatch stopWatch = new StopWatch();
51  
52              stopWatch.start();
53  
54              InitUtil.initWithSpring();
55  
56              upgrade();
57              verify();
58  
59              System.out.println(
60                  "\nSuccessfully completed upgrade process in " +
61                      (stopWatch.getTime() / Time.SECOND) + " seconds.");
62  
63              System.exit(0);
64          }
65          catch (Exception e) {
66              e.printStackTrace();
67  
68              System.exit(1);
69          }
70      }
71  
72      public static void upgrade() throws Exception {
73  
74          // Disable database caching before upgrade
75  
76          CacheRegistry.setActive(false);
77  
78          // Upgrade
79  
80          int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
81  
82          if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
83              String msg = "You must first upgrade to Liferay Portal 4.2.1";
84  
85              System.out.println(msg);
86  
87              throw new RuntimeException(msg);
88          }
89  
90          StartupHelperUtil.upgradeProcess(buildNumber);
91  
92          // Class names
93  
94          ClassNameLocalServiceUtil.checkClassNames();
95  
96          // Resource actions
97  
98          ResourceActionsUtil.init();
99  
100         ResourceActionLocalServiceUtil.checkResourceActions();
101 
102         // Resource codes
103 
104         ResourceCodeLocalServiceUtil.checkResourceCodes();
105 
106         // Delete temporary images
107 
108         StartupHelperUtil.deleteTempImages();
109 
110         // Clear the caches only if the upgrade process was run
111 
112         if (StartupHelperUtil.isUpgraded()) {
113             MultiVMPoolUtil.clear();
114         }
115     }
116 
117     public static void verify() throws Exception {
118 
119         // Verify
120 
121         Release release = ReleaseLocalServiceUtil.getRelease();
122 
123         StartupHelperUtil.verifyProcess(release.isVerified());
124 
125         // Update indexes
126 
127         if (StartupHelperUtil.isUpgraded()) {
128             StartupHelperUtil.updateIndexes();
129         }
130 
131         // Update release
132 
133         boolean verified = StartupHelperUtil.isVerified();
134 
135         if (release.isVerified()) {
136             verified = true;
137         }
138 
139         ReleaseLocalServiceUtil.updateRelease(verified);
140 
141         // Enable database caching after verify
142 
143         CacheRegistry.setActive(true);
144     }
145 
146 }