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