1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.deploy.hot;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.ArrayList;
29  import java.util.HashSet;
30  import java.util.List;
31  import java.util.Set;
32  
33  /**
34   * <a href="HotDeployUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Ivica Cardic
37   * @author Brian Wing Shun Chan
38   */
39  public class HotDeployUtil {
40  
41      public static void fireDeployEvent(HotDeployEvent event) {
42          _instance._fireDeployEvent(event);
43      }
44  
45      public static void fireUndeployEvent(HotDeployEvent event) {
46          _instance._fireUndeployEvent(event);
47      }
48  
49      public static void flushPrematureEvents() {
50          _instance._flushPrematureEvents();
51      }
52  
53      public static void registerListener(HotDeployListener listener) {
54          _instance._registerListener(listener);
55      }
56  
57      public static void unregisterListeners() {
58          _instance._unregisterListeners();
59      }
60  
61      private HotDeployUtil() {
62          if (_log.isInfoEnabled()) {
63              _log.info("Initializing hot deploy manager " + this.hashCode());
64          }
65  
66          _dependentEvents = new ArrayList<HotDeployEvent>();
67          _deployedServletContextNames = new HashSet<String>();
68          _listeners = new ArrayList<HotDeployListener>();
69          _prematureEvents = new ArrayList<HotDeployEvent>();
70      }
71  
72      private void _doFireDeployEvent(HotDeployEvent event) {
73          if (_deployedServletContextNames.contains(
74                  event.getServletContextName())) {
75  
76              return;
77          }
78  
79          boolean hasDependencies = true;
80  
81          Set<String> dependentServletContextNames =
82              event.getDependentServletContextNames();
83  
84          for (String dependentServletContextName :
85                  dependentServletContextNames) {
86  
87              if (!_deployedServletContextNames.contains(
88                      dependentServletContextName)) {
89  
90                  hasDependencies = false;
91  
92                  break;
93              }
94          }
95  
96          if (hasDependencies) {
97              if (_dependentEvents.contains(event)) {
98                  if (_log.isInfoEnabled()) {
99                      _log.info(
100                         "Deploy " + event.getServletContextName() +
101                             " from queue");
102                 }
103             }
104 
105             for (HotDeployListener listener : _listeners) {
106                 try {
107                     listener.invokeDeploy(event);
108                 }
109                 catch (HotDeployException hde) {
110                     _log.error(hde, hde);
111                 }
112             }
113 
114             _deployedServletContextNames.add(event.getServletContextName());
115 
116             _dependentEvents.remove(event);
117 
118             List<HotDeployEvent> dependentEvents =
119                 new ArrayList<HotDeployEvent>(_dependentEvents);
120 
121             for (HotDeployEvent dependentEvent : dependentEvents) {
122                 _doFireDeployEvent(dependentEvent);
123             }
124         }
125         else {
126             if (!_dependentEvents.contains(event)) {
127                 if (_log.isInfoEnabled()) {
128                     _log.info(
129                         "Queue " + event.getServletContextName() +
130                             " for deploy because its dependencies are not " +
131                                 "available");
132                 }
133 
134                 _dependentEvents.add(event);
135             }
136         }
137     }
138 
139     private void _fireDeployEvent(HotDeployEvent event) {
140 
141         // Capture events that are fired before the portal initialized. These
142         // events are later fired by flushPrematureEvents.
143 
144         if (_prematureEvents != null) {
145             _prematureEvents.add(event);
146 
147             return;
148         }
149 
150         // Fire current event
151 
152         _doFireDeployEvent(event);
153     }
154 
155     private void _fireUndeployEvent(HotDeployEvent event) {
156         for (HotDeployListener listener : _listeners) {
157             try {
158                 listener.invokeUndeploy(event);
159             }
160             catch (HotDeployException hde) {
161                 _log.error(hde, hde);
162             }
163         }
164 
165         _deployedServletContextNames.remove(event.getServletContextName());
166     }
167 
168     private void _flushPrematureEvents() {
169         for (HotDeployEvent event : _prematureEvents) {
170             _doFireDeployEvent(event);
171         }
172 
173         _prematureEvents = null;
174     }
175 
176     private void _registerListener(HotDeployListener listener) {
177         _listeners.add(listener);
178     }
179 
180     private void _unregisterListeners() {
181         _listeners.clear();
182     }
183 
184     private static Log _log = LogFactoryUtil.getLog(HotDeployUtil.class);
185 
186     private static HotDeployUtil _instance = new HotDeployUtil();
187 
188     private List<HotDeployEvent> _dependentEvents;
189     private Set<String> _deployedServletContextNames;
190     private List<HotDeployListener> _listeners;
191     private List<HotDeployEvent> _prematureEvents;
192 
193 }