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.util.InstancePool;
28  
29  /**
30   * <a href="SmartUpgradeSchema.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public abstract class SmartUpgradeSchema extends UpgradeProcess {
36  
37      public void upgrade() throws UpgradeException {
38          String smartUpgradeSchemaImplClassName = this.getClass().getName();
39  
40          if (_alreadyUpgraded) {
41              _log.info(
42                  "Skipping " + smartUpgradeSchemaImplClassName +
43                      " because it has already been executed");
44  
45              return;
46          }
47  
48          _alreadyUpgraded = true;
49  
50          try {
51              upgradeOnce();
52  
53              boolean doUpgrade = false;
54  
55              for (int i = 0; i < _UPGRADE_PROGRESS_CLASSES.length; i++) {
56                  String curUpgradeSchemaClassName =
57                      _UPGRADE_PROGRESS_CLASSES[i].getName();
58  
59                  if (doUpgrade) {
60                      SmartUpgradeSchema upgradeSchema =
61                          (SmartUpgradeSchema)InstancePool.get(
62                              curUpgradeSchemaClassName);
63  
64                      if (!upgradeSchema.isAlreadyUpgraded()) {
65                          _log.info(
66                              "Automatically executing " +
67                                  curUpgradeSchemaClassName);
68  
69                          upgradeSchema.setAlreadyUpgraded(true);
70  
71                          upgradeSchema.upgradeOnce();
72                      }
73                  }
74  
75                  if (smartUpgradeSchemaImplClassName.equals(
76                          curUpgradeSchemaClassName)) {
77  
78                      doUpgrade = true;
79                  }
80              }
81          }
82          catch (Exception e) {
83              throw new UpgradeException(e);
84          }
85      }
86  
87      protected boolean isAlreadyUpgraded() {
88          return _alreadyUpgraded;
89      }
90  
91      protected void setAlreadyUpgraded(boolean alreadyUpgraded) {
92          _alreadyUpgraded = alreadyUpgraded;
93      }
94  
95      protected abstract void upgradeOnce() throws Exception;
96  
97      private static final Class<?>[] _UPGRADE_PROGRESS_CLASSES = new Class[] {
98          com.liferay.portal.upgrade.v4_3_0.UpgradeSchema.class,
99          com.liferay.portal.upgrade.v4_3_1.UpgradeSchema.class,
100         com.liferay.portal.upgrade.v4_3_2.UpgradeSchema.class,
101         com.liferay.portal.upgrade.v4_3_3.UpgradeSchema.class,
102         com.liferay.portal.upgrade.v4_3_4.UpgradeSchema.class,
103         com.liferay.portal.upgrade.v4_4_0.UpgradeSchema.class,
104         com.liferay.portal.upgrade.v5_0_0.UpgradeSchema.class,
105         com.liferay.portal.upgrade.v5_1_0.UpgradeSchema.class,
106         com.liferay.portal.upgrade.v5_1_2.UpgradeSchema.class,
107         com.liferay.portal.upgrade.v5_2_0.UpgradeSchema.class,
108         com.liferay.portal.upgrade.v5_2_1.UpgradeSchema.class,
109         com.liferay.portal.upgrade.v5_2_3.UpgradeSchema.class,
110         com.liferay.portal.upgrade.v5_2_4.UpgradeSchema.class
111     };
112 
113     private static Log _log = LogFactoryUtil.getLog(SmartUpgradeSchema.class);
114 
115     private boolean _alreadyUpgraded;
116 
117 }