001    /**
002     * Copyright (c) 2000-2011 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.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InfrastructureUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.PropsKeys;
034    import com.liferay.portal.kernel.util.ServerDetector;
035    import com.liferay.portal.kernel.util.SystemProperties;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.pop.POPServerUtil;
038    import com.liferay.portal.struts.AuthPublicPathRegistry;
039    import com.liferay.portal.util.BrowserLauncher;
040    import com.liferay.portal.util.PrefsPropsUtil;
041    import com.liferay.portal.util.PropsUtil;
042    import com.liferay.portal.util.PropsValues;
043    
044    import java.io.File;
045    
046    import java.util.ArrayList;
047    import java.util.List;
048    
049    import org.jamwiki.Environment;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     */
054    public class GlobalStartupAction extends SimpleAction {
055    
056            public static List<AutoDeployListener> getAutoDeployListeners() {
057                    if (_autoDeployListeners != null) {
058                            return _autoDeployListeners;
059                    }
060    
061                    List<AutoDeployListener> autoDeployListeners =
062                            new ArrayList<AutoDeployListener>();
063    
064                    String[] autoDeployListenerClassNames = PropsUtil.getArray(
065                            PropsKeys.AUTO_DEPLOY_LISTENERS);
066    
067                    for (String autoDeployListenerClassName :
068                                    autoDeployListenerClassNames) {
069    
070                            try {
071                                    if (_log.isDebugEnabled()) {
072                                            _log.debug("Instantiating " + autoDeployListenerClassName);
073                                    }
074    
075                                    AutoDeployListener autoDeployListener =
076                                            (AutoDeployListener)InstanceFactory.newInstance(
077                                                    autoDeployListenerClassName);
078    
079                                    autoDeployListeners.add(autoDeployListener);
080                            }
081                            catch (Exception e) {
082                                    _log.error(e);
083                            }
084                    }
085    
086                    _autoDeployListeners = autoDeployListeners;
087    
088                    return _autoDeployListeners;
089            }
090    
091            public static List<HotDeployListener> getHotDeployListeners() {
092                    if (_hotDeployListeners != null) {
093                            return _hotDeployListeners;
094                    }
095    
096                    List<HotDeployListener> hotDeployListeners =
097                            new ArrayList<HotDeployListener>();
098    
099                    String[] hotDeployListenerClassNames = PropsUtil.getArray(
100                            PropsKeys.HOT_DEPLOY_LISTENERS);
101    
102                    for (String hotDeployListenerClassName : hotDeployListenerClassNames) {
103                            try {
104                                    if (_log.isDebugEnabled()) {
105                                            _log.debug("Instantiating " + hotDeployListenerClassName);
106                                    }
107    
108                                    HotDeployListener hotDeployListener =
109                                            (HotDeployListener)InstanceFactory.newInstance(
110                                                    hotDeployListenerClassName);
111    
112                                    hotDeployListeners.add(hotDeployListener);
113                            }
114                            catch (Exception e) {
115                                    _log.error(e);
116                            }
117                    }
118    
119                    _hotDeployListeners = hotDeployListeners;
120    
121                    return _hotDeployListeners;
122            }
123    
124            public static List<SandboxDeployListener> getSandboxDeployListeners() {
125                    List<SandboxDeployListener> sandboxDeployListeners =
126                            new ArrayList<SandboxDeployListener>();
127    
128                    String[] sandboxDeployListenerClassNames = PropsUtil.getArray(
129                            PropsKeys.SANDBOX_DEPLOY_LISTENERS);
130    
131                    for (String sandboxDeployListenerClassName :
132                                    sandboxDeployListenerClassNames) {
133    
134                            try {
135                                    if (_log.isDebugEnabled()) {
136                                            _log.debug(
137                                                    "Instantiating " + sandboxDeployListenerClassName);
138                                    }
139    
140                                    SandboxDeployListener sandboxDeployListener =
141                                            (SandboxDeployListener)InstanceFactory.newInstance(
142                                                    sandboxDeployListenerClassName);
143    
144                                    sandboxDeployListeners.add(sandboxDeployListener);
145                            }
146                            catch (Exception e) {
147                                    _log.error(e);
148                            }
149                    }
150    
151                    return sandboxDeployListeners;
152            }
153    
154            @Override
155            public void run(String[] ids) {
156    
157                    // Auto deploy
158    
159                    try {
160                            if (PrefsPropsUtil.getBoolean(
161                                            PropsKeys.AUTO_DEPLOY_ENABLED,
162                                            PropsValues.AUTO_DEPLOY_ENABLED)) {
163    
164                                    if (_log.isInfoEnabled()) {
165                                            _log.info("Registering auto deploy directories");
166                                    }
167    
168                                    File deployDir = new File(
169                                            PrefsPropsUtil.getString(
170                                                    PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
171                                                    PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
172                                    File destDir = new File(DeployUtil.getAutoDeployDestDir());
173                                    long interval = PrefsPropsUtil.getLong(
174                                            PropsKeys.AUTO_DEPLOY_INTERVAL,
175                                            PropsValues.AUTO_DEPLOY_INTERVAL);
176                                    int blacklistThreshold = PrefsPropsUtil.getInteger(
177                                            PropsKeys.AUTO_DEPLOY_BLACKLIST_THRESHOLD,
178                                            PropsValues.AUTO_DEPLOY_BLACKLIST_THRESHOLD);
179    
180                                    List<AutoDeployListener> autoDeployListeners =
181                                            getAutoDeployListeners();
182    
183                                    AutoDeployDir autoDeployDir = new AutoDeployDir(
184                                            AutoDeployDir.DEFAULT_NAME, deployDir, destDir, interval,
185                                            blacklistThreshold, 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                    // JCR
263    
264                    try {
265                            JCRFactoryUtil.prepare();
266    
267                            if (GetterUtil.getBoolean(PropsUtil.get(
268                                            PropsKeys.JCR_INITIALIZE_ON_STARTUP))) {
269    
270                                    JCRFactoryUtil.initialize();
271                            }
272                    }
273                    catch (Exception e) {
274                            _log.error(e);
275                    }
276    
277                    // JNDI
278    
279                    try {
280                            InfrastructureUtil.getDataSource();
281                    }
282                    catch (Exception e) {
283                            _log.error(e, e);
284                    }
285    
286                    try {
287                            if (!ServerDetector.isJOnAS()) {
288                                    InfrastructureUtil.getMailSession();
289                            }
290                    }
291                    catch (Exception e) {
292                            if (_log.isWarnEnabled()) {
293                                    _log.warn(e.getMessage());
294                            }
295                    }
296    
297                    // POP server
298    
299                    if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
300                            POPServerUtil.start();
301                    }
302    
303                    // Launch browser
304    
305                    if (Validator.isNotNull(PropsValues.BROWSER_LAUNCHER_URL)) {
306                            Thread browserLauncherThread = new Thread(new BrowserLauncher());
307    
308                            browserLauncherThread.start();
309                    }
310            }
311    
312            private static Log _log = LogFactoryUtil.getLog(GlobalStartupAction.class);
313    
314            private static List<AutoDeployListener> _autoDeployListeners;
315            private static List<HotDeployListener> _hotDeployListeners;
316    
317    }