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.events;
24  
25  import com.liferay.lock.service.LockServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
29  import com.liferay.portal.kernel.cache.CacheRegistry;
30  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
31  import com.liferay.portal.kernel.events.ActionException;
32  import com.liferay.portal.kernel.events.SimpleAction;
33  import com.liferay.portal.kernel.log.Log;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.messaging.MessageBus;
36  import com.liferay.portal.kernel.messaging.MessageBusUtil;
37  import com.liferay.portal.kernel.messaging.sender.MessageSender;
38  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
39  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
40  import com.liferay.portal.kernel.util.GetterUtil;
41  import com.liferay.portal.kernel.util.InstancePool;
42  import com.liferay.portal.kernel.util.ReleaseInfo;
43  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
44  import com.liferay.portal.model.CompanyConstants;
45  import com.liferay.portal.model.Release;
46  import com.liferay.portal.scheduler.SchedulerEngineProxy;
47  import com.liferay.portal.search.lucene.LuceneUtil;
48  import com.liferay.portal.service.ClassNameLocalServiceUtil;
49  import com.liferay.portal.service.ReleaseLocalServiceUtil;
50  import com.liferay.portal.tools.sql.DBUtil;
51  import com.liferay.portal.upgrade.UpgradeProcess;
52  import com.liferay.portal.util.PropsKeys;
53  import com.liferay.portal.util.PropsUtil;
54  import com.liferay.portal.verify.VerifyProcess;
55  
56  import java.io.IOException;
57  
58  import java.sql.SQLException;
59  
60  import javax.naming.NamingException;
61  
62  /**
63   * <a href="StartupAction.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   * @author Alexander Chow
67   * @author Raymond Augé
68   *
69   */
70  public class StartupAction extends SimpleAction {
71  
72      public void run(String[] ids) throws ActionException {
73          try {
74              doRun(ids);
75          }
76          catch (RuntimeException re) {
77              throw re;
78          }
79          catch (Exception e) {
80              throw new ActionException(e);
81          }
82          finally {
83              LuceneUtil.checkLuceneDir(CompanyConstants.SYSTEM);
84          }
85      }
86  
87      protected void deleteTemporaryImages()
88          throws IOException, NamingException, SQLException {
89  
90          DBUtil dbUtil = DBUtil.getInstance();
91  
92          dbUtil.runSQL(_DELETE_TEMP_IMAGES_1);
93          dbUtil.runSQL(_DELETE_TEMP_IMAGES_2);
94      }
95  
96      protected void doRun(String[] ids) throws PortalException, SystemException {
97  
98          // Print release information
99  
100         System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
101 
102         // Clear locks
103 
104         try {
105             LockServiceUtil.clear();
106         }
107         catch (Exception e) {
108             _log.error(e, e);
109         }
110 
111         // Add shutdown hook
112 
113         Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
114 
115         // Velocity
116 
117         VelocityEngineUtil.init();
118 
119         // Disable database caching before upgrade
120 
121         CacheRegistry.setActive(false);
122 
123         // Upgrade
124 
125         int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
126 
127         if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
128             String msg = "You must first upgrade to Liferay Portal 4.2.1";
129 
130             _log.fatal(msg);
131 
132             throw new RuntimeException(msg);
133         }
134 
135         boolean ranUpgradeProcess = false;
136 
137         String[] upgradeProcesses = PropsUtil.getArray(
138             PropsKeys.UPGRADE_PROCESSES);
139 
140         for (int i = 0; i < upgradeProcesses.length; i++) {
141             if (_log.isDebugEnabled()) {
142                 _log.debug("Initializing upgrade " + upgradeProcesses[i]);
143             }
144 
145             UpgradeProcess upgradeProcess = (UpgradeProcess)InstancePool.get(
146                 upgradeProcesses[i]);
147 
148             if (upgradeProcess == null) {
149                 _log.error(upgradeProcesses[i] + " cannot be found");
150 
151                 continue;
152             }
153 
154             if ((upgradeProcess.getThreshold() == 0) ||
155                 (upgradeProcess.getThreshold() > buildNumber)) {
156 
157                 if (_log.isInfoEnabled()) {
158                     _log.info("Running upgrade " + upgradeProcesses[i]);
159                 }
160 
161                 upgradeProcess.upgrade();
162 
163                 if (_log.isInfoEnabled()) {
164                     _log.info("Finished upgrade " + upgradeProcesses[i]);
165                 }
166 
167                 ranUpgradeProcess = true;
168             }
169             else {
170                 if (_log.isDebugEnabled()) {
171                     _log.debug(
172                         "Upgrade threshold " + upgradeProcess.getThreshold() +
173                             " will not trigger upgrade");
174 
175                     _log.debug("Skipping upgrade " + upgradeProcesses[i]);
176                 }
177             }
178         }
179 
180         // Class names
181 
182         ClassNameLocalServiceUtil.checkClassNames();
183 
184         // Delete temporary images
185 
186         try {
187             deleteTemporaryImages();
188         }
189         catch (Exception e) {
190             _log.error(e, e);
191         }
192 
193         // Update indexes
194 
195         if (ranUpgradeProcess) {
196             try {
197                 DBUtil.getInstance().runSQLTemplate("indexes.sql", false);
198             }
199             catch (Exception e) {
200                 _log.error(e, e);
201             }
202         }
203 
204         // Enable database caching after upgrade
205 
206         CacheRegistry.setActive(true);
207 
208         // Clear the caches only if the upgrade process was run
209 
210         if (ranUpgradeProcess) {
211             MultiVMPoolUtil.clear();
212         }
213 
214         // Messaging
215 
216         MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
217             MessageBus.class.getName());
218         MessageSender messageSender =
219             (MessageSender)PortalBeanLocatorUtil.locate(
220                 MessageSender.class.getName());
221         SynchronousMessageSender synchronousMessageSender =
222             (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
223                 SynchronousMessageSender.class.getName());
224 
225         MessageBusUtil.init(
226             messageBus, messageSender, synchronousMessageSender);
227 
228         // Scheduler
229 
230         SchedulerEngineUtil.init(new SchedulerEngineProxy());
231 
232         SchedulerEngineUtil.start();
233 
234         // Verify
235 
236         Release release = ReleaseLocalServiceUtil.getRelease();
237 
238         int verifyFrequency = GetterUtil.getInteger(
239             PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
240         boolean verified = release.isVerified();
241 
242         if ((verifyFrequency == VerifyProcess.ALWAYS) ||
243             ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
244             (ranUpgradeProcess)) {
245 
246             if (!ranUpgradeProcess) {
247                 PropsUtil.set(PropsKeys.INDEX_ON_STARTUP, "true");
248             }
249 
250             String[] verifyProcesses = PropsUtil.getArray(
251                 PropsKeys.VERIFY_PROCESSES);
252 
253             for (int i = 0; i < verifyProcesses.length; i++) {
254                 if (_log.isDebugEnabled()) {
255                     _log.debug(
256                         "Initializing verification " + verifyProcesses[i]);
257                 }
258 
259                 try {
260                     VerifyProcess verifyProcess = (VerifyProcess)Class.forName(
261                         verifyProcesses[i]).newInstance();
262 
263                     if (_log.isInfoEnabled()) {
264                         _log.info("Running verification " + verifyProcesses[i]);
265                     }
266 
267                     verifyProcess.verify();
268 
269                     if (_log.isInfoEnabled()) {
270                         _log.info(
271                             "Finished verification " + verifyProcesses[i]);
272                     }
273 
274                     verified = true;
275                 }
276                 catch (ClassNotFoundException cnfe) {
277                     _log.error(verifyProcesses[i] + " cannot be found");
278                 }
279                 catch (IllegalAccessException iae) {
280                     _log.error(verifyProcesses[i] + " cannot be accessed");
281                 }
282                 catch (InstantiationException ie) {
283                     _log.error(verifyProcesses[i] + " cannot be initiated");
284                 }
285             }
286         }
287 
288         // Update release
289 
290         ReleaseLocalServiceUtil.updateRelease(verified);
291     }
292 
293     private static final String _DELETE_TEMP_IMAGES_1 =
294         "DELETE FROM Image WHERE imageId IN (SELECT articleImageId FROM " +
295             "JournalArticleImage WHERE tempImage = TRUE)";
296 
297     private static final String _DELETE_TEMP_IMAGES_2 =
298         "DELETE FROM JournalArticleImage where tempImage = TRUE";
299 
300     private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
301 
302 }