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