001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
019    import com.liferay.portal.kernel.events.ActionException;
020    import com.liferay.portal.kernel.events.SimpleAction;
021    import com.liferay.portal.kernel.freemarker.FreeMarkerEngineUtil;
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.SchedulerEngineUtil;
029    import com.liferay.portal.kernel.servlet.JspFactorySwapper;
030    import com.liferay.portal.kernel.util.ReleaseInfo;
031    import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
032    import com.liferay.portal.scheduler.SchedulerEngineProxy;
033    import com.liferay.portal.security.lang.PortalSecurityManager;
034    import com.liferay.portal.service.LockLocalServiceUtil;
035    import com.liferay.portal.tools.DBUpgrader;
036    import com.liferay.portal.util.PropsValues;
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            public void run(String[] ids) throws ActionException {
046                    try {
047                            doRun(ids);
048                    }
049                    catch (RuntimeException re) {
050                            throw re;
051                    }
052                    catch (Exception e) {
053                            throw new ActionException(e);
054                    }
055            }
056    
057            protected void doRun(String[] ids) throws Exception {
058    
059                    // Print release information
060    
061                    System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
062    
063                    // Clear locks
064    
065                    if (_log.isDebugEnabled()) {
066                            _log.debug("Clear locks");
067                    }
068    
069                    try {
070                            LockLocalServiceUtil.clear();
071                    }
072                    catch (Exception e) {
073                            if (_log.isWarnEnabled()) {
074                                    _log.warn(
075                                            "Unable to clear locks because Lock table does not exist");
076                            }
077                    }
078    
079                    // Shutdown hook
080    
081                    if (_log.isDebugEnabled()) {
082                            _log.debug("Add shutdown hook");
083                    }
084    
085                    Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
086    
087                    // Security manager
088    
089                    if ((System.getSecurityManager() == null) &&
090                            (PropsValues.PORTAL_SECURITY_MANAGER_ENABLE)) {
091    
092                            System.setSecurityManager(new PortalSecurityManager());
093                    }
094    
095                    // FreeMarker
096    
097                    if (_log.isDebugEnabled()) {
098                            _log.debug("Initialize FreeMarker engine");
099                    }
100    
101                    FreeMarkerEngineUtil.init();
102    
103                    // Velocity
104    
105                    if (_log.isDebugEnabled()) {
106                            _log.debug("Initialize Velocity engine");
107                    }
108    
109                    VelocityEngineUtil.init();
110    
111                    // Upgrade
112    
113                    if (_log.isDebugEnabled()) {
114                            _log.debug("Upgrade database");
115                    }
116    
117                    DBUpgrader.upgrade();
118    
119                    // Messaging
120    
121                    if (_log.isDebugEnabled()) {
122                            _log.debug("Initialize message bus");
123                    }
124    
125                    MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
126                            MessageBus.class.getName());
127                    MessageSender messageSender =
128                            (MessageSender)PortalBeanLocatorUtil.locate(
129                                    MessageSender.class.getName());
130                    SynchronousMessageSender synchronousMessageSender =
131                            (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
132                                    SynchronousMessageSender.class.getName());
133    
134                    MessageBusUtil.init(
135                            messageBus, messageSender, synchronousMessageSender);
136    
137                    // Scheduler
138    
139                    if (_log.isDebugEnabled()) {
140                            _log.debug("Initialize scheduler engine");
141                    }
142    
143                    SchedulerEngineUtil.init(new SchedulerEngineProxy());
144    
145                    SchedulerEngineUtil.start();
146    
147                    // Verify
148    
149                    if (_log.isDebugEnabled()) {
150                            _log.debug("Verify database");
151                    }
152    
153                    DBUpgrader.verify();
154    
155                    // Liferay JspFactory
156    
157                    JspFactorySwapper.swap();
158    
159                    // Cluster executor
160    
161                    ClusterExecutorUtil.initialize();
162            }
163    
164            private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
165    
166    }