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