001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.events;
022:
023: import com.liferay.lock.service.LockServiceUtil;
024: import com.liferay.portal.kernel.bean.BeanLocatorUtil;
025: import com.liferay.portal.kernel.cache.CacheRegistry;
026: import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
027: import com.liferay.portal.kernel.events.ActionException;
028: import com.liferay.portal.kernel.events.SimpleAction;
029: import com.liferay.portal.kernel.jndi.PortalJNDIUtil;
030: import com.liferay.portal.kernel.util.GetterUtil;
031: import com.liferay.portal.kernel.util.InstancePool;
032: import com.liferay.portal.kernel.util.ReleaseInfo;
033: import com.liferay.portal.lucene.LuceneUtil;
034: import com.liferay.portal.model.Release;
035: import com.liferay.portal.model.impl.CompanyImpl;
036: import com.liferay.portal.service.ClassNameLocalServiceUtil;
037: import com.liferay.portal.service.ReleaseLocalServiceUtil;
038: import com.liferay.portal.spring.util.SpringUtil;
039: import com.liferay.portal.tools.sql.DBUtil;
040: import com.liferay.portal.upgrade.UpgradeProcess;
041: import com.liferay.portal.util.PropsUtil;
042: import com.liferay.portal.verify.VerifyProcess;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: import org.springframework.context.ApplicationContext;
048:
049: /**
050: * <a href="StartupAction.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: * @author Alexander Chow
054: *
055: */
056: public class StartupAction extends SimpleAction {
057:
058: public void run(String[] ids) throws ActionException {
059: try {
060:
061: // Print release information
062:
063: System.out.println("Starting "
064: + ReleaseInfo.getReleaseInfo());
065:
066: // Clear locks
067:
068: try {
069: LockServiceUtil.clear();
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073:
074: // Add shutdown hook
075:
076: Runtime.getRuntime().addShutdownHook(
077: new Thread(new ShutdownHook()));
078:
079: // Preinitialize Spring beans. See LEP-4734.
080:
081: ApplicationContext context = SpringUtil.getContext();
082:
083: String[] beanDefinitionNames = context
084: .getBeanDefinitionNames();
085:
086: for (int i = 0; i < beanDefinitionNames.length; i++) {
087: String beanDefinitionName = beanDefinitionNames[i];
088:
089: BeanLocatorUtil.locate(beanDefinitionName, false);
090: }
091:
092: // JNDI
093:
094: try {
095: PortalJNDIUtil.getDataSource();
096: } catch (Exception e) {
097: _log.error(e, e);
098: }
099:
100: try {
101: PortalJNDIUtil.getMailSession();
102: } catch (Exception e) {
103: if (_log.isWarnEnabled()) {
104: _log.warn(e.getMessage());
105: }
106: }
107:
108: // Disable database caching before upgrade
109:
110: CacheRegistry.setActive(false);
111:
112: // Upgrade
113:
114: int buildNumber = ReleaseLocalServiceUtil
115: .getBuildNumberOrCreate();
116:
117: if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
118: String msg = "You must first upgrade to Liferay Portal 4.2.1";
119:
120: _log.fatal(msg);
121:
122: throw new RuntimeException(msg);
123: }
124:
125: boolean ranUpgradeProcess = false;
126:
127: String[] upgradeProcesses = PropsUtil
128: .getArray(PropsUtil.UPGRADE_PROCESSES);
129:
130: for (int i = 0; i < upgradeProcesses.length; i++) {
131: if (_log.isDebugEnabled()) {
132: _log.debug("Initializing upgrade "
133: + upgradeProcesses[i]);
134: }
135:
136: UpgradeProcess upgradeProcess = (UpgradeProcess) InstancePool
137: .get(upgradeProcesses[i]);
138:
139: if (upgradeProcess != null) {
140: if ((upgradeProcess.getThreshold() == 0)
141: || (upgradeProcess.getThreshold() > buildNumber)) {
142:
143: if (_log.isInfoEnabled()) {
144: _log.info("Running upgrade "
145: + upgradeProcesses[i]);
146: }
147:
148: upgradeProcess.upgrade();
149:
150: if (_log.isInfoEnabled()) {
151: _log.info("Finished upgrade "
152: + upgradeProcesses[i]);
153: }
154:
155: ranUpgradeProcess = true;
156: } else {
157: if (_log.isDebugEnabled()) {
158: _log.debug("Upgrade threshold "
159: + upgradeProcess.getThreshold()
160: + " will not trigger upgrade");
161:
162: _log.debug("Skipping upgrade "
163: + upgradeProcesses[i]);
164: }
165: }
166: } else {
167: _log
168: .error(upgradeProcesses[i]
169: + " cannot be found");
170: }
171: }
172:
173: // Class names
174:
175: ClassNameLocalServiceUtil.checkClassNames();
176:
177: // Delete temporary images
178:
179: deleteTemporaryImages();
180:
181: // Update indexes
182:
183: if (ranUpgradeProcess) {
184: DBUtil.getInstance().runSQLTemplate("indexes.sql",
185: false);
186: }
187:
188: // Enable database caching after upgrade
189:
190: CacheRegistry.setActive(true);
191:
192: MultiVMPoolUtil.clear();
193:
194: // Verify
195:
196: Release release = ReleaseLocalServiceUtil.getRelease();
197:
198: int verifyFrequency = GetterUtil.getInteger(PropsUtil
199: .get(PropsUtil.VERIFY_FREQUENCY));
200: boolean verified = release.isVerified();
201:
202: if ((verifyFrequency == VerifyProcess.ALWAYS)
203: || ((verifyFrequency == VerifyProcess.ONCE) && !verified)
204: || (ranUpgradeProcess)) {
205:
206: String[] verifyProcesses = PropsUtil
207: .getArray(PropsUtil.VERIFY_PROCESSES);
208:
209: for (int i = 0; i < verifyProcesses.length; i++) {
210: if (_log.isDebugEnabled()) {
211: _log.debug("Initializing verification "
212: + verifyProcesses[i]);
213: }
214:
215: try {
216: VerifyProcess verifyProcess = (VerifyProcess) Class
217: .forName(verifyProcesses[i])
218: .newInstance();
219:
220: if (_log.isInfoEnabled()) {
221: _log.info("Running verification "
222: + verifyProcesses[i]);
223: }
224:
225: verifyProcess.verify();
226:
227: if (_log.isInfoEnabled()) {
228: _log.info("Finished verification "
229: + verifyProcesses[i]);
230: }
231:
232: verified = true;
233: } catch (ClassNotFoundException cnfe) {
234: _log.error(verifyProcesses[i]
235: + " cannot be found");
236: } catch (InstantiationException ie) {
237: _log.error(verifyProcesses[i]
238: + " cannot be initiated");
239: }
240: }
241: }
242:
243: // Update release
244:
245: ReleaseLocalServiceUtil.updateRelease(verified);
246:
247: // Lucene
248:
249: LuceneUtil.checkLuceneDir(CompanyImpl.SYSTEM);
250: } catch (RuntimeException re) {
251: throw re;
252: } catch (Exception e) {
253: throw new ActionException(e);
254: }
255: }
256:
257: protected void deleteTemporaryImages() throws Exception {
258: DBUtil dbUtil = DBUtil.getInstance();
259:
260: dbUtil.runSQL(_DELETE_TEMP_IMAGES_1);
261: dbUtil.runSQL(_DELETE_TEMP_IMAGES_2);
262: }
263:
264: private static final String _DELETE_TEMP_IMAGES_1 = "DELETE FROM Image WHERE imageId IN (SELECT articleImageId FROM "
265: + "JournalArticleImage WHERE tempImage = TRUE)";
266:
267: private static final String _DELETE_TEMP_IMAGES_2 = "DELETE FROM JournalArticleImage where tempImage = TRUE";
268:
269: private static Log _log = LogFactory.getLog(StartupAction.class);
270:
271: }
|