001    /**
002     * Copyright (c) 2000-2012 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.kernel.util.ServerDetector;
034    import com.liferay.portal.plugin.PluginPackageIndexer;
035    import com.liferay.portal.security.lang.PortalSecurityManager;
036    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
037    import com.liferay.portal.service.LockLocalServiceUtil;
038    import com.liferay.portal.tools.DBUpgrader;
039    import com.liferay.portal.util.PropsValues;
040    import com.liferay.portlet.messageboards.util.MBMessageIndexer;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Alexander Chow
045     * @author Raymond Augé
046     */
047    public class StartupAction extends SimpleAction {
048    
049            @Override
050            public void run(String[] ids) throws ActionException {
051                    try {
052                            doRun(ids);
053                    }
054                    catch (RuntimeException re) {
055                            throw re;
056                    }
057                    catch (Exception e) {
058                            throw new ActionException(e);
059                    }
060            }
061    
062            protected void doRun(String[] ids) throws Exception {
063    
064                    // Print release information
065    
066                    System.out.println("Starting " + ReleaseInfo.getReleaseInfo());
067    
068                    // Clear locks
069    
070                    if (_log.isDebugEnabled()) {
071                            _log.debug("Clear locks");
072                    }
073    
074                    boolean enabled = PortalSecurityManagerThreadLocal.isEnabled();
075    
076                    try {
077                            PortalSecurityManagerThreadLocal.setEnabled(false);
078    
079                            LockLocalServiceUtil.clear();
080                    }
081                    catch (Exception e) {
082                            if (_log.isWarnEnabled()) {
083                                    _log.warn(
084                                            "Unable to clear locks because Lock table does not exist");
085                            }
086                    }
087                    finally {
088                            PortalSecurityManagerThreadLocal.setEnabled(enabled);
089                    }
090    
091                    // Shutdown hook
092    
093                    if (_log.isDebugEnabled()) {
094                            _log.debug("Add shutdown hook");
095                    }
096    
097                    Runtime runtime = Runtime.getRuntime();
098    
099                    runtime.addShutdownHook(new Thread(new ShutdownHook()));
100    
101                    // Security manager
102    
103                    String portalSecurityManagerStrategy =
104                            PropsValues.PORTAL_SECURITY_MANAGER_STRATEGY;
105    
106                    if (portalSecurityManagerStrategy.equals("smart")) {
107                            if (ServerDetector.isWebSphere()) {
108                                    portalSecurityManagerStrategy = "none";
109                            }
110                            else {
111                                    portalSecurityManagerStrategy = "default";
112                            }
113                    }
114    
115                    if (portalSecurityManagerStrategy.equals("liferay")) {
116                            if (System.getSecurityManager() == null) {
117                                    System.setSecurityManager(new PortalSecurityManager());
118                            }
119                    }
120                    else if (portalSecurityManagerStrategy.equals("none")) {
121                            System.setSecurityManager(null);
122                    }
123    
124                    // Template manager
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug("Initialize template manager");
128                    }
129    
130                    TemplateManagerUtil.init();
131    
132                    // Indexers
133    
134                    IndexerRegistryUtil.register(new MBMessageIndexer());
135                    IndexerRegistryUtil.register(new PluginPackageIndexer());
136    
137                    // Upgrade
138    
139                    if (_log.isDebugEnabled()) {
140                            _log.debug("Upgrade database");
141                    }
142    
143                    enabled = PortalSecurityManagerThreadLocal.isEnabled();
144    
145                    try {
146                            PortalSecurityManagerThreadLocal.setEnabled(false);
147    
148                            DBUpgrader.upgrade();
149                    }
150                    finally {
151                            PortalSecurityManagerThreadLocal.setEnabled(enabled);
152                    }
153    
154                    // Messaging
155    
156                    if (_log.isDebugEnabled()) {
157                            _log.debug("Initialize message bus");
158                    }
159    
160                    MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
161                            MessageBus.class.getName());
162                    MessageSender messageSender =
163                            (MessageSender)PortalBeanLocatorUtil.locate(
164                                    MessageSender.class.getName());
165                    SynchronousMessageSender synchronousMessageSender =
166                            (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
167                                    SynchronousMessageSender.class.getName());
168    
169                    MessageBusUtil.init(
170                            messageBus, messageSender, synchronousMessageSender);
171    
172                    // Cluster executor
173    
174                    ClusterExecutorUtil.initialize();
175    
176                    // Scheduler
177    
178                    if (_log.isDebugEnabled()) {
179                            _log.debug("Initialize scheduler engine lifecycle");
180                    }
181    
182                    SchedulerEngineHelperUtil.initialize();
183    
184                    // Verify
185    
186                    if (_log.isDebugEnabled()) {
187                            _log.debug("Verify database");
188                    }
189    
190                    DBUpgrader.verify();
191    
192                    // Liferay JspFactory
193    
194                    JspFactorySwapper.swap();
195    
196                    // Jericho
197    
198                    CachedLoggerProvider.install();
199            }
200    
201            private static Log _log = LogFactoryUtil.getLog(StartupAction.class);
202    
203    }