1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.events;
21  
22  import com.liferay.lock.service.LockServiceUtil;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
26  import com.liferay.portal.kernel.cache.CacheRegistry;
27  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
28  import com.liferay.portal.kernel.events.ActionException;
29  import com.liferay.portal.kernel.events.SimpleAction;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.messaging.MessageBus;
33  import com.liferay.portal.kernel.messaging.MessageBusUtil;
34  import com.liferay.portal.kernel.messaging.sender.MessageSender;
35  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
36  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
37  import com.liferay.portal.kernel.util.GetterUtil;
38  import com.liferay.portal.kernel.util.InstancePool;
39  import com.liferay.portal.kernel.util.ReleaseInfo;
40  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
41  import com.liferay.portal.model.CompanyConstants;
42  import com.liferay.portal.model.Release;
43  import com.liferay.portal.scheduler.SchedulerEngineProxy;
44  import com.liferay.portal.search.lucene.LuceneUtil;
45  import com.liferay.portal.service.ClassNameLocalServiceUtil;
46  import com.liferay.portal.service.ReleaseLocalServiceUtil;
47  import com.liferay.portal.tools.sql.DBUtil;
48  import com.liferay.portal.upgrade.UpgradeProcess;
49  import com.liferay.portal.util.PropsKeys;
50  import com.liferay.portal.util.PropsUtil;
51  import com.liferay.portal.util.PropsValues;
52  import com.liferay.portal.verify.VerifyProcess;
53  
54  import java.io.IOException;
55  
56  import java.sql.SQLException;
57  
58  import javax.naming.NamingException;
59  
60  /**
61   * <a href="StartupAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   * @author Alexander Chow
65   * @author Raymond Augé
66   *
67   */
68  public class StartupAction extends SimpleAction {
69  
70      public void run(String[] ids) throws ActionException {
71          try {
72              doRun(ids);
73          }
74          catch (RuntimeException re) {
75              throw re;
76          }
77          catch (Exception e) {
78              throw new ActionException(e);
79          }
80          finally {
81              LuceneUtil.checkLuceneDir(CompanyConstants.SYSTEM);
82          }
83      }
84  
85      protected void deleteTemporaryImages()
86          throws IOException, NamingException, SQLException {
87  
88          DBUtil dbUtil = DBUtil.getInstance();
89  
90          dbUtil.runSQL(_DELETE_TEMP_IMAGES_1);
91          dbUtil.runSQL(_DELETE_TEMP_IMAGES_2);
92      }
93  
94      protected void doRun(String[] ids) throws PortalException, SystemException {
95  
96          // Print release information
97  
98          System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
99  
100         // Clear locks
101 
102         try {
103             LockServiceUtil.clear();
104         }
105         catch (Exception e) {
106             _log.error(e, e);
107         }
108 
109         // Add shutdown hook
110 
111         Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
112 
113         // Velocity
114 
115         VelocityEngineUtil.init();
116 
117         // Disable database caching before upgrade
118 
119         CacheRegistry.setActive(false);
120 
121         // Upgrade
122 
123         int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
124 
125         if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
126             String msg = "You must first upgrade to Liferay Portal 4.2.1";
127 
128             _log.fatal(msg);
129 
130             throw new RuntimeException(msg);
131         }
132 
133         boolean ranUpgradeProcess = false;
134 
135         String[] upgradeProcesses = PropsUtil.getArray(
136             PropsKeys.UPGRADE_PROCESSES);
137 
138         for (int i = 0; i < upgradeProcesses.length; i++) {
139             if (_log.isDebugEnabled()) {
140                 _log.debug("Initializing upgrade " + upgradeProcesses[i]);
141             }
142 
143             UpgradeProcess upgradeProcess = (UpgradeProcess)InstancePool.get(
144                 upgradeProcesses[i]);
145 
146             if (upgradeProcess == null) {
147                 _log.error(upgradeProcesses[i] + " cannot be found");
148 
149                 continue;
150             }
151 
152             if ((upgradeProcess.getThreshold() == 0) ||
153                 (upgradeProcess.getThreshold() > buildNumber)) {
154 
155                 if (_log.isInfoEnabled()) {
156                     _log.info("Running upgrade " + upgradeProcesses[i]);
157                 }
158 
159                 upgradeProcess.upgrade();
160 
161                 if (_log.isInfoEnabled()) {
162                     _log.info("Finished upgrade " + upgradeProcesses[i]);
163                 }
164 
165                 ranUpgradeProcess = true;
166             }
167             else {
168                 if (_log.isDebugEnabled()) {
169                     _log.debug(
170                         "Upgrade threshold " + upgradeProcess.getThreshold() +
171                             " will not trigger upgrade");
172 
173                     _log.debug("Skipping upgrade " + upgradeProcesses[i]);
174                 }
175             }
176         }
177 
178         // Class names
179 
180         ClassNameLocalServiceUtil.checkClassNames();
181 
182         // Delete temporary images
183 
184         try {
185             deleteTemporaryImages();
186         }
187         catch (Exception e) {
188             _log.error(e, e);
189         }
190 
191         // Update indexes
192 
193         if (ranUpgradeProcess) {
194             try {
195                 DBUtil.getInstance().runSQLTemplate("indexes.sql", false);
196             }
197             catch (Exception e) {
198                 _log.error(e, e);
199             }
200         }
201 
202         // Enable database caching after upgrade
203 
204         CacheRegistry.setActive(true);
205 
206         // Clear the caches only if the upgrade process was run
207 
208         if (ranUpgradeProcess) {
209             MultiVMPoolUtil.clear();
210         }
211 
212         // Messaging
213 
214         MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
215             MessageBus.class.getName());
216         MessageSender messageSender =
217             (MessageSender)PortalBeanLocatorUtil.locate(
218                 MessageSender.class.getName());
219         SynchronousMessageSender synchronousMessageSender =
220             (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
221                 SynchronousMessageSender.class.getName());
222 
223         MessageBusUtil.init(
224             messageBus, messageSender, synchronousMessageSender);
225 
226         // Scheduler
227 
228         SchedulerEngineUtil.init(new SchedulerEngineProxy());
229 
230         SchedulerEngineUtil.start();
231 
232         // Verify
233 
234         Release release = ReleaseLocalServiceUtil.getRelease();
235 
236         // LPS-1880
237 
238         boolean tempIndexReadOnly = PropsValues.INDEX_READ_ONLY;
239 
240         PropsValues.INDEX_READ_ONLY = true;
241 
242         int verifyFrequency = GetterUtil.getInteger(
243             PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
244         boolean verified = release.isVerified();
245 
246         if ((verifyFrequency == VerifyProcess.ALWAYS) ||
247             ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
248             (ranUpgradeProcess)) {
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         PropsValues.INDEX_READ_ONLY = tempIndexReadOnly;
289 
290         // Update release
291 
292         ReleaseLocalServiceUtil.updateRelease(verified);
293     }
294 
295     private static final String _DELETE_TEMP_IMAGES_1 =
296         "DELETE FROM Image WHERE imageId IN (SELECT articleImageId FROM " +
297             "JournalArticleImage WHERE tempImage = TRUE)";
298 
299     private static final String _DELETE_TEMP_IMAGES_2 =
300         "DELETE FROM JournalArticleImage where tempImage = TRUE";
301 
302     private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
303 
304 }