001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.staging.StagingAdvicesThreadLocal;
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 newBuildNumber, boolean verified)
037                    throws VerifyException {
038    
039                    int verifyFrequency = GetterUtil.getInteger(
040                            PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
041    
042                    if ((verifyFrequency == VerifyProcess.ALWAYS) ||
043                            ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
044                            ranUpgradeProcess || newBuildNumber) {
045    
046                            return _verifyProcess(ranUpgradeProcess);
047                    }
048    
049                    return false;
050            }
051    
052            private static boolean _verifyProcess(boolean ranUpgradeProcess)
053                    throws VerifyException {
054    
055                    boolean ranVerifyProcess = false;
056    
057                    if (ranUpgradeProcess && PropsValues.INDEX_ON_UPGRADE) {
058                            PropsUtil.set(PropsKeys.INDEX_ON_STARTUP, Boolean.TRUE.toString());
059    
060                            PropsValues.INDEX_ON_STARTUP = true;
061                    }
062    
063                    boolean tempIndexReadOnly = SearchEngineUtil.isIndexReadOnly();
064    
065                    SearchEngineUtil.setIndexReadOnly(true);
066    
067                    NotificationThreadLocal.setEnabled(false);
068                    StagingAdvicesThreadLocal.setEnabled(false);
069                    WorkflowThreadLocal.setEnabled(false);
070    
071                    try {
072                            String[] verifyProcessClassNames = PropsUtil.getArray(
073                                    PropsKeys.VERIFY_PROCESSES);
074    
075                            for (String verifyProcessClassName : verifyProcessClassNames) {
076                                    boolean tempRanVerifyProcess = _verifyProcess(
077                                            verifyProcessClassName);
078    
079                                    if (tempRanVerifyProcess) {
080                                            ranVerifyProcess = true;
081                                    }
082                            }
083                    }
084                    finally {
085                            SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
086    
087                            NotificationThreadLocal.setEnabled(true);
088                            StagingAdvicesThreadLocal.setEnabled(true);
089                            WorkflowThreadLocal.setEnabled(true);
090                    }
091    
092                    return ranVerifyProcess;
093            }
094    
095            private static boolean _verifyProcess(String verifyProcessClassName)
096                    throws VerifyException {
097    
098                    if (_log.isDebugEnabled()) {
099                            _log.debug("Initializing verification " + verifyProcessClassName);
100                    }
101    
102                    try {
103                            Class<?> clazz = Class.forName(verifyProcessClassName);
104    
105                            VerifyProcess verifyProcess = (VerifyProcess)clazz.newInstance();
106    
107                            if (_log.isDebugEnabled()) {
108                                    _log.debug("Running verification " + verifyProcessClassName);
109                            }
110    
111                            verifyProcess.verify();
112    
113                            if (_log.isDebugEnabled()) {
114                                    _log.debug("Finished verification " + verifyProcessClassName);
115                            }
116    
117                            return true;
118                    }
119                    catch (ClassNotFoundException cnfe) {
120                            _log.error(verifyProcessClassName + " cannot be found");
121                    }
122                    catch (IllegalAccessException iae) {
123                            _log.error(verifyProcessClassName + " cannot be accessed");
124                    }
125                    catch (InstantiationException ie) {
126                            _log.error(verifyProcessClassName + " cannot be initiated");
127                    }
128    
129                    return false;
130            }
131    
132            private static Log _log = LogFactoryUtil.getLog(VerifyProcessUtil.class);
133    
134    }