001    /**
002     * Copyright (c) 2000-present 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.IndexWriterHelperUtil;
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.util.VerifyThreadLocal;
024    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
025    import com.liferay.portal.util.PropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.exportimport.staging.StagingAdvicesThreadLocal;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Alexander Chow
032     * @author Raymond Aug??
033     */
034    public class VerifyProcessUtil {
035    
036            public static boolean verifyProcess(
037                            boolean ranUpgradeProcess, boolean newBuildNumber, boolean verified)
038                    throws VerifyException {
039    
040                    int verifyFrequency = GetterUtil.getInteger(
041                            PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
042    
043                    if ((verifyFrequency == VerifyProcess.ALWAYS) ||
044                            ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
045                            ranUpgradeProcess || newBuildNumber) {
046    
047                            return _verifyProcess(ranUpgradeProcess);
048                    }
049    
050                    return false;
051            }
052    
053            private static boolean _verifyProcess(boolean ranUpgradeProcess)
054                    throws VerifyException {
055    
056                    boolean ranVerifyProcess = false;
057    
058                    if (ranUpgradeProcess && PropsValues.INDEX_ON_UPGRADE) {
059                            PropsUtil.set(PropsKeys.INDEX_ON_STARTUP, Boolean.TRUE.toString());
060    
061                            PropsValues.INDEX_ON_STARTUP = true;
062                    }
063    
064                    boolean tempIndexReadOnly = IndexWriterHelperUtil.isIndexReadOnly();
065    
066                    IndexWriterHelperUtil.setIndexReadOnly(true);
067    
068                    NotificationThreadLocal.setEnabled(false);
069                    StagingAdvicesThreadLocal.setEnabled(false);
070                    VerifyThreadLocal.setVerifyInProgress(true);
071                    WorkflowThreadLocal.setEnabled(false);
072    
073                    try {
074                            String[] verifyProcessClassNames = PropsUtil.getArray(
075                                    PropsKeys.VERIFY_PROCESSES);
076    
077                            for (String verifyProcessClassName : verifyProcessClassNames) {
078                                    boolean tempRanVerifyProcess = _verifyProcess(
079                                            verifyProcessClassName);
080    
081                                    if (tempRanVerifyProcess) {
082                                            ranVerifyProcess = true;
083                                    }
084                            }
085                    }
086                    finally {
087                            IndexWriterHelperUtil.setIndexReadOnly(tempIndexReadOnly);
088                            NotificationThreadLocal.setEnabled(true);
089                            StagingAdvicesThreadLocal.setEnabled(true);
090                            VerifyThreadLocal.setVerifyInProgress(false);
091                            WorkflowThreadLocal.setEnabled(true);
092                    }
093    
094                    return ranVerifyProcess;
095            }
096    
097            private static boolean _verifyProcess(String verifyProcessClassName)
098                    throws VerifyException {
099    
100                    if (_log.isDebugEnabled()) {
101                            _log.debug("Initializing verification " + verifyProcessClassName);
102                    }
103    
104                    try {
105                            Class<?> clazz = Class.forName(verifyProcessClassName);
106    
107                            VerifyProcess verifyProcess = (VerifyProcess)clazz.newInstance();
108    
109                            if (_log.isDebugEnabled()) {
110                                    _log.debug("Running verification " + verifyProcessClassName);
111                            }
112    
113                            verifyProcess.verify();
114    
115                            if (_log.isDebugEnabled()) {
116                                    _log.debug("Finished verification " + verifyProcessClassName);
117                            }
118    
119                            return true;
120                    }
121                    catch (ClassNotFoundException cnfe) {
122                            _log.error(verifyProcessClassName + " cannot be found");
123                    }
124                    catch (IllegalAccessException iae) {
125                            _log.error(verifyProcessClassName + " cannot be accessed");
126                    }
127                    catch (InstantiationException ie) {
128                            _log.error(verifyProcessClassName + " cannot be initiated");
129                    }
130    
131                    return false;
132            }
133    
134            private static final Log _log = LogFactoryUtil.getLog(
135                    VerifyProcessUtil.class);
136    
137    }