1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.deploy.DeployUtil;
18  import com.liferay.portal.jcr.JCRFactoryUtil;
19  import com.liferay.portal.kernel.deploy.auto.AutoDeployDir;
20  import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
21  import com.liferay.portal.kernel.deploy.auto.AutoDeployUtil;
22  import com.liferay.portal.kernel.deploy.hot.HotDeployListener;
23  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
24  import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployDir;
25  import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployListener;
26  import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployUtil;
27  import com.liferay.portal.kernel.events.SimpleAction;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InfrastructureUtil;
32  import com.liferay.portal.kernel.util.InstanceFactory;
33  import com.liferay.portal.kernel.util.PropsKeys;
34  import com.liferay.portal.kernel.util.ServerDetector;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.pop.POPServerUtil;
37  import com.liferay.portal.util.BrowserLauncher;
38  import com.liferay.portal.util.PrefsPropsUtil;
39  import com.liferay.portal.util.PropsUtil;
40  import com.liferay.portal.util.PropsValues;
41  
42  import java.io.File;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  /**
48   * <a href="GlobalStartupAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class GlobalStartupAction extends SimpleAction {
53  
54      public static List<AutoDeployListener> getAutoDeployListeners() {
55          List<AutoDeployListener> autoDeployListeners =
56              new ArrayList<AutoDeployListener>();
57  
58          String[] autoDeployListenerClassNames = PropsUtil.getArray(
59              PropsKeys.AUTO_DEPLOY_LISTENERS);
60  
61          for (String autoDeployListenerClassName :
62                  autoDeployListenerClassNames) {
63  
64              try {
65                  if (_log.isDebugEnabled()) {
66                      _log.debug("Instantiating " + autoDeployListenerClassName);
67                  }
68  
69                  AutoDeployListener autoDeployListener =
70                      (AutoDeployListener)InstanceFactory.newInstance(
71                          autoDeployListenerClassName);
72  
73                  autoDeployListeners.add(autoDeployListener);
74              }
75              catch (Exception e) {
76                  _log.error(e);
77              }
78          }
79  
80          return autoDeployListeners;
81      }
82  
83      public static List<HotDeployListener> getHotDeployListeners() {
84          List<HotDeployListener> hotDeployListeners =
85              new ArrayList<HotDeployListener>();
86  
87          String[] hotDeployListenerClassNames = PropsUtil.getArray(
88              PropsKeys.HOT_DEPLOY_LISTENERS);
89  
90          for (String hotDeployListenerClassName : hotDeployListenerClassNames) {
91              try {
92                  if (_log.isDebugEnabled()) {
93                      _log.debug("Instantiating " + hotDeployListenerClassName);
94                  }
95  
96                  HotDeployListener hotDeployListener =
97                      (HotDeployListener)InstanceFactory.newInstance(
98                          hotDeployListenerClassName);
99  
100                 hotDeployListeners.add(hotDeployListener);
101             }
102             catch (Exception e) {
103                 _log.error(e);
104             }
105         }
106 
107         return hotDeployListeners;
108     }
109 
110     public static List<SandboxDeployListener> getSandboxDeployListeners() {
111         List<SandboxDeployListener> sandboxDeployListeners =
112             new ArrayList<SandboxDeployListener>();
113 
114         String[] sandboxDeployListenerClassNames = PropsUtil.getArray(
115             PropsKeys.SANDBOX_DEPLOY_LISTENERS);
116 
117         for (String sandboxDeployListenerClassName :
118                 sandboxDeployListenerClassNames) {
119 
120             try {
121                 if (_log.isDebugEnabled()) {
122                     _log.debug(
123                         "Instantiating " + sandboxDeployListenerClassName);
124                 }
125 
126                 SandboxDeployListener sandboxDeployListener =
127                     (SandboxDeployListener)InstanceFactory.newInstance(
128                         sandboxDeployListenerClassName);
129 
130                 sandboxDeployListeners.add(sandboxDeployListener);
131             }
132             catch (Exception e) {
133                 _log.error(e);
134             }
135         }
136 
137         return sandboxDeployListeners;
138     }
139 
140     public void run(String[] ids) {
141 
142         // Hot deploy
143 
144         if (_log.isDebugEnabled()) {
145             _log.debug("Registering hot deploy listeners");
146         }
147 
148         for (HotDeployListener hotDeployListener : getHotDeployListeners()) {
149             HotDeployUtil.registerListener(hotDeployListener);
150         }
151 
152         // Auto deploy
153 
154         try {
155             if (PrefsPropsUtil.getBoolean(
156                     PropsKeys.AUTO_DEPLOY_ENABLED,
157                     PropsValues.AUTO_DEPLOY_ENABLED)) {
158 
159                 if (_log.isInfoEnabled()) {
160                     _log.info("Registering auto deploy directories");
161                 }
162 
163                 File deployDir = new File(
164                     PrefsPropsUtil.getString(
165                         PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
166                         PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
167                 File destDir = new File(DeployUtil.getAutoDeployDestDir());
168                 long interval = PrefsPropsUtil.getLong(
169                     PropsKeys.AUTO_DEPLOY_INTERVAL,
170                     PropsValues.AUTO_DEPLOY_INTERVAL);
171                 int blacklistThreshold = PrefsPropsUtil.getInteger(
172                     PropsKeys.AUTO_DEPLOY_BLACKLIST_THRESHOLD,
173                     PropsValues.AUTO_DEPLOY_BLACKLIST_THRESHOLD);
174 
175                 List<AutoDeployListener> autoDeployListeners =
176                     getAutoDeployListeners();
177 
178                 AutoDeployDir autoDeployDir = new AutoDeployDir(
179                     AutoDeployDir.DEFAULT_NAME, deployDir, destDir, interval,
180                     blacklistThreshold, autoDeployListeners);
181 
182                 AutoDeployUtil.registerDir(autoDeployDir);
183             }
184             else {
185                 if (_log.isInfoEnabled()) {
186                     _log.info("Not registering auto deploy directories");
187                 }
188             }
189         }
190         catch (Exception e) {
191             _log.error(e);
192         }
193 
194         // Sandobox deploy
195 
196         try {
197             if (PrefsPropsUtil.getBoolean(
198                     PropsKeys.SANDBOX_DEPLOY_ENABLED,
199                     PropsValues.SANDBOX_DEPLOY_ENABLED)) {
200 
201                 if (_log.isInfoEnabled()) {
202                     _log.info("Registering sandbox deploy directories");
203                 }
204 
205                 File deployDir = new File(
206                     PrefsPropsUtil.getString(
207                         PropsKeys.SANDBOX_DEPLOY_DIR,
208                         PropsValues.SANDBOX_DEPLOY_DIR));
209                 long interval = PrefsPropsUtil.getLong(
210                     PropsKeys.SANDBOX_DEPLOY_INTERVAL,
211                     PropsValues.SANDBOX_DEPLOY_INTERVAL);
212 
213                 List<SandboxDeployListener> sandboxDeployListeners =
214                     getSandboxDeployListeners();
215 
216                 SandboxDeployDir sandboxDeployDir = new SandboxDeployDir(
217                     SandboxDeployDir.DEFAULT_NAME, deployDir, interval,
218                     sandboxDeployListeners);
219 
220                 SandboxDeployUtil.registerDir(sandboxDeployDir);
221             }
222             else {
223                 if (_log.isInfoEnabled()) {
224                     _log.info("Not registering sandbox deploy directories");
225                 }
226             }
227         }
228         catch (Exception e) {
229             _log.error(e);
230         }
231 
232         // JCR
233 
234         try {
235             JCRFactoryUtil.prepare();
236 
237             if (GetterUtil.getBoolean(PropsUtil.get(
238                     PropsKeys.JCR_INITIALIZE_ON_STARTUP))) {
239 
240                 JCRFactoryUtil.initialize();
241             }
242         }
243         catch (Exception e) {
244             _log.error(e);
245         }
246 
247         // JNDI
248 
249         try {
250             InfrastructureUtil.getDataSource();
251         }
252         catch (Exception e) {
253             _log.error(e, e);
254         }
255 
256         try {
257             if (!ServerDetector.isJOnAS()) {
258                 InfrastructureUtil.getMailSession();
259             }
260         }
261         catch (Exception e) {
262             if (_log.isWarnEnabled()) {
263                 _log.warn(e.getMessage());
264             }
265         }
266 
267         // POP server
268 
269         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
270             POPServerUtil.start();
271         }
272 
273         // Launch browser
274 
275         if (Validator.isNotNull(PropsValues.BROWSER_LAUNCHER_URL)) {
276             Thread browserLauncherThread = new Thread(new BrowserLauncher());
277 
278             browserLauncherThread.start();
279         }
280     }
281 
282     private static Log _log = LogFactoryUtil.getLog(GlobalStartupAction.class);
283 
284 }