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.events.SimpleAction;
025    import com.liferay.portal.kernel.javadoc.JavadocManagerUtil;
026    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.servlet.ServletContextPool;
030    import com.liferay.portal.kernel.util.BasePortalLifecycle;
031    import com.liferay.portal.kernel.util.ClassLoaderUtil;
032    import com.liferay.portal.kernel.util.InfrastructureUtil;
033    import com.liferay.portal.kernel.util.InstanceFactory;
034    import com.liferay.portal.kernel.util.PortalLifecycle;
035    import com.liferay.portal.kernel.util.PortalLifecycleUtil;
036    import com.liferay.portal.kernel.util.PropsKeys;
037    import com.liferay.portal.kernel.util.ServerDetector;
038    import com.liferay.portal.kernel.util.StringPool;
039    import com.liferay.portal.kernel.util.Validator;
040    import com.liferay.portal.spring.context.PortalContextLoaderListener;
041    import com.liferay.portal.struts.AuthPublicPathRegistry;
042    import com.liferay.portal.util.BrowserLauncher;
043    import com.liferay.portal.util.PrefsPropsUtil;
044    import com.liferay.portal.util.PropsUtil;
045    import com.liferay.portal.util.PropsValues;
046    
047    import java.io.File;
048    
049    import java.util.ArrayList;
050    import java.util.List;
051    
052    import javax.servlet.ServletContext;
053    
054    /**
055     * @author Brian Wing Shun Chan
056     */
057    public class GlobalStartupAction extends SimpleAction {
058    
059            public static List<AutoDeployListener> getAutoDeployListeners(
060                    boolean reset) {
061    
062                    if ((_autoDeployListeners != null) && !reset) {
063                            return _autoDeployListeners;
064                    }
065    
066                    List<AutoDeployListener> autoDeployListeners = new ArrayList<>();
067    
068                    String[] autoDeployListenerClassNames = PropsUtil.getArray(
069                            PropsKeys.AUTO_DEPLOY_LISTENERS);
070    
071                    for (String autoDeployListenerClassName :
072                                    autoDeployListenerClassNames) {
073    
074                            try {
075                                    if (_log.isDebugEnabled()) {
076                                            _log.debug("Instantiating " + autoDeployListenerClassName);
077                                    }
078    
079                                    AutoDeployListener autoDeployListener =
080                                            (AutoDeployListener)InstanceFactory.newInstance(
081                                                    autoDeployListenerClassName);
082    
083                                    autoDeployListeners.add(autoDeployListener);
084                            }
085                            catch (Exception e) {
086                                    _log.error(e);
087                            }
088                    }
089    
090                    _autoDeployListeners = autoDeployListeners;
091    
092                    return _autoDeployListeners;
093            }
094    
095            public static List<HotDeployListener> getHotDeployListeners() {
096                    if (_hotDeployListeners != null) {
097                            return _hotDeployListeners;
098                    }
099    
100                    List<HotDeployListener> hotDeployListeners = new ArrayList<>();
101    
102                    String[] hotDeployListenerClassNames = PropsUtil.getArray(
103                            PropsKeys.HOT_DEPLOY_LISTENERS);
104    
105                    for (String hotDeployListenerClassName : hotDeployListenerClassNames) {
106                            try {
107                                    if (_log.isDebugEnabled()) {
108                                            _log.debug("Instantiating " + hotDeployListenerClassName);
109                                    }
110    
111                                    HotDeployListener hotDeployListener =
112                                            (HotDeployListener)InstanceFactory.newInstance(
113                                                    hotDeployListenerClassName);
114    
115                                    hotDeployListeners.add(hotDeployListener);
116                            }
117                            catch (Exception e) {
118                                    _log.error(e);
119                            }
120                    }
121    
122                    _hotDeployListeners = hotDeployListeners;
123    
124                    return _hotDeployListeners;
125            }
126    
127            @Override
128            public void run(String[] ids) {
129    
130                    // Auto deploy
131    
132                    try {
133                            File deployDir = new File(
134                                    PrefsPropsUtil.getString(
135                                            PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
136                                            PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
137                            File destDir = new File(DeployUtil.getAutoDeployDestDir());
138                            long interval = PrefsPropsUtil.getLong(
139                                    PropsKeys.AUTO_DEPLOY_INTERVAL,
140                                    PropsValues.AUTO_DEPLOY_INTERVAL);
141    
142                            List<AutoDeployListener> autoDeployListeners =
143                                    getAutoDeployListeners(false);
144    
145                            AutoDeployDir autoDeployDir = new AutoDeployDir(
146                                    AutoDeployDir.DEFAULT_NAME, deployDir, destDir, interval,
147                                    autoDeployListeners);
148    
149                            if (PrefsPropsUtil.getBoolean(
150                                            PropsKeys.AUTO_DEPLOY_ENABLED,
151                                            PropsValues.AUTO_DEPLOY_ENABLED)) {
152    
153                                    if (_log.isInfoEnabled()) {
154                                            _log.info("Registering auto deploy directories");
155                                    }
156    
157                                    AutoDeployUtil.registerDir(autoDeployDir);
158                            }
159                            else {
160                                    if (_log.isInfoEnabled()) {
161                                            _log.info("Not registering auto deploy directories");
162                                    }
163                            }
164                    }
165                    catch (Exception e) {
166                            _log.error(e);
167                    }
168    
169                    // Hot deploy
170    
171                    if (_log.isDebugEnabled()) {
172                            _log.debug("Registering hot deploy listeners");
173                    }
174    
175                    for (HotDeployListener hotDeployListener : getHotDeployListeners()) {
176                            HotDeployUtil.registerListener(hotDeployListener);
177                    }
178    
179                    // Authentication
180    
181                    AuthPublicPathRegistry.register(PropsValues.AUTH_PUBLIC_PATHS);
182    
183                    // Javadoc
184    
185                    ClassLoader contextClassLoader =
186                            ClassLoaderUtil.getContextClassLoader();
187    
188                    JavadocManagerUtil.load(StringPool.BLANK, contextClassLoader);
189    
190                    // JNDI
191    
192                    try {
193                            InfrastructureUtil.getDataSource();
194                    }
195                    catch (Exception e) {
196                            _log.error(e, e);
197                    }
198    
199                    try {
200                            if (!ServerDetector.isJOnAS()) {
201                                    InfrastructureUtil.getMailSession();
202                            }
203                    }
204                    catch (Exception e) {
205                            if (_log.isWarnEnabled()) {
206                                    _log.warn(e.getMessage());
207                            }
208                    }
209    
210                    // JSON web service
211    
212                    ServletContext servletContext = ServletContextPool.get(
213                            PortalContextLoaderListener.getPortalServletContextName());
214    
215                    JSONWebServiceActionsManagerUtil.registerServletContext(servletContext);
216    
217                    // Plugins
218    
219                    PortalLifecycleUtil.register(
220                            new BasePortalLifecycle() {
221    
222                                    @Override
223                                    protected void doPortalDestroy() {
224                                    }
225    
226                                    @Override
227                                    protected void doPortalInit() {
228                                            RequiredPluginsUtil.startCheckingRequiredPlugins();
229                                    }
230    
231                            },
232                            PortalLifecycle.METHOD_INIT);
233    
234                    // Launch browser
235    
236                    if (Validator.isNotNull(PropsValues.BROWSER_LAUNCHER_URL)) {
237                            Thread browserLauncherThread = new Thread(new BrowserLauncher());
238    
239                            browserLauncherThread.start();
240                    }
241            }
242    
243            private static final Log _log = LogFactoryUtil.getLog(
244                    GlobalStartupAction.class);
245    
246            private static List<AutoDeployListener> _autoDeployListeners;
247            private static List<HotDeployListener> _hotDeployListeners;
248    
249    }