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