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