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