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