001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.cache.ehcache.EhcacheStreamBootstrapCacheLoader;
018    import com.liferay.portal.jericho.CachedLoggerProvider;
019    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskLifecycle;
020    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
021    import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
022    import com.liferay.portal.kernel.cluster.ClusterLinkUtil;
023    import com.liferay.portal.kernel.cluster.ClusterMasterExecutorUtil;
024    import com.liferay.portal.kernel.events.ActionException;
025    import com.liferay.portal.kernel.events.SimpleAction;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.messaging.MessageBus;
029    import com.liferay.portal.kernel.messaging.MessageBusUtil;
030    import com.liferay.portal.kernel.messaging.sender.MessageSender;
031    import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
032    import com.liferay.portal.kernel.nio.intraband.Intraband;
033    import com.liferay.portal.kernel.nio.intraband.SystemDataType;
034    import com.liferay.portal.kernel.nio.intraband.cache.PortalCacheDatagramReceiveHandler;
035    import com.liferay.portal.kernel.nio.intraband.mailbox.MailboxDatagramReceiveHandler;
036    import com.liferay.portal.kernel.nio.intraband.messaging.MessageDatagramReceiveHandler;
037    import com.liferay.portal.kernel.nio.intraband.rpc.RPCDatagramReceiveHandler;
038    import com.liferay.portal.kernel.patcher.PatcherUtil;
039    import com.liferay.portal.kernel.resiliency.mpi.MPIHelperUtil;
040    import com.liferay.portal.kernel.resiliency.spi.SPIUtil;
041    import com.liferay.portal.kernel.resiliency.spi.agent.annotation.Direction;
042    import com.liferay.portal.kernel.resiliency.spi.agent.annotation.DistributedRegistry;
043    import com.liferay.portal.kernel.resiliency.spi.agent.annotation.MatchType;
044    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
045    import com.liferay.portal.kernel.scheduler.SchedulerException;
046    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
047    import com.liferay.portal.kernel.servlet.JspFactorySwapper;
048    import com.liferay.portal.kernel.template.TemplateManagerUtil;
049    import com.liferay.portal.kernel.util.BasePortalLifecycle;
050    import com.liferay.portal.kernel.util.PortalLifecycle;
051    import com.liferay.portal.kernel.util.ReleaseInfo;
052    import com.liferay.portal.kernel.util.StringPool;
053    import com.liferay.portal.kernel.util.StringUtil;
054    import com.liferay.portal.kernel.util.Validator;
055    import com.liferay.portal.plugin.PluginPackageIndexer;
056    import com.liferay.portal.security.lang.DoPrivilegedUtil;
057    import com.liferay.portal.service.LockLocalServiceUtil;
058    import com.liferay.portal.tools.DBUpgrader;
059    import com.liferay.portal.util.WebKeys;
060    import com.liferay.portlet.messageboards.util.MBMessageIndexer;
061    
062    import javax.portlet.MimeResponse;
063    import javax.portlet.PortletRequest;
064    
065    import org.apache.struts.taglib.tiles.ComponentConstants;
066    
067    /**
068     * @author Brian Wing Shun Chan
069     * @author Alexander Chow
070     * @author Raymond Aug??
071     */
072    public class StartupAction extends SimpleAction {
073    
074            @Override
075            public void run(String[] ids) throws ActionException {
076                    try {
077                            doRun(ids);
078                    }
079                    catch (RuntimeException re) {
080                            throw re;
081                    }
082                    catch (Exception e) {
083                            throw new ActionException(e);
084                    }
085            }
086    
087            protected void doRun(String[] ids) throws Exception {
088    
089                    // Print release information
090    
091                    System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
092    
093                    // Installed patches
094    
095                    if (_log.isInfoEnabled() && !PatcherUtil.hasInconsistentPatchLevels()) {
096                            String installedPatches = StringUtil.merge(
097                                    PatcherUtil.getInstalledPatches(), StringPool.COMMA_AND_SPACE);
098    
099                            if (Validator.isNull(installedPatches)) {
100                                    _log.info("There are no patches installed");
101                            }
102                            else {
103                                    _log.info(
104                                            "The following patches are installed: " + installedPatches);
105                            }
106                    }
107    
108                    // Portal resiliency
109    
110                    DistributedRegistry.registerDistributed(
111                            ComponentConstants.COMPONENT_CONTEXT, Direction.DUPLEX,
112                            MatchType.POSTFIX);
113                    DistributedRegistry.registerDistributed(
114                            MimeResponse.MARKUP_HEAD_ELEMENT, Direction.DUPLEX,
115                            MatchType.EXACT);
116                    DistributedRegistry.registerDistributed(
117                            PortletRequest.LIFECYCLE_PHASE, Direction.DUPLEX, MatchType.EXACT);
118                    DistributedRegistry.registerDistributed(WebKeys.class);
119    
120                    Intraband intraband = MPIHelperUtil.getIntraband();
121    
122                    intraband.registerDatagramReceiveHandler(
123                            SystemDataType.MAILBOX.getValue(),
124                            new MailboxDatagramReceiveHandler());
125    
126                    MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
127                            MessageBus.class.getName());
128    
129                    intraband.registerDatagramReceiveHandler(
130                            SystemDataType.MESSAGE.getValue(),
131                            new MessageDatagramReceiveHandler(messageBus));
132    
133                    intraband.registerDatagramReceiveHandler(
134                            SystemDataType.PORTAL_CACHE.getValue(),
135                            new PortalCacheDatagramReceiveHandler());
136                    intraband.registerDatagramReceiveHandler(
137                            SystemDataType.RPC.getValue(), new RPCDatagramReceiveHandler());
138    
139                    // Clear locks
140    
141                    if (_log.isDebugEnabled()) {
142                            _log.debug("Clear locks");
143                    }
144    
145                    try {
146                            LockLocalServiceUtil.clear();
147                    }
148                    catch (Exception e) {
149                            if (_log.isWarnEnabled()) {
150                                    _log.warn(
151                                            "Unable to clear locks because Lock table does not exist");
152                            }
153                    }
154    
155                    // Shutdown hook
156    
157                    if (_log.isDebugEnabled()) {
158                            _log.debug("Add shutdown hook");
159                    }
160    
161                    Runtime runtime = Runtime.getRuntime();
162    
163                    runtime.addShutdownHook(new Thread(new ShutdownHook()));
164    
165                    // Template manager
166    
167                    if (_log.isDebugEnabled()) {
168                            _log.debug("Initialize template manager");
169                    }
170    
171                    TemplateManagerUtil.init();
172    
173                    // Indexers
174    
175                    IndexerRegistryUtil.register(new MBMessageIndexer());
176                    IndexerRegistryUtil.register(new PluginPackageIndexer());
177    
178                    // Upgrade
179    
180                    if (_log.isDebugEnabled()) {
181                            _log.debug("Upgrade database");
182                    }
183    
184                    DBUpgrader.upgrade();
185    
186                    // Messaging
187    
188                    if (_log.isDebugEnabled()) {
189                            _log.debug("Initialize message bus");
190                    }
191    
192                    MessageSender messageSender =
193                            (MessageSender)PortalBeanLocatorUtil.locate(
194                                    MessageSender.class.getName());
195                    SynchronousMessageSender synchronousMessageSender =
196                            (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
197                                    SynchronousMessageSender.class.getName());
198    
199                    MessageBusUtil.init(
200                            DoPrivilegedUtil.wrap(messageBus),
201                            DoPrivilegedUtil.wrap(messageSender),
202                            DoPrivilegedUtil.wrap(synchronousMessageSender));
203    
204                    // Cluster link
205    
206                    ClusterLinkUtil.initialize();
207    
208                    // Cluster executor
209    
210                    ClusterExecutorUtil.initialize();
211    
212                    if (!SPIUtil.isSPI()) {
213                            ClusterMasterExecutorUtil.initialize();
214                    }
215    
216                    // Ehache bootstrap
217    
218                    EhcacheStreamBootstrapCacheLoader.start();
219    
220                    // Scheduler
221    
222                    if (_log.isDebugEnabled()) {
223                            _log.debug("Initialize scheduler engine lifecycle");
224                    }
225    
226                    try {
227                            SchedulerEngineHelperUtil.initialize();
228                    }
229                    catch (SchedulerException se) {
230                            _log.error(se, se);
231                    }
232    
233                    // Verify
234    
235                    if (_log.isDebugEnabled()) {
236                            _log.debug("Verify database");
237                    }
238    
239                    DBUpgrader.verify();
240    
241                    // Background tasks
242    
243                    BasePortalLifecycle backgroundTaskLifecycle =
244                            new BackgroundTaskLifecycle();
245    
246                    backgroundTaskLifecycle.registerPortalLifecycle(
247                            PortalLifecycle.METHOD_INIT);
248    
249                    // Liferay JspFactory
250    
251                    JspFactorySwapper.swap();
252    
253                    // Jericho
254    
255                    CachedLoggerProvider.install();
256            }
257    
258            private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
259    
260    }