001    /**
002     * Copyright (c) 2000-2012 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.staging.StagingAdvicesThreadLocal;
026    import com.liferay.portal.util.PropsUtil;
027    import com.liferay.portal.util.PropsValues;
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 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) {
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 = SearchEngineUtil.isIndexReadOnly();
065    
066                    SearchEngineUtil.setIndexReadOnly(true);
067    
068                    BatchSessionUtil.setEnabled(true);
069                    NotificationThreadLocal.setEnabled(false);
070                    StagingAdvicesThreadLocal.setEnabled(false);
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                            SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
088    
089                            BatchSessionUtil.setEnabled(false);
090                            NotificationThreadLocal.setEnabled(true);
091                            StagingAdvicesThreadLocal.setEnabled(true);
092                            WorkflowThreadLocal.setEnabled(true);
093                    }
094    
095                    return ranVerifyProcess;
096            }
097    
098            private static boolean _verifyProcess(String verifyProcessClassName)
099                    throws VerifyException {
100    
101                    if (_log.isDebugEnabled()) {
102                            _log.debug("Initializing verification " + verifyProcessClassName);
103                    }
104    
105                    try {
106                            VerifyProcess verifyProcess = (VerifyProcess)Class.forName(
107                                    verifyProcessClassName).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 Log _log = LogFactoryUtil.getLog(VerifyProcessUtil.class);
135    
136    }