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