1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.PropsKeys;
22  import com.liferay.portal.service.persistence.BatchSessionUtil;
23  import com.liferay.portal.util.PropsUtil;
24  import com.liferay.portal.util.PropsValues;
25  
26  /**
27   * <a href="VerifyProcessUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   * @author Alexander Chow
31   * @author Raymond Augé
32   */
33  public class VerifyProcessUtil {
34  
35      public static boolean verifyProcess(
36              boolean ranUpgradeProcess, boolean verified)
37          throws VerifyException {
38  
39          boolean ranVerifyProcess = false;
40  
41          int verifyFrequency = GetterUtil.getInteger(
42              PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
43  
44          if ((verifyFrequency == VerifyProcess.ALWAYS) ||
45              ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
46              (ranUpgradeProcess)) {
47  
48              if (ranUpgradeProcess && PropsValues.INDEX_ON_UPGRADE) {
49                  PropsUtil.set(
50                      PropsKeys.INDEX_ON_STARTUP, Boolean.TRUE.toString());
51              }
52  
53              String[] verifyProcessClassNames = PropsUtil.getArray(
54                  PropsKeys.VERIFY_PROCESSES);
55  
56              BatchSessionUtil.setEnabled(true);
57  
58              boolean tempIndexReadOnly = SearchEngineUtil.isIndexReadOnly();
59  
60              SearchEngineUtil.setIndexReadOnly(true);
61  
62              try {
63                  for (String verifyProcessClassName : verifyProcessClassNames) {
64                      boolean tempRanVerifyProcess = _verifyProcess(
65                          verifyProcessClassName);
66  
67                      if (tempRanVerifyProcess) {
68                          ranVerifyProcess = true;
69                      }
70                  }
71              }
72              finally {
73                  BatchSessionUtil.setEnabled(false);
74  
75                  SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
76              }
77          }
78  
79          return ranVerifyProcess;
80      }
81  
82      private static boolean _verifyProcess(String verifyProcessClassName)
83          throws VerifyException {
84  
85          if (_log.isDebugEnabled()) {
86              _log.debug("Initializing verification " + verifyProcessClassName);
87          }
88  
89          try {
90              VerifyProcess verifyProcess = (VerifyProcess)Class.forName(
91                  verifyProcessClassName).newInstance();
92  
93              if (_log.isDebugEnabled()) {
94                  _log.debug("Running verification " + verifyProcessClassName);
95              }
96  
97              verifyProcess.verify();
98  
99              if (_log.isDebugEnabled()) {
100                 _log.debug("Finished verification " + verifyProcessClassName);
101             }
102 
103             return true;
104         }
105         catch (ClassNotFoundException cnfe) {
106             _log.error(verifyProcessClassName + " cannot be found");
107         }
108         catch (IllegalAccessException iae) {
109             _log.error(verifyProcessClassName + " cannot be accessed");
110         }
111         catch (InstantiationException ie) {
112             _log.error(verifyProcessClassName + " cannot be initiated");
113         }
114 
115         return false;
116     }
117 
118     private static Log _log = LogFactoryUtil.getLog(VerifyProcessUtil.class);
119 
120 }