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.pop.POPServerUtil;
37  import com.liferay.portal.util.PrefsPropsUtil;
38  import com.liferay.portal.util.PropsUtil;
39  import com.liferay.portal.util.PropsValues;
40  
41  import java.io.File;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  /**
50   * <a href="GlobalStartupAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
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(PropsUtil.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(PropsUtil.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) throws ActionException {
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                     PropsUtil.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                         PropsUtil.AUTO_DEPLOY_DEPLOY_DIR,
135                         PropsValues.AUTO_DEPLOY_DEPLOY_DIR));
136                 File destDir = new File(DeployUtil.getAutoDeployDestDir());
137                 long interval = PrefsPropsUtil.getLong(
138                     PropsUtil.AUTO_DEPLOY_INTERVAL,
139                     PropsValues.AUTO_DEPLOY_INTERVAL);
140                 int blacklistThreshold = PrefsPropsUtil.getInteger(
141                     PropsUtil.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             if (GetterUtil.getBoolean(PropsUtil.get(
167                     PropsUtil.JCR_INITIALIZE_ON_STARTUP))) {
168 
169                 JCRFactoryUtil.initialize();
170             }
171         }
172         catch (Exception e) {
173             _log.error(e);
174         }
175 
176         // JGroups
177 
178         CommLink.getInstance();
179 
180         // POP server
181 
182         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
183             POPServerUtil.start();
184         }
185     }
186 
187     private static Log _log = LogFactory.getLog(GlobalStartupAction.class);
188 
189 }