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