1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.events;
24  
25  import com.liferay.lock.service.LockServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
29  import com.liferay.portal.kernel.cache.CacheRegistry;
30  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
31  import com.liferay.portal.kernel.events.ActionException;
32  import com.liferay.portal.kernel.events.SimpleAction;
33  import com.liferay.portal.kernel.log.Log;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.messaging.MessageBus;
36  import com.liferay.portal.kernel.messaging.MessageBusUtil;
37  import com.liferay.portal.kernel.messaging.sender.MessageSender;
38  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
39  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
40  import com.liferay.portal.kernel.util.ReleaseInfo;
41  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
42  import com.liferay.portal.model.CompanyConstants;
43  import com.liferay.portal.model.Release;
44  import com.liferay.portal.scheduler.SchedulerEngineProxy;
45  import com.liferay.portal.search.lucene.LuceneUtil;
46  import com.liferay.portal.security.lang.PortalSecurityManager;
47  import com.liferay.portal.security.permission.ResourceActionsUtil;
48  import com.liferay.portal.service.ClassNameLocalServiceUtil;
49  import com.liferay.portal.service.ReleaseLocalServiceUtil;
50  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
51  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
52  import com.liferay.portal.util.PropsValues;
53  
54  /**
55   * <a href="StartupAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Alexander Chow
59   * @author Raymond Augé
60   *
61   */
62  public class StartupAction extends SimpleAction {
63  
64      public void run(String[] ids) throws ActionException {
65          try {
66              doRun(ids);
67          }
68          catch (RuntimeException re) {
69              throw re;
70          }
71          catch (Exception e) {
72              throw new ActionException(e);
73          }
74          finally {
75              LuceneUtil.checkLuceneDir(CompanyConstants.SYSTEM);
76          }
77      }
78  
79      protected void doRun(String[] ids) throws PortalException, SystemException {
80  
81          // Print release information
82  
83          System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
84  
85          // Clear locks
86  
87          try {
88              LockServiceUtil.clear();
89          }
90          catch (Exception e) {
91              _log.error(e, e);
92          }
93  
94          // Shutdown hook
95  
96          Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
97  
98          // Security manager
99  
100         if ((System.getSecurityManager() == null) &&
101             (PropsValues.PORTAL_SECURITY_MANAGER_ENABLE)) {
102 
103             System.setSecurityManager(new PortalSecurityManager());
104         }
105 
106         // Velocity
107 
108         VelocityEngineUtil.init();
109 
110         // Disable database caching before upgrade
111 
112         CacheRegistry.setActive(false);
113 
114         // Upgrade
115 
116         int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
117 
118         if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
119             String msg = "You must first upgrade to Liferay Portal 4.2.1";
120 
121             _log.fatal(msg);
122 
123             throw new RuntimeException(msg);
124         }
125 
126         StartupHelperUtil.upgradeProcess(buildNumber);
127 
128         // Class names
129 
130         ClassNameLocalServiceUtil.checkClassNames();
131 
132         // Resource actions
133 
134         ResourceActionsUtil.init();
135 
136         ResourceActionLocalServiceUtil.checkResourceActions();
137 
138         // Resource codes
139 
140         ResourceCodeLocalServiceUtil.checkResourceCodes();
141 
142         // Delete temporary images
143 
144         StartupHelperUtil.deleteTempImages();
145 
146         // Clear the caches only if the upgrade process was run
147 
148         if (StartupHelperUtil.isUpgraded()) {
149             MultiVMPoolUtil.clear();
150         }
151 
152         // Messaging
153 
154         MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
155             MessageBus.class.getName());
156         MessageSender messageSender =
157             (MessageSender)PortalBeanLocatorUtil.locate(
158                 MessageSender.class.getName());
159         SynchronousMessageSender synchronousMessageSender =
160             (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
161                 SynchronousMessageSender.class.getName());
162 
163         MessageBusUtil.init(
164             messageBus, messageSender, synchronousMessageSender);
165 
166         // Scheduler
167 
168         SchedulerEngineUtil.init(new SchedulerEngineProxy());
169 
170         SchedulerEngineUtil.start();
171 
172         // Verify
173 
174         Release release = ReleaseLocalServiceUtil.getRelease();
175 
176         StartupHelperUtil.verifyProcess(release.isVerified());
177 
178         // Update indexes
179 
180         if (StartupHelperUtil.isUpgraded()) {
181             StartupHelperUtil.updateIndexes();
182         }
183 
184         // Update release
185 
186         boolean verified = StartupHelperUtil.isVerified();
187 
188         if (release.isVerified()) {
189             verified = true;
190         }
191 
192         ReleaseLocalServiceUtil.updateRelease(verified);
193 
194         // Enable database caching after verify
195 
196         CacheRegistry.setActive(true);
197     }
198 
199     private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
200 
201 }