001    /**
002     * Copyright (c) 2000-present 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.deploy.DeployUtil;
018    import com.liferay.portal.deploy.RequiredPluginsUtil;
019    import com.liferay.portal.kernel.deploy.auto.AutoDeployDir;
020    import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
021    import com.liferay.portal.kernel.deploy.auto.AutoDeployUtil;
022    import com.liferay.portal.kernel.deploy.hot.HotDeployListener;
023    import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
024    import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployDir;
025    import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployListener;
026    import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployUtil;
027    import com.liferay.portal.kernel.events.SimpleAction;
028    import com.liferay.portal.kernel.javadoc.JavadocManagerUtil;
029    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil;
030    import com.liferay.portal.kernel.log.Log;
031    import com.liferay.portal.kernel.log.LogFactoryUtil;
032    import com.liferay.portal.kernel.servlet.ServletContextPool;
033    import com.liferay.portal.kernel.util.BasePortalLifecycle;
034    import com.liferay.portal.kernel.util.ClassLoaderUtil;
035    import com.liferay.portal.kernel.util.InfrastructureUtil;
036    import com.liferay.portal.kernel.util.InstanceFactory;
037    import com.liferay.portal.kernel.util.PortalLifecycle;
038    import com.liferay.portal.kernel.util.PortalLifecycleUtil;
039    import com.liferay.portal.kernel.util.PropsKeys;
040    import com.liferay.portal.kernel.util.ServerDetector;
041    import com.liferay.portal.kernel.util.StringPool;
042    import com.liferay.portal.kernel.util.Validator;
043    import com.liferay.portal.pop.POPServerUtil;
044    import com.liferay.portal.spring.context.PortalContextLoaderListener;
045    import com.liferay.portal.struts.AuthPublicPathRegistry;
046    import com.liferay.portal.util.BrowserLauncher;
047    import com.liferay.portal.util.PrefsPropsUtil;
048    import com.liferay.portal.util.PropsUtil;
049    import com.liferay.portal.util.PropsValues;
050    
051    import java.io.File;
052    
053    import java.util.ArrayList;
054    import java.util.List;
055    
056    import javax.servlet.ServletContext;
057    
058    /**
059     * @author Brian Wing Shun Chan
060     */
061    public class GlobalStartupAction extends SimpleAction {
062    
063            public static List<AutoDeployListener> getAutoDeployListeners(
064                    boolean reset) {
065    
066                    if ((_autoDeployListeners != null) && !reset) {
067                            return _autoDeployListeners;
068                    }
069    
070                    List<AutoDeployListener> autoDeployListeners = new ArrayList<>();
071    
072                    String[] autoDeployListenerClassNames = PropsUtil.getArray(
073                            PropsKeys.AUTO_DEPLOY_LISTENERS);
074    
075                    for (String autoDeployListenerClassName :
076                                    autoDeployListenerClassNames) {
077    
078                            try {
079                                    if (_log.isDebugEnabled()) {
080                                            _log.debug("Instantiating " + autoDeployListenerClassName);
081                                    }
082    
083                                    AutoDeployListener autoDeployListener =
084                                            (AutoDeployListener)InstanceFactory.newInstance(
085                                                    autoDeployListenerClassName);
086    
087                                    autoDeployListeners.add(autoDeployListener);
088                            }
089                            catch (Exception e) {
090                                    _log.error(e);
091                            }
092                    }
093    
094                    _autoDeployListeners = autoDeployListeners;
095    
096                    return _autoDeployListeners;
097            }
098    
099            public static List<HotDeployListener> getHotDeployListeners() {
100                    if (_hotDeployListeners != null) {
101                            return _hotDeployListeners;
102                    }
103    
104                    List<HotDeployListener> hotDeployListeners = new ArrayList<>();
105    
106                    String[] hotDeployListenerClassNames = PropsUtil.getArray(
107                            PropsKeys.HOT_DEPLOY_LISTENERS);
108    
109                    for (String hotDeployListenerClassName : hotDeployListenerClassNames) {
110                            try {
111                                    if (_log.isDebugEnabled()) {
112                                            _log.debug("Instantiating " + hotDeployListenerClassName);
113                                    }
114    
115                                    HotDeployListener hotDeployListener =
116                                            (HotDeployListener)InstanceFactory.newInstance(
117                                                    hotDeployListenerClassName);
118    
119                                    hotDeployListeners.add(hotDeployListener);
120                            }
121                            catch (Exception e) {
122                                    _log.error(e);
123                            }
124                    }
125    
126                    _hotDeployListeners = hotDeployListeners;
127    
128                    return _hotDeployListeners;
129            }
130    
131            public static List<SandboxDeployListener> getSandboxDeployListeners() {
132                    List<SandboxDeployListener> sandboxDeployListeners = new ArrayList<>();
133    
134                    String[] sandboxDeployListenerClassNames = PropsUtil.getArray(
135                            PropsKeys.SANDBOX_DEPLOY_LISTENERS);
136    
137                    for (String sandboxDeployListenerClassName :
138                                    sandboxDeployListenerClassNames) {
139    
140                            try {
141                                    if (_log.isDebugEnabled()) {
142                                            _log.debug(
143                                                    "Instantiating " + sandboxDeployListenerClassName);
144                                    }
145    
146                                    SandboxDeployListener sandboxDeployListener =
147                                            (SandboxDeployListener)InstanceFactory.newInstance(
148                                                    sandboxDeployListenerClassName);
149    
150                                    sandboxDeployListeners.add(sandboxDeployListener);
151                            }
152                            catch (Exception e) {
153                                    _log.error(e);
154                            }
155                    }
156    
157                    return sandboxDeployListeners;
158            }
159    
160            @Override
161            public void run(String[] ids) {
162    
163                    // Auto deploy
164    
165                    try {
166                            File deployDir = new File(
167                                    PrefsPropsUtil.getString(
168                                            PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
169                                            PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
170                            File destDir = new File(DeployUtil.getAutoDeployDestDir());
171                            long interval = PrefsPropsUtil.getLong(
172                                    PropsKeys.AUTO_DEPLOY_INTERVAL,
173                                    PropsValues.AUTO_DEPLOY_INTERVAL);
174    
175                            List<AutoDeployListener> autoDeployListeners =
176                                    getAutoDeployListeners(false);
177    
178                            AutoDeployDir autoDeployDir = new AutoDeployDir(
179                                    AutoDeployDir.DEFAULT_NAME, deployDir, destDir, interval,
180                                    autoDeployListeners);
181    
182                            if (PrefsPropsUtil.getBoolean(
183                                            PropsKeys.AUTO_DEPLOY_ENABLED,
184                                            PropsValues.AUTO_DEPLOY_ENABLED)) {
185    
186                                    if (_log.isInfoEnabled()) {
187                                            _log.info("Registering auto deploy directories");
188                                    }
189    
190                                    AutoDeployUtil.registerDir(autoDeployDir);
191                            }
192                            else {
193                                    if (_log.isInfoEnabled()) {
194                                            _log.info("Not registering auto deploy directories");
195                                    }
196                            }
197                    }
198                    catch (Exception e) {
199                            _log.error(e);
200                    }
201    
202                    // Hot deploy
203    
204                    if (_log.isDebugEnabled()) {
205                            _log.debug("Registering hot deploy listeners");
206                    }
207    
208                    for (HotDeployListener hotDeployListener : getHotDeployListeners()) {
209                            HotDeployUtil.registerListener(hotDeployListener);
210                    }
211    
212                    // Sandobox deploy
213    
214                    try {
215                            if (PrefsPropsUtil.getBoolean(
216                                            PropsKeys.SANDBOX_DEPLOY_ENABLED,
217                                            PropsValues.SANDBOX_DEPLOY_ENABLED)) {
218    
219                                    if (_log.isInfoEnabled()) {
220                                            _log.info("Registering sandbox deploy directories");
221                                    }
222    
223                                    File deployDir = new File(
224                                            PrefsPropsUtil.getString(
225                                                    PropsKeys.SANDBOX_DEPLOY_DIR,
226                                                    PropsValues.SANDBOX_DEPLOY_DIR));
227                                    long interval = PrefsPropsUtil.getLong(
228                                            PropsKeys.SANDBOX_DEPLOY_INTERVAL,
229                                            PropsValues.SANDBOX_DEPLOY_INTERVAL);
230    
231                                    List<SandboxDeployListener> sandboxDeployListeners =
232                                            getSandboxDeployListeners();
233    
234                                    SandboxDeployDir sandboxDeployDir = new SandboxDeployDir(
235                                            SandboxDeployDir.DEFAULT_NAME, deployDir, interval,
236                                            sandboxDeployListeners);
237    
238                                    SandboxDeployUtil.registerDir(sandboxDeployDir);
239                            }
240                            else {
241                                    if (_log.isInfoEnabled()) {
242                                            _log.info("Not registering sandbox deploy directories");
243                                    }
244                            }
245                    }
246                    catch (Exception e) {
247                            _log.error(e);
248                    }
249    
250                    // Authentication
251    
252                    AuthPublicPathRegistry.register(PropsValues.AUTH_PUBLIC_PATHS);
253    
254                    // Javadoc
255    
256                    ClassLoader contextClassLoader =
257                            ClassLoaderUtil.getContextClassLoader();
258    
259                    JavadocManagerUtil.load(StringPool.BLANK, contextClassLoader);
260    
261                    // JNDI
262    
263                    try {
264                            InfrastructureUtil.getDataSource();
265                    }
266                    catch (Exception e) {
267                            _log.error(e, e);
268                    }
269    
270                    try {
271                            if (!ServerDetector.isJOnAS()) {
272                                    InfrastructureUtil.getMailSession();
273                            }
274                    }
275                    catch (Exception e) {
276                            if (_log.isWarnEnabled()) {
277                                    _log.warn(e.getMessage());
278                            }
279                    }
280    
281                    // JSON web service
282    
283                    ServletContext servletContext = ServletContextPool.get(
284                            PortalContextLoaderListener.getPortalServletContextName());
285    
286                    JSONWebServiceActionsManagerUtil.registerServletContext(servletContext);
287    
288                    // Plugins
289    
290                    PortalLifecycleUtil.register(
291                            new BasePortalLifecycle() {
292    
293                                    @Override
294                                    protected void doPortalDestroy() {
295                                    }
296    
297                                    @Override
298                                    protected void doPortalInit() {
299                                            RequiredPluginsUtil.startCheckingRequiredPlugins();
300                                    }
301    
302                            },
303                            PortalLifecycle.METHOD_INIT);
304    
305                    // POP server
306    
307                    if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
308                            POPServerUtil.start();
309                    }
310    
311                    // Launch browser
312    
313                    if (Validator.isNotNull(PropsValues.BROWSER_LAUNCHER_URL)) {
314                            Thread browserLauncherThread = new Thread(new BrowserLauncher());
315    
316                            browserLauncherThread.start();
317                    }
318            }
319    
320            private static final Log _log = LogFactoryUtil.getLog(
321                    GlobalStartupAction.class);
322    
323            private static List<AutoDeployListener> _autoDeployListeners;
324            private static List<HotDeployListener> _hotDeployListeners;
325    
326    }