1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.verify;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.SearchEngineUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.PropsKeys;
30  import com.liferay.portal.service.persistence.BatchSessionUtil;
31  import com.liferay.portal.util.PropsUtil;
32  
33  /**
34   * <a href="VerifyProcessUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Alexander Chow
38   * @author Raymond Augé
39   */
40  public class VerifyProcessUtil {
41  
42      public static boolean verifyProcess(
43              boolean ranUpgradeProcess, boolean verified)
44          throws VerifyException {
45  
46          boolean ranVerifyProcess = false;
47  
48          int verifyFrequency = GetterUtil.getInteger(
49              PropsUtil.get(PropsKeys.VERIFY_FREQUENCY));
50  
51          if ((verifyFrequency == VerifyProcess.ALWAYS) ||
52              ((verifyFrequency == VerifyProcess.ONCE) && !verified) ||
53              (ranUpgradeProcess)) {
54  
55              if (ranUpgradeProcess) {
56                  PropsUtil.set(
57                      PropsKeys.INDEX_ON_STARTUP, Boolean.TRUE.toString());
58              }
59  
60              String[] verifyProcessClassNames = PropsUtil.getArray(
61                  PropsKeys.VERIFY_PROCESSES);
62  
63              BatchSessionUtil.setEnabled(true);
64  
65              boolean tempIndexReadOnly = SearchEngineUtil.isIndexReadOnly();
66  
67              SearchEngineUtil.setIndexReadOnly(true);
68  
69              try {
70                  for (String verifyProcessClassName : verifyProcessClassNames) {
71                      boolean tempRanVerifyProcess = _verifyProcess(
72                          verifyProcessClassName);
73  
74                      if (tempRanVerifyProcess) {
75                          ranVerifyProcess = true;
76                      }
77                  }
78              }
79              finally {
80                  BatchSessionUtil.setEnabled(false);
81  
82                  SearchEngineUtil.setIndexReadOnly(tempIndexReadOnly);
83              }
84          }
85  
86          return ranVerifyProcess;
87      }
88  
89      private static boolean _verifyProcess(String verifyProcessClassName)
90          throws VerifyException {
91  
92          if (_log.isDebugEnabled()) {
93              _log.debug("Initializing verification " + verifyProcessClassName);
94          }
95  
96          try {
97              VerifyProcess verifyProcess = (VerifyProcess)Class.forName(
98                  verifyProcessClassName).newInstance();
99  
100             if (_log.isDebugEnabled()) {
101                 _log.debug("Running verification " + verifyProcessClassName);
102             }
103 
104             verifyProcess.verify();
105 
106             if (_log.isDebugEnabled()) {
107                 _log.debug("Finished verification " + verifyProcessClassName);
108             }
109 
110             return true;
111         }
112         catch (ClassNotFoundException cnfe) {
113             _log.error(verifyProcessClassName + " cannot be found");
114         }
115         catch (IllegalAccessException iae) {
116             _log.error(verifyProcessClassName + " cannot be accessed");
117         }
118         catch (InstantiationException ie) {
119             _log.error(verifyProcessClassName + " cannot be initiated");
120         }
121 
122         return false;
123     }
124 
125     private static Log _log = LogFactoryUtil.getLog(VerifyProcessUtil.class);
126 
127 }