001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.SearchEngineUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.NotificationThreadLocal;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
024    import com.liferay.portal.service.persistence.BatchSessionUtil;
025    import com.liferay.portal.util.PropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Alexander Chow
031     * @author Raymond Augé
032     */
033    public class VerifyProcessUtil {
034    
035            public static boolean verifyProcess(
036                            boolean ranUpgradeProcess, boolean verified)
037                    throws VerifyException {
038    
039                    boolean ranVerifyProcess = false;
040    
041                    int verifyFrequency = GetterUtil.getInteger(
042                            PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
043    
044                    if ((verifyFrequency == VerifyProcess.ALWAYS) ||
045                            ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
046                            (ranUpgradeProcess)) {
047    
048                            if (ranUpgradeProcess && PropsValues.INDEX_ON_UPGRADE) {
049                                    PropsUtil.set(
050                                            PropsKeys.INDEX_ON_STARTUP, Boolean.TRUE.toString());
051                            }
052    
053                            String[] verifyProcessClassNames = PropsUtil.getArray(
054                                    PropsKeys.VERIFY_PROCESSES);
055    
056                            BatchSessionUtil.setEnabled(true);
057                            NotificationThreadLocal.setEnabled(false);
058                            WorkflowThreadLocal.setEnabled(false);
059    
060                            boolean tempIndexReadOnly = SearchEngineUtil.isIndexReadOnly();
061    
062                            SearchEngineUtil.setIndexReadOnly(true);
063    
064                            try {
065                                    for (String verifyProcessClassName : verifyProcessClassNames) {
066                                            boolean tempRanVerifyProcess = _verifyProcess(
067                                                    verifyProcessClassName);
068    
069                                            if (tempRanVerifyProcess) {
070                                                    ranVerifyProcess = true;
071                                            }
072                                    }
073                            }
074                            finally {
075                                    BatchSessionUtil.setEnabled(false);
076                                    NotificationThreadLocal.setEnabled(true);
077                                    WorkflowThreadLocal.setEnabled(true);
078    
079                                    SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
080                            }
081                    }
082    
083                    return ranVerifyProcess;
084            }
085    
086            private static boolean _verifyProcess(String verifyProcessClassName)
087                    throws VerifyException {
088    
089                    if (_log.isDebugEnabled()) {
090                            _log.debug("Initializing verification " + verifyProcessClassName);
091                    }
092    
093                    try {
094                            VerifyProcess verifyProcess = (VerifyProcess)Class.forName(
095                                    verifyProcessClassName).newInstance();
096    
097                            if (_log.isDebugEnabled()) {
098                                    _log.debug("Running verification " + verifyProcessClassName);
099                            }
100    
101                            verifyProcess.verify();
102    
103                            if (_log.isDebugEnabled()) {
104                                    _log.debug("Finished verification " + verifyProcessClassName);
105                            }
106    
107                            return true;
108                    }
109                    catch (ClassNotFoundException cnfe) {
110                            _log.error(verifyProcessClassName + " cannot be found");
111                    }
112                    catch (IllegalAccessException iae) {
113                            _log.error(verifyProcessClassName + " cannot be accessed");
114                    }
115                    catch (InstantiationException ie) {
116                            _log.error(verifyProcessClassName + " cannot be initiated");
117                    }
118    
119                    return false;
120            }
121    
122            private static Log _log = LogFactoryUtil.getLog(VerifyProcessUtil.class);
123    
124    }