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