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