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