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