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