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.jericho.CachedLoggerProvider;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
020    import com.liferay.portal.kernel.events.ActionException;
021    import com.liferay.portal.kernel.events.SimpleAction;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.messaging.MessageBus;
025    import com.liferay.portal.kernel.messaging.MessageBusUtil;
026    import com.liferay.portal.kernel.messaging.sender.MessageSender;
027    import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
028    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
029    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
030    import com.liferay.portal.kernel.servlet.JspFactorySwapper;
031    import com.liferay.portal.kernel.template.TemplateManagerUtil;
032    import com.liferay.portal.kernel.util.ReleaseInfo;
033    import com.liferay.portal.plugin.PluginPackageIndexer;
034    import com.liferay.portal.service.LockLocalServiceUtil;
035    import com.liferay.portal.tools.DBUpgrader;
036    import com.liferay.portlet.messageboards.util.MBMessageIndexer;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Alexander Chow
041     * @author Raymond Augé
042     */
043    public class StartupAction extends SimpleAction {
044    
045            @Override
046            public void run(String[] ids) throws ActionException {
047                    try {
048                            doRun(ids);
049                    }
050                    catch (RuntimeException re) {
051                            throw re;
052                    }
053                    catch (Exception e) {
054                            throw new ActionException(e);
055                    }
056            }
057    
058            protected void doRun(String[] ids) throws Exception {
059    
060                    // Print release information
061    
062                    System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
063    
064                    // Clear locks
065    
066                    if (_log.isDebugEnabled()) {
067                            _log.debug("Clear locks");
068                    }
069    
070                    try {
071                            LockLocalServiceUtil.clear();
072                    }
073                    catch (Exception e) {
074                            if (_log.isWarnEnabled()) {
075                                    _log.warn(
076                                            "Unable to clear locks because Lock table does not exist");
077                            }
078                    }
079    
080                    // Shutdown hook
081    
082                    if (_log.isDebugEnabled()) {
083                            _log.debug("Add shutdown hook");
084                    }
085    
086                    Runtime runtime = Runtime.getRuntime();
087    
088                    runtime.addShutdownHook(new Thread(new ShutdownHook()));
089    
090                    // Template manager
091    
092                    if (_log.isDebugEnabled()) {
093                            _log.debug("Initialize template manager");
094                    }
095    
096                    TemplateManagerUtil.init();
097    
098                    // Indexers
099    
100                    IndexerRegistryUtil.register(new MBMessageIndexer());
101                    IndexerRegistryUtil.register(new PluginPackageIndexer());
102    
103                    // Upgrade
104    
105                    if (_log.isDebugEnabled()) {
106                            _log.debug("Upgrade database");
107                    }
108    
109                    DBUpgrader.upgrade();
110    
111                    // Messaging
112    
113                    if (_log.isDebugEnabled()) {
114                            _log.debug("Initialize message bus");
115                    }
116    
117                    MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
118                            MessageBus.class.getName());
119                    MessageSender messageSender =
120                            (MessageSender)PortalBeanLocatorUtil.locate(
121                                    MessageSender.class.getName());
122                    SynchronousMessageSender synchronousMessageSender =
123                            (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
124                                    SynchronousMessageSender.class.getName());
125    
126                    MessageBusUtil.init(
127                            messageBus, messageSender, synchronousMessageSender);
128    
129                    // Cluster executor
130    
131                    ClusterExecutorUtil.initialize();
132    
133                    // Scheduler
134    
135                    if (_log.isDebugEnabled()) {
136                            _log.debug("Initialize scheduler engine lifecycle");
137                    }
138    
139                    SchedulerEngineHelperUtil.initialize();
140    
141                    // Verify
142    
143                    if (_log.isDebugEnabled()) {
144                            _log.debug("Verify database");
145                    }
146    
147                    DBUpgrader.verify();
148    
149                    // Liferay JspFactory
150    
151                    JspFactorySwapper.swap();
152    
153                    // Jericho
154    
155                    CachedLoggerProvider.install();
156            }
157    
158            private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
159    
160    }