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    
182                                    List<AutoDeployListener> autoDeployListeners =
183                                            getAutoDeployListeners(false);
184    
185                                    AutoDeployDir autoDeployDir = new AutoDeployDir(
186                                            AutoDeployDir.DEFAULT_NAME, deployDir, destDir, interval,
187                                            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                    ClassLoader contextClassLoader =
267                            ClassLoaderUtil.getContextClassLoader();
268    
269                    JavadocManagerUtil.load(StringPool.BLANK, contextClassLoader);
270    
271                    // JCR
272    
273                    try {
274                            JCRFactoryUtil.prepare();
275    
276                            if (GetterUtil.getBoolean(
277                                            PropsUtil.get(PropsKeys.JCR_INITIALIZE_ON_STARTUP))) {
278    
279                                    JCRFactoryUtil.initialize();
280                            }
281                    }
282                    catch (Exception e) {
283                            _log.error(e);
284                    }
285    
286                    // JNDI
287    
288                    try {
289                            InfrastructureUtil.getDataSource();
290                    }
291                    catch (Exception e) {
292                            _log.error(e, e);
293                    }
294    
295                    try {
296                            if (!ServerDetector.isJOnAS()) {
297                                    InfrastructureUtil.getMailSession();
298                            }
299                    }
300                    catch (Exception e) {
301                            if (_log.isWarnEnabled()) {
302                                    _log.warn(e.getMessage());
303                            }
304                    }
305    
306                    // POP server
307    
308                    if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
309                            POPServerUtil.start();
310                    }
311    
312                    // Launch browser
313    
314                    if (Validator.isNotNull(PropsValues.BROWSER_LAUNCHER_URL)) {
315                            Thread browserLauncherThread = new Thread(new BrowserLauncher());
316    
317                            browserLauncherThread.start();
318                    }
319            }
320    
321            private static Log _log = LogFactoryUtil.getLog(GlobalStartupAction.class);
322    
323            private static List<AutoDeployListener> _autoDeployListeners;
324            private static List<HotDeployListener> _hotDeployListeners;
325    
326    }