1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.search.SearchEngineUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.NotificationThreadLocal;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
24  import com.liferay.portal.service.persistence.BatchSessionUtil;
25  import com.liferay.portal.util.PropsUtil;
26  import com.liferay.portal.util.PropsValues;
27  
28  /**
29   * <a href="VerifyProcessUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   * @author Alexander Chow
33   * @author Raymond Augé
34   */
35  public class VerifyProcessUtil {
36  
37      public static boolean verifyProcess(
38              boolean ranUpgradeProcess, boolean verified)
39          throws VerifyException {
40  
41          boolean ranVerifyProcess = false;
42  
43          int verifyFrequency = GetterUtil.getInteger(
44              PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
45  
46          if ((verifyFrequency == VerifyProcess.ALWAYS) ||
47              ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
48              (ranUpgradeProcess)) {
49  
50              if (ranUpgradeProcess && PropsValues.INDEX_ON_UPGRADE) {
51                  PropsUtil.set(
52                      PropsKeys.INDEX_ON_STARTUP, Boolean.TRUE.toString());
53              }
54  
55              String[] verifyProcessClassNames = PropsUtil.getArray(
56                  PropsKeys.VERIFY_PROCESSES);
57  
58              BatchSessionUtil.setEnabled(true);
59              NotificationThreadLocal.setEnabled(false);
60              WorkflowThreadLocal.setEnabled(false);
61  
62              boolean tempIndexReadOnly = SearchEngineUtil.isIndexReadOnly();
63  
64              SearchEngineUtil.setIndexReadOnly(true);
65  
66              try {
67                  for (String verifyProcessClassName : verifyProcessClassNames) {
68                      boolean tempRanVerifyProcess = _verifyProcess(
69                          verifyProcessClassName);
70  
71                      if (tempRanVerifyProcess) {
72                          ranVerifyProcess = true;
73                      }
74                  }
75              }
76              finally {
77                  BatchSessionUtil.setEnabled(false);
78                  NotificationThreadLocal.setEnabled(true);
79                  WorkflowThreadLocal.setEnabled(true);
80  
81                  SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
82              }
83          }
84  
85          return ranVerifyProcess;
86      }
87  
88      private static boolean _verifyProcess(String verifyProcessClassName)
89          throws VerifyException {
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Initializing verification " + verifyProcessClassName);
93          }
94  
95          try {
96              VerifyProcess verifyProcess = (VerifyProcess)Class.forName(
97                  verifyProcessClassName).newInstance();
98  
99              if (_log.isDebugEnabled()) {
100                 _log.debug("Running verification " + verifyProcessClassName);
101             }
102 
103             verifyProcess.verify();
104 
105             if (_log.isDebugEnabled()) {
106                 _log.debug("Finished verification " + verifyProcessClassName);
107             }
108 
109             return true;
110         }
111         catch (ClassNotFoundException cnfe) {
112             _log.error(verifyProcessClassName + " cannot be found");
113         }
114         catch (IllegalAccessException iae) {
115             _log.error(verifyProcessClassName + " cannot be accessed");
116         }
117         catch (InstantiationException ie) {
118             _log.error(verifyProcessClassName + " cannot be initiated");
119         }
120 
121         return false;
122     }
123 
124     private static Log _log = LogFactoryUtil.getLog(VerifyProcessUtil.class);
125 
126 }