1   /**
2    * Copyright (c) 2000-2008 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.ActionException;
34  import com.liferay.portal.kernel.events.SimpleAction;
35  import com.liferay.portal.kernel.util.GetterUtil;
36  import com.liferay.portal.kernel.util.InstancePool;
37  import com.liferay.portal.pop.POPServerUtil;
38  import com.liferay.portal.util.PrefsPropsUtil;
39  import com.liferay.portal.util.PropsUtil;
40  import com.liferay.portal.util.PropsValues;
41  
42  import java.io.File;
43  
44  import java.util.ArrayList;
45  import java.util.Iterator;
46  import java.util.List;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
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 getAutoDeployListeners() {
60          List list = new ArrayList();
61  
62          String[] autoDeployListeners =
63              PropsUtil.getArray(PropsUtil.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 getHotDeployListeners() {
86          List list = new ArrayList();
87  
88          String[] hotDeployListeners =
89              PropsUtil.getArray(PropsUtil.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) throws ActionException {
112 
113         // Hot deploy
114 
115         if (_log.isDebugEnabled()) {
116             _log.debug("Registering hot deploy listeners");
117         }
118 
119         Iterator itr = getHotDeployListeners().iterator();
120 
121         while (itr.hasNext()) {
122             HotDeployListener hotDeployListener = (HotDeployListener)itr.next();
123 
124             HotDeployUtil.registerListener(hotDeployListener);
125         }
126 
127         // Auto deploy
128 
129         try {
130             if (PrefsPropsUtil.getBoolean(
131                     PropsUtil.AUTO_DEPLOY_ENABLED,
132                     PropsValues.AUTO_DEPLOY_ENABLED)) {
133 
134                 if (_log.isInfoEnabled()) {
135                     _log.info("Registering auto deploy directories");
136                 }
137 
138                 File deployDir = new File(
139                     PrefsPropsUtil.getString(
140                         PropsUtil.AUTO_DEPLOY_DEPLOY_DIR,
141                         PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
142                 File destDir = new File(DeployUtil.getAutoDeployDestDir());
143                 long interval = PrefsPropsUtil.getLong(
144                     PropsUtil.AUTO_DEPLOY_INTERVAL,
145                     PropsValues.AUTO_DEPLOY_INTERVAL);
146                 int blacklistThreshold = PrefsPropsUtil.getInteger(
147                     PropsUtil.AUTO_DEPLOY_BLACKLIST_THRESHOLD,
148                     PropsValues.AUTO_DEPLOY_BLACKLIST_THRESHOLD);
149 
150                 List autoDeployListeners = getAutoDeployListeners();
151 
152                 AutoDeployDir autoDeployDir = new AutoDeployDir(
153                     "defaultAutoDeployDir", deployDir, destDir, interval,
154                     blacklistThreshold, autoDeployListeners);
155 
156                 AutoDeployUtil.registerDir(autoDeployDir);
157             }
158             else {
159                 if (_log.isInfoEnabled()) {
160                     _log.info("Not registering auto deploy directories");
161                 }
162             }
163         }
164         catch (Exception e) {
165             _log.error(e);
166         }
167 
168         // JCR
169 
170         try {
171             if (GetterUtil.getBoolean(PropsUtil.get(
172                     PropsUtil.JCR_INITIALIZE_ON_STARTUP))) {
173 
174                 JCRFactoryUtil.initialize();
175             }
176         }
177         catch (Exception e) {
178             _log.error(e);
179         }
180 
181         // JGroups
182 
183         CommLink.getInstance();
184 
185         // POP server
186 
187         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
188             POPServerUtil.start();
189         }
190 
191         // Other required events
192 
193         runEvent(FixOracleAction.class.getName(), ids);
194         runEvent(FixCounterAction.class.getName(), ids);
195         //runEvent(FixJournalAction.class.getName(), ids);
196     }
197 
198     protected void runEvent(String className, String[] ids)
199         throws ActionException {
200 
201         SimpleAction action = (SimpleAction)InstancePool.get(className);
202 
203         action.run(ids);
204     }
205 
206     private static Log _log = LogFactory.getLog(GlobalStartupAction.class);
207 
208 }