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.security.pacl.PACLClassLoaderUtil;
041    import com.liferay.portal.struts.AuthPublicPathRegistry;
042    import com.liferay.portal.util.BrowserLauncher;
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                                    int blacklistThreshold = PrefsPropsUtil.getInteger(
180                                            PropsKeys.AUTO_DEPLOY_BLACKLIST_THRESHOLD,
181                                            PropsValues.AUTO_DEPLOY_BLACKLIST_THRESHOLD);
182    
183                                    List<AutoDeployListener> autoDeployListeners =
184                                            getAutoDeployListeners();
185    
186                                    AutoDeployDir autoDeployDir = new AutoDeployDir(
187                                            AutoDeployDir.DEFAULT_NAME, deployDir, destDir, interval,
188                                            blacklistThreshold, autoDeployListeners);
189    
190                                    AutoDeployUtil.registerDir(autoDeployDir);
191                            }
192                            else {
193                                    if (_log.isInfoEnabled()) {
194                                            _log.info("Not registering auto deploy directories");
195                                    }
196                            }
197                    }
198                    catch (Exception e) {
199                            _log.error(e);
200                    }
201    
202                    // Hot deploy
203    
204                    if (_log.isDebugEnabled()) {
205                            _log.debug("Registering hot deploy listeners");
206                    }
207    
208                    for (HotDeployListener hotDeployListener : getHotDeployListeners()) {
209                            HotDeployUtil.registerListener(hotDeployListener);
210                    }
211    
212                    // Sandobox deploy
213    
214                    try {
215                            if (PrefsPropsUtil.getBoolean(
216                                            PropsKeys.SANDBOX_DEPLOY_ENABLED,
217                                            PropsValues.SANDBOX_DEPLOY_ENABLED)) {
218    
219                                    if (_log.isInfoEnabled()) {
220                                            _log.info("Registering sandbox deploy directories");
221                                    }
222    
223                                    File deployDir = new File(
224                                            PrefsPropsUtil.getString(
225                                                    PropsKeys.SANDBOX_DEPLOY_DIR,
226                                                    PropsValues.SANDBOX_DEPLOY_DIR));
227                                    long interval = PrefsPropsUtil.getLong(
228                                            PropsKeys.SANDBOX_DEPLOY_INTERVAL,
229                                            PropsValues.SANDBOX_DEPLOY_INTERVAL);
230    
231                                    List<SandboxDeployListener> sandboxDeployListeners =
232                                            getSandboxDeployListeners();
233    
234                                    SandboxDeployDir sandboxDeployDir = new SandboxDeployDir(
235                                            SandboxDeployDir.DEFAULT_NAME, deployDir, interval,
236                                            sandboxDeployListeners);
237    
238                                    SandboxDeployUtil.registerDir(sandboxDeployDir);
239                            }
240                            else {
241                                    if (_log.isInfoEnabled()) {
242                                            _log.info("Not registering sandbox deploy directories");
243                                    }
244                            }
245                    }
246                    catch (Exception e) {
247                            _log.error(e);
248                    }
249    
250                    // Authentication
251    
252                    AuthPublicPathRegistry.register(PropsValues.AUTH_PUBLIC_PATHS);
253    
254                    // JAMWiki
255    
256                    try {
257                            String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
258    
259                            Environment.setValue(Environment.PROP_BASE_FILE_DIR, tmpDir);
260                    }
261                    catch (Throwable t) {
262                            _log.error(t);
263                    }
264    
265                    // Javadoc
266    
267                    ClassLoader contextClassLoader =
268                            PACLClassLoaderUtil.getContextClassLoader();
269    
270                    JavadocManagerUtil.load(StringPool.BLANK, contextClassLoader);
271    
272                    // JCR
273    
274                    try {
275                            JCRFactoryUtil.prepare();
276    
277                            if (GetterUtil.getBoolean(
278                                            PropsUtil.get(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    }