1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.events;
21  
22  import com.liferay.lock.service.LockServiceUtil;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
26  import com.liferay.portal.kernel.cache.CacheRegistry;
27  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
28  import com.liferay.portal.kernel.events.ActionException;
29  import com.liferay.portal.kernel.events.SimpleAction;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.messaging.MessageBus;
33  import com.liferay.portal.kernel.messaging.MessageBusUtil;
34  import com.liferay.portal.kernel.messaging.sender.MessageSender;
35  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
36  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
37  import com.liferay.portal.kernel.util.ReleaseInfo;
38  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
39  import com.liferay.portal.model.CompanyConstants;
40  import com.liferay.portal.model.Release;
41  import com.liferay.portal.scheduler.SchedulerEngineProxy;
42  import com.liferay.portal.search.lucene.LuceneUtil;
43  import com.liferay.portal.security.lang.PortalSecurityManager;
44  import com.liferay.portal.security.permission.ResourceActionsUtil;
45  import com.liferay.portal.service.ClassNameLocalServiceUtil;
46  import com.liferay.portal.service.ReleaseLocalServiceUtil;
47  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
48  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
49  import com.liferay.portal.util.PropsValues;
50  
51  /**
52   * <a href="StartupAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Alexander Chow
56   * @author Raymond Augé
57   *
58   */
59  public class StartupAction extends SimpleAction {
60  
61      public void run(String[] ids) throws ActionException {
62          try {
63              doRun(ids);
64          }
65          catch (RuntimeException re) {
66              throw re;
67          }
68          catch (Exception e) {
69              throw new ActionException(e);
70          }
71          finally {
72              LuceneUtil.checkLuceneDir(CompanyConstants.SYSTEM);
73          }
74      }
75  
76      protected void doRun(String[] ids) throws PortalException, SystemException {
77  
78          // Print release information
79  
80          System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
81  
82          // Clear locks
83  
84          try {
85              LockServiceUtil.clear();
86          }
87          catch (Exception e) {
88              _log.error(e, e);
89          }
90  
91          // Shutdown hook
92  
93          Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
94  
95          // Security manager
96  
97          if ((System.getSecurityManager() == null) &&
98              (PropsValues.PORTAL_SECURITY_MANAGER_ENABLE)) {
99  
100             System.setSecurityManager(new PortalSecurityManager());
101         }
102 
103         // Velocity
104 
105         VelocityEngineUtil.init();
106 
107         // Disable database caching before upgrade
108 
109         CacheRegistry.setActive(false);
110 
111         // Upgrade
112 
113         int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
114 
115         if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
116             String msg = "You must first upgrade to Liferay Portal 4.2.1";
117 
118             _log.fatal(msg);
119 
120             throw new RuntimeException(msg);
121         }
122 
123         StartupHelperUtil.upgradeProcess(buildNumber);
124 
125         // Class names
126 
127         ClassNameLocalServiceUtil.checkClassNames();
128 
129         // Resource actions
130 
131         ResourceActionsUtil.init();
132 
133         ResourceActionLocalServiceUtil.checkResourceActions();
134 
135         // Resource codes
136 
137         ResourceCodeLocalServiceUtil.checkResourceCodes();
138 
139         // Delete temporary images
140 
141         StartupHelperUtil.deleteTempImages();
142 
143         // Clear the caches only if the upgrade process was run
144 
145         if (StartupHelperUtil.isUpgraded()) {
146             MultiVMPoolUtil.clear();
147         }
148 
149         // Messaging
150 
151         MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
152             MessageBus.class.getName());
153         MessageSender messageSender =
154             (MessageSender)PortalBeanLocatorUtil.locate(
155                 MessageSender.class.getName());
156         SynchronousMessageSender synchronousMessageSender =
157             (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
158                 SynchronousMessageSender.class.getName());
159 
160         MessageBusUtil.init(
161             messageBus, messageSender, synchronousMessageSender);
162 
163         // Scheduler
164 
165         SchedulerEngineUtil.init(new SchedulerEngineProxy());
166 
167         SchedulerEngineUtil.start();
168 
169         // Verify
170 
171         Release release = ReleaseLocalServiceUtil.getRelease();
172 
173         StartupHelperUtil.verifyProcess(release.isVerified());
174 
175         // Update indexes
176 
177         if (StartupHelperUtil.isUpgraded()) {
178             StartupHelperUtil.updateIndexes();
179         }
180 
181         // Update release
182 
183         boolean verified = StartupHelperUtil.isVerified();
184 
185         if (release.isVerified()) {
186             verified = true;
187         }
188 
189         ReleaseLocalServiceUtil.updateRelease(verified);
190 
191         // Enable database caching after verify
192 
193         CacheRegistry.setActive(true);
194     }
195 
196     private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
197 
198 }