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