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