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