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