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