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