1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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.InstanceFactory;
30  import com.liferay.portal.kernel.util.PropsKeys;
31  import com.liferay.portal.kernel.util.ServerDetector;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.pop.POPServerUtil;
34  import com.liferay.portal.util.BrowserLauncher;
35  import com.liferay.portal.util.PrefsPropsUtil;
36  import com.liferay.portal.util.PropsUtil;
37  import com.liferay.portal.util.PropsValues;
38  import com.liferay.util.SystemProperties;
39  
40  import java.io.File;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  import org.jamwiki.Environment;
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 void run(String[] ids) {
111 
112         // Hot deploy
113 
114         if (_log.isDebugEnabled()) {
115             _log.debug("Registering hot deploy listeners");
116         }
117 
118         for (HotDeployListener hotDeployListener : getHotDeployListeners()) {
119             HotDeployUtil.registerListener(hotDeployListener);
120         }
121 
122         // Auto deploy
123 
124         try {
125             if (PrefsPropsUtil.getBoolean(
126                     PropsKeys.AUTO_DEPLOY_ENABLED,
127                     PropsValues.AUTO_DEPLOY_ENABLED)) {
128 
129                 if (_log.isInfoEnabled()) {
130                     _log.info("Registering auto deploy directories");
131                 }
132 
133                 File deployDir = new File(
134                     PrefsPropsUtil.getString(
135                         PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
136                         PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
137                 File destDir = new File(DeployUtil.getAutoDeployDestDir());
138                 long interval = PrefsPropsUtil.getLong(
139                     PropsKeys.AUTO_DEPLOY_INTERVAL,
140                     PropsValues.AUTO_DEPLOY_INTERVAL);
141                 int blacklistThreshold = PrefsPropsUtil.getInteger(
142                     PropsKeys.AUTO_DEPLOY_BLACKLIST_THRESHOLD,
143                     PropsValues.AUTO_DEPLOY_BLACKLIST_THRESHOLD);
144 
145                 List<AutoDeployListener> autoDeployListeners =
146                     getAutoDeployListeners();
147 
148                 AutoDeployDir autoDeployDir = new AutoDeployDir(
149                     AutoDeployDir.DEFAULT_NAME, deployDir, destDir, interval,
150                     blacklistThreshold, autoDeployListeners);
151 
152                 AutoDeployUtil.registerDir(autoDeployDir);
153             }
154             else {
155                 if (_log.isInfoEnabled()) {
156                     _log.info("Not registering auto deploy directories");
157                 }
158             }
159         }
160         catch (Exception e) {
161             _log.error(e);
162         }
163 
164         // JAMWiki
165 
166         try {
167             String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
168 
169             Environment.setValue(Environment.PROP_BASE_FILE_DIR, tmpDir);
170         }
171         catch (Throwable t) {
172             _log.error(t);
173         }
174 
175         // JCR
176 
177         try {
178             JCRFactoryUtil.prepare();
179 
180             if (GetterUtil.getBoolean(PropsUtil.get(
181                     PropsKeys.JCR_INITIALIZE_ON_STARTUP))) {
182 
183                 JCRFactoryUtil.initialize();
184             }
185         }
186         catch (Exception e) {
187             _log.error(e);
188         }
189 
190         // JNDI
191 
192         try {
193             InfrastructureUtil.getDataSource();
194         }
195         catch (Exception e) {
196             _log.error(e, e);
197         }
198 
199         try {
200             if (!ServerDetector.isJOnAS()) {
201                 InfrastructureUtil.getMailSession();
202             }
203         }
204         catch (Exception e) {
205             if (_log.isWarnEnabled()) {
206                 _log.warn(e.getMessage());
207             }
208         }
209 
210         // POP server
211 
212         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
213             POPServerUtil.start();
214         }
215 
216         // Launch browser
217 
218         if (Validator.isNotNull(PropsValues.BROWSER_LAUNCHER_URL)) {
219             Thread browserLauncherThread = new Thread(new BrowserLauncher());
220 
221             browserLauncherThread.start();
222         }
223     }
224 
225     private static Log _log = LogFactoryUtil.getLog(GlobalStartupAction.class);
226 
227 }