1   /**
2    * Copyright (c) 2000-2008 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.events;
24  
25  import com.liferay.lock.service.LockServiceUtil;
26  import com.liferay.portal.kernel.bean.BeanLocatorUtil;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
29  import com.liferay.portal.kernel.events.ActionException;
30  import com.liferay.portal.kernel.events.SimpleAction;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.InstancePool;
33  import com.liferay.portal.kernel.util.ReleaseInfo;
34  import com.liferay.portal.lucene.LuceneUtil;
35  import com.liferay.portal.model.CompanyConstants;
36  import com.liferay.portal.model.Release;
37  import com.liferay.portal.service.ClassNameLocalServiceUtil;
38  import com.liferay.portal.service.ReleaseLocalServiceUtil;
39  import com.liferay.portal.spring.util.SpringUtil;
40  import com.liferay.portal.tools.sql.DBUtil;
41  import com.liferay.portal.upgrade.UpgradeProcess;
42  import com.liferay.portal.util.PropsUtil;
43  import com.liferay.portal.verify.VerifyProcess;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  import org.springframework.context.ApplicationContext;
49  
50  /**
51   * <a href="StartupAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   * @author Alexander Chow
55   *
56   */
57  public class StartupAction extends SimpleAction {
58  
59      public void run(String[] ids) throws ActionException {
60          try {
61  
62              // Print release information
63  
64              System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
65  
66              // Clear locks
67  
68              try {
69                  LockServiceUtil.clear();
70              }
71              catch (Exception e) {
72                  _log.error(e, e);
73              }
74  
75              // Add shutdown hook
76  
77              Runtime.getRuntime().addShutdownHook(
78                  new Thread(new ShutdownHook()));
79  
80              // Preinitialize Spring beans. See LEP-4734.
81  
82              ApplicationContext context = SpringUtil.getContext();
83  
84              String[] beanDefinitionNames = context.getBeanDefinitionNames();
85  
86              for (int i = 0; i < beanDefinitionNames.length; i++) {
87                  String beanDefinitionName = beanDefinitionNames[i];
88  
89                  BeanLocatorUtil.locate(beanDefinitionName, false);
90              }
91  
92              // Disable database caching before upgrade
93  
94              CacheRegistry.setActive(false);
95  
96              // Upgrade
97  
98              int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
99  
100             if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
101                 String msg = "You must first upgrade to Liferay Portal 4.2.1";
102 
103                 _log.fatal(msg);
104 
105                 throw new RuntimeException(msg);
106             }
107 
108             boolean ranUpgradeProcess = false;
109 
110             String[] upgradeProcesses =
111                 PropsUtil.getArray(PropsUtil.UPGRADE_PROCESSES);
112 
113             for (int i = 0; i < upgradeProcesses.length; i++) {
114                 if (_log.isDebugEnabled()) {
115                     _log.debug("Initializing upgrade " + upgradeProcesses[i]);
116                 }
117 
118                 UpgradeProcess upgradeProcess =
119                     (UpgradeProcess)InstancePool.get(upgradeProcesses[i]);
120 
121                 if (upgradeProcess != null) {
122                     if ((upgradeProcess.getThreshold() == 0) ||
123                         (upgradeProcess.getThreshold() > buildNumber)) {
124 
125                         if (_log.isInfoEnabled()) {
126                             _log.info(
127                                 "Running upgrade " + upgradeProcesses[i]);
128                         }
129 
130                         upgradeProcess.upgrade();
131 
132                         if (_log.isInfoEnabled()) {
133                             _log.info(
134                                 "Finished upgrade " + upgradeProcesses[i]);
135                         }
136 
137                         ranUpgradeProcess = true;
138                     }
139                     else {
140                         if (_log.isDebugEnabled()) {
141                             _log.debug(
142                                 "Upgrade threshold " +
143                                     upgradeProcess.getThreshold() +
144                                         " will not trigger upgrade");
145 
146                             _log.debug(
147                                 "Skipping upgrade " + upgradeProcesses[i]);
148                         }
149                     }
150                 }
151                 else {
152                     _log.error(upgradeProcesses[i] + " cannot be found");
153                 }
154             }
155 
156             // Class names
157 
158             ClassNameLocalServiceUtil.checkClassNames();
159 
160             // Delete temporary images
161 
162             deleteTemporaryImages();
163 
164             // Update indexes
165 
166             if (ranUpgradeProcess) {
167                 DBUtil.getInstance().runSQLTemplate("indexes.sql", false);
168             }
169 
170             // Enable database caching after upgrade
171 
172             CacheRegistry.setActive(true);
173 
174             MultiVMPoolUtil.clear();
175 
176             // Verify
177 
178             Release release = ReleaseLocalServiceUtil.getRelease();
179 
180             int verifyFrequency = GetterUtil.getInteger(
181                 PropsUtil.get(PropsUtil.VERIFY_FREQUENCY));
182             boolean verified = release.isVerified();
183 
184             if ((verifyFrequency == VerifyProcess.ALWAYS) ||
185                 ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
186                 (ranUpgradeProcess)) {
187 
188                 String[] verifyProcesses =
189                     PropsUtil.getArray(PropsUtil.VERIFY_PROCESSES);
190 
191                 for (int i = 0; i < verifyProcesses.length; i++) {
192                     if (_log.isDebugEnabled()) {
193                         _log.debug(
194                             "Initializing verification " + verifyProcesses[i]);
195                     }
196 
197                     try {
198                         VerifyProcess verifyProcess =
199                             (VerifyProcess)Class.forName(
200                                 verifyProcesses[i]).newInstance();
201 
202                         if (_log.isInfoEnabled()) {
203                             _log.info(
204                                 "Running verification " + verifyProcesses[i]);
205                         }
206 
207                         verifyProcess.verify();
208 
209                         if (_log.isInfoEnabled()) {
210                             _log.info(
211                                 "Finished verification " + verifyProcesses[i]);
212                         }
213 
214                         verified = true;
215                     }
216                     catch (ClassNotFoundException cnfe) {
217                         _log.error(verifyProcesses[i] + " cannot be found");
218                     }
219                     catch (InstantiationException ie) {
220                         _log.error(verifyProcesses[i] + " cannot be initiated");
221                     }
222                 }
223             }
224 
225             // Update release
226 
227             ReleaseLocalServiceUtil.updateRelease(verified);
228         }
229         catch (RuntimeException re) {
230             throw re;
231         }
232         catch (Exception e) {
233             throw new ActionException(e);
234         }
235         finally {
236 
237             // Lucene
238 
239             LuceneUtil.checkLuceneDir(CompanyConstants.SYSTEM);
240         }
241     }
242 
243     protected void deleteTemporaryImages() throws Exception {
244         DBUtil dbUtil = DBUtil.getInstance();
245 
246         dbUtil.runSQL(_DELETE_TEMP_IMAGES_1);
247         dbUtil.runSQL(_DELETE_TEMP_IMAGES_2);
248     }
249 
250     private static final String _DELETE_TEMP_IMAGES_1 =
251         "DELETE FROM Image WHERE imageId IN (SELECT articleImageId FROM " +
252             "JournalArticleImage WHERE tempImage = TRUE)";
253 
254     private static final String _DELETE_TEMP_IMAGES_2 =
255         "DELETE FROM JournalArticleImage where tempImage = TRUE";
256 
257     private static Log _log = LogFactory.getLog(StartupAction.class);
258 
259 }