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