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.upgrade;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.upgrade.UpgradeException;
28  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
29  
30  /**
31   * <a href="UpgradeProcessUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Alexander Chow
35   * @author Raymond Augé
36   */
37  public class UpgradeProcessUtil {
38  
39      public static boolean upgradeProcess(
40              int buildNumber, String[] upgradeProcessClassNames,
41              ClassLoader classLoader)
42          throws UpgradeException {
43  
44          boolean ranUpgradeProcess = false;
45  
46          for (String upgradeProcessClassName : upgradeProcessClassNames) {
47              boolean tempRanUpgradeProcess = _upgradeProcess(
48                  buildNumber, upgradeProcessClassName, classLoader);
49  
50              if (tempRanUpgradeProcess) {
51                  ranUpgradeProcess = true;
52              }
53          }
54  
55          return ranUpgradeProcess;
56      }
57  
58      private static boolean _upgradeProcess(
59              int buildNumber, String upgradeProcessClassName,
60              ClassLoader classLoader)
61          throws UpgradeException {
62  
63          if (_log.isDebugEnabled()) {
64              _log.debug("Initializing upgrade " + upgradeProcessClassName);
65          }
66  
67          UpgradeProcess upgradeProcess = null;
68  
69          try {
70              upgradeProcess = (UpgradeProcess)classLoader.loadClass(
71                  upgradeProcessClassName).newInstance();
72          }
73          catch (Exception e) {
74              _log.error(e, e);
75          }
76  
77          if (upgradeProcess == null) {
78              _log.error(upgradeProcessClassName + " cannot be found");
79  
80              return false;
81          }
82  
83          if ((upgradeProcess.getThreshold() == 0) ||
84              (upgradeProcess.getThreshold() > buildNumber)) {
85  
86              if (_log.isDebugEnabled()) {
87                  _log.debug("Running upgrade " + upgradeProcessClassName);
88              }
89  
90              upgradeProcess.upgrade();
91  
92              if (_log.isDebugEnabled()) {
93                  _log.debug("Finished upgrade " + upgradeProcessClassName);
94              }
95  
96              return true;
97          }
98          else {
99              if (_log.isDebugEnabled()) {
100                 _log.debug(
101                     "Upgrade threshold " + upgradeProcess.getThreshold() +
102                         " will not trigger upgrade");
103 
104                 _log.debug("Skipping upgrade " + upgradeProcessClassName);
105             }
106 
107             return false;
108         }
109     }
110 
111     private static Log _log = LogFactoryUtil.getLog(UpgradeProcessUtil.class);
112 
113 }