1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.events;
21  
22  import com.liferay.portal.comm.CommLink;
23  import com.liferay.portal.deploy.DeployUtil;
24  import com.liferay.portal.jcr.JCRFactoryUtil;
25  import com.liferay.portal.kernel.deploy.auto.AutoDeployDir;
26  import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
27  import com.liferay.portal.kernel.deploy.auto.AutoDeployUtil;
28  import com.liferay.portal.kernel.deploy.hot.HotDeployListener;
29  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
30  import com.liferay.portal.kernel.events.SimpleAction;
31  import com.liferay.portal.kernel.log.Log;
32  import com.liferay.portal.kernel.log.LogFactoryUtil;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.InfrastructureUtil;
35  import com.liferay.portal.kernel.util.ServerDetector;
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.PropsKeys;
40  import com.liferay.portal.util.PropsUtil;
41  import com.liferay.portal.util.PropsValues;
42  
43  import java.io.File;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  /**
49   * <a href="GlobalStartupAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class GlobalStartupAction extends SimpleAction {
55  
56      public static List<AutoDeployListener> getAutoDeployListeners() {
57          List<AutoDeployListener> list = new ArrayList<AutoDeployListener>();
58  
59          String[] autoDeployListeners =
60              PropsUtil.getArray(PropsKeys.AUTO_DEPLOY_LISTENERS);
61  
62          for (int i = 0; i < autoDeployListeners.length; i++) {
63              try {
64                  if (_log.isDebugEnabled()) {
65                      _log.debug("Instantiating " + autoDeployListeners[i]);
66                  }
67  
68                  AutoDeployListener autoDeployListener =
69                      (AutoDeployListener)Class.forName(
70                          autoDeployListeners[i]).newInstance();
71  
72                  list.add(autoDeployListener);
73              }
74              catch (Exception e) {
75                  _log.error(e);
76              }
77          }
78  
79          return list;
80      }
81  
82      public static List<HotDeployListener> getHotDeployListeners() {
83          List<HotDeployListener> list = new ArrayList<HotDeployListener>();
84  
85          String[] hotDeployListeners =
86              PropsUtil.getArray(PropsKeys.HOT_DEPLOY_LISTENERS);
87  
88          for (int i = 0; i < hotDeployListeners.length; i++) {
89              try {
90                  if (_log.isDebugEnabled()) {
91                      _log.debug("Instantiating " + hotDeployListeners[i]);
92                  }
93  
94                  HotDeployListener hotDeployListener =
95                      (HotDeployListener)Class.forName(
96                          hotDeployListeners[i]).newInstance();
97  
98                  list.add(hotDeployListener);
99              }
100             catch (Exception e) {
101                 _log.error(e);
102             }
103         }
104 
105         return list;
106     }
107 
108     public void run(String[] ids) {
109 
110         // Hot deploy
111 
112         if (_log.isDebugEnabled()) {
113             _log.debug("Registering hot deploy listeners");
114         }
115 
116         for (HotDeployListener hotDeployListener : getHotDeployListeners()) {
117             HotDeployUtil.registerListener(hotDeployListener);
118         }
119 
120         // Auto deploy
121 
122         try {
123             if (PrefsPropsUtil.getBoolean(
124                     PropsKeys.AUTO_DEPLOY_ENABLED,
125                     PropsValues.AUTO_DEPLOY_ENABLED)) {
126 
127                 if (_log.isInfoEnabled()) {
128                     _log.info("Registering auto deploy directories");
129                 }
130 
131                 File deployDir = new File(
132                     PrefsPropsUtil.getString(
133                         PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
134                         PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
135                 File destDir = new File(DeployUtil.getAutoDeployDestDir());
136                 long interval = PrefsPropsUtil.getLong(
137                     PropsKeys.AUTO_DEPLOY_INTERVAL,
138                     PropsValues.AUTO_DEPLOY_INTERVAL);
139                 int blacklistThreshold = PrefsPropsUtil.getInteger(
140                     PropsKeys.AUTO_DEPLOY_BLACKLIST_THRESHOLD,
141                     PropsValues.AUTO_DEPLOY_BLACKLIST_THRESHOLD);
142 
143                 List<AutoDeployListener> autoDeployListeners =
144                     getAutoDeployListeners();
145 
146                 AutoDeployDir autoDeployDir = new AutoDeployDir(
147                     "defaultAutoDeployDir", deployDir, destDir, interval,
148                     blacklistThreshold, autoDeployListeners);
149 
150                 AutoDeployUtil.registerDir(autoDeployDir);
151             }
152             else {
153                 if (_log.isInfoEnabled()) {
154                     _log.info("Not registering auto deploy directories");
155                 }
156             }
157         }
158         catch (Exception e) {
159             _log.error(e);
160         }
161 
162         // JCR
163 
164         try {
165             JCRFactoryUtil.prepare();
166 
167             if (GetterUtil.getBoolean(PropsUtil.get(
168                     PropsKeys.JCR_INITIALIZE_ON_STARTUP))) {
169 
170                 JCRFactoryUtil.initialize();
171             }
172         }
173         catch (Exception e) {
174             _log.error(e);
175         }
176 
177         // JGroups
178 
179         CommLink.getInstance();
180 
181         // JNDI
182 
183         try {
184             InfrastructureUtil.getDataSource();
185         }
186         catch (Exception e) {
187             _log.error(e, e);
188         }
189 
190         try {
191             if (!ServerDetector.isJOnAS()) {
192                 InfrastructureUtil.getMailSession();
193             }
194         }
195         catch (Exception e) {
196             if (_log.isWarnEnabled()) {
197                 _log.warn(e.getMessage());
198             }
199         }
200 
201         // POP server
202 
203         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
204             POPServerUtil.start();
205         }
206 
207         // Launch browser
208 
209         Thread browserLauncherThread = new Thread(new BrowserLauncher());
210 
211         browserLauncherThread.start();
212     }
213 
214     private static Log _log = LogFactoryUtil.getLog(GlobalStartupAction.class);
215 
216 }