1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
18  import com.liferay.portal.kernel.events.ActionException;
19  import com.liferay.portal.kernel.events.SimpleAction;
20  import com.liferay.portal.kernel.freemarker.FreeMarkerEngineUtil;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.messaging.MessageBus;
24  import com.liferay.portal.kernel.messaging.MessageBusUtil;
25  import com.liferay.portal.kernel.messaging.sender.MessageSender;
26  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
27  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
28  import com.liferay.portal.kernel.servlet.JspFactorySwapper;
29  import com.liferay.portal.kernel.util.ReleaseInfo;
30  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
31  import com.liferay.portal.scheduler.SchedulerEngineProxy;
32  import com.liferay.portal.security.lang.PortalSecurityManager;
33  import com.liferay.portal.service.LockLocalServiceUtil;
34  import com.liferay.portal.tools.DBUpgrader;
35  import com.liferay.portal.util.PropsValues;
36  
37  /**
38   * <a href="StartupAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Alexander Chow
42   * @author Raymond Augé
43   */
44  public class StartupAction extends SimpleAction {
45  
46      public void run(String[] ids) throws ActionException {
47          try {
48              doRun(ids);
49          }
50          catch (RuntimeException re) {
51              throw re;
52          }
53          catch (Exception e) {
54              throw new ActionException(e);
55          }
56      }
57  
58      protected void doRun(String[] ids) throws Exception {
59  
60          // Print release information
61  
62          System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
63  
64          // Clear locks
65  
66          if (_log.isDebugEnabled()) {
67              _log.debug("Clear locks");
68          }
69  
70          try {
71              LockLocalServiceUtil.clear();
72          }
73          catch (Exception e) {
74              if (_log.isWarnEnabled()) {
75                  _log.warn(
76                      "Unable to clear locks because Lock table does not exist");
77              }
78          }
79  
80          // Shutdown hook
81  
82          if (_log.isDebugEnabled()) {
83              _log.debug("Add shutdown hook");
84          }
85  
86          Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
87  
88          // Security manager
89  
90          if ((System.getSecurityManager() == null) &&
91              (PropsValues.PORTAL_SECURITY_MANAGER_ENABLE)) {
92  
93              System.setSecurityManager(new PortalSecurityManager());
94          }
95  
96          // FreeMarker
97  
98          if (_log.isDebugEnabled()) {
99              _log.debug("Initialize FreeMarker engine");
100         }
101 
102         FreeMarkerEngineUtil.init();
103 
104         // Velocity
105 
106         if (_log.isDebugEnabled()) {
107             _log.debug("Initialize Velocity engine");
108         }
109 
110         VelocityEngineUtil.init();
111 
112         // Upgrade
113 
114         if (_log.isDebugEnabled()) {
115             _log.debug("Upgrade database");
116         }
117 
118         DBUpgrader.upgrade();
119 
120         // Messaging
121 
122         if (_log.isDebugEnabled()) {
123             _log.debug("Initialize message bus");
124         }
125 
126         MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
127             MessageBus.class.getName());
128         MessageSender messageSender =
129             (MessageSender)PortalBeanLocatorUtil.locate(
130                 MessageSender.class.getName());
131         SynchronousMessageSender synchronousMessageSender =
132             (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
133                 SynchronousMessageSender.class.getName());
134 
135         MessageBusUtil.init(
136             messageBus, messageSender, synchronousMessageSender);
137 
138         // Scheduler
139 
140         if (_log.isDebugEnabled()) {
141             _log.debug("Initialize scheduler engine");
142         }
143 
144         SchedulerEngineUtil.init(new SchedulerEngineProxy());
145 
146         SchedulerEngineUtil.start();
147 
148         // Verify
149 
150         if (_log.isDebugEnabled()) {
151             _log.debug("Verify database");
152         }
153 
154         DBUpgrader.verify();
155 
156         // Liferay JspFactory
157 
158         JspFactorySwapper.swap();
159     }
160 
161     private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
162 
163 }