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