001    /**
002     * Copyright (c) 2000-2012 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.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                    int verifyFrequency = GetterUtil.getInteger(
040                            PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
041    
042                    if ((verifyFrequency == VerifyProcess.ALWAYS) ||
043                            ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
044                            (ranUpgradeProcess)) {
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                    boolean tempIndexOnStartUp = PropsValues.INDEX_ON_STARTUP;
058    
059                    if (ranUpgradeProcess && PropsValues.INDEX_ON_UPGRADE) {
060                            PropsUtil.set(PropsKeys.INDEX_ON_STARTUP, Boolean.TRUE.toString());
061    
062                            PropsValues.INDEX_ON_STARTUP = true;
063                    }
064    
065                    boolean tempIndexReadOnly = SearchEngineUtil.isIndexReadOnly();
066    
067                    SearchEngineUtil.setIndexReadOnly(true);
068    
069                    BatchSessionUtil.setEnabled(true);
070                    NotificationThreadLocal.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                            PropsUtil.set(
088                                    PropsKeys.INDEX_ON_STARTUP, String.valueOf(tempIndexOnStartUp));
089    
090                            PropsValues.INDEX_ON_STARTUP = tempIndexOnStartUp;
091    
092                            SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
093    
094                            BatchSessionUtil.setEnabled(false);
095                            NotificationThreadLocal.setEnabled(true);
096                            WorkflowThreadLocal.setEnabled(true);
097                    }
098    
099                    return ranVerifyProcess;
100            }
101    
102            private static boolean _verifyProcess(String verifyProcessClassName)
103                    throws VerifyException {
104    
105                    if (_log.isDebugEnabled()) {
106                            _log.debug("Initializing verification " + verifyProcessClassName);
107                    }
108    
109                    try {
110                            VerifyProcess verifyProcess = (VerifyProcess)Class.forName(
111                                    verifyProcessClassName).newInstance();
112    
113                            if (_log.isDebugEnabled()) {
114                                    _log.debug("Running verification " + verifyProcessClassName);
115                            }
116    
117                            verifyProcess.verify();
118    
119                            if (_log.isDebugEnabled()) {
120                                    _log.debug("Finished verification " + verifyProcessClassName);
121                            }
122    
123                            return true;
124                    }
125                    catch (ClassNotFoundException cnfe) {
126                            _log.error(verifyProcessClassName + " cannot be found");
127                    }
128                    catch (IllegalAccessException iae) {
129                            _log.error(verifyProcessClassName + " cannot be accessed");
130                    }
131                    catch (InstantiationException ie) {
132                            _log.error(verifyProcessClassName + " cannot be initiated");
133                    }
134    
135                    return false;
136            }
137    
138            private static Log _log = LogFactoryUtil.getLog(VerifyProcessUtil.class);
139    
140    }