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  import java.util.concurrent.CopyOnWriteArrayList;
33  
34  /**
35   * <a href="HotDeployUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Ivica Cardic
38   * @author Brian Wing Shun Chan
39   */
40  public class HotDeployUtil {
41  
42      public static void fireDeployEvent(HotDeployEvent event) {
43          _instance._fireDeployEvent(event);
44      }
45  
46      public static void fireUndeployEvent(HotDeployEvent event) {
47          _instance._fireUndeployEvent(event);
48      }
49  
50      public static void flushPrematureEvents() {
51          _instance._flushPrematureEvents();
52      }
53  
54      public static void registerListener(HotDeployListener listener) {
55          _instance._registerListener(listener);
56      }
57  
58      public static void unregisterListener(HotDeployListener listener) {
59          _instance._unregisterListener(listener);
60      }
61  
62      public static void unregisterListeners() {
63          _instance._unregisterListeners();
64      }
65  
66      private HotDeployUtil() {
67          if (_log.isInfoEnabled()) {
68              _log.info("Initializing hot deploy manager " + this.hashCode());
69          }
70  
71          _dependentEvents = new ArrayList<HotDeployEvent>();
72          _deployedServletContextNames = new HashSet<String>();
73          _listeners = new CopyOnWriteArrayList<HotDeployListener>();
74          _prematureEvents = new ArrayList<HotDeployEvent>();
75      }
76  
77      private void _doFireDeployEvent(HotDeployEvent event) {
78          if (_deployedServletContextNames.contains(
79                  event.getServletContextName())) {
80  
81              return;
82          }
83  
84          boolean hasDependencies = true;
85  
86          Set<String> dependentServletContextNames =
87              event.getDependentServletContextNames();
88  
89          for (String dependentServletContextName :
90                  dependentServletContextNames) {
91  
92              if (!_deployedServletContextNames.contains(
93                      dependentServletContextName)) {
94  
95                  hasDependencies = false;
96  
97                  break;
98              }
99          }
100 
101         if (hasDependencies) {
102             if (_dependentEvents.contains(event)) {
103                 if (_log.isInfoEnabled()) {
104                     _log.info(
105                         "Deploy " + event.getServletContextName() +
106                             " from queue");
107                 }
108             }
109 
110             for (HotDeployListener listener : _listeners) {
111                 try {
112                     listener.invokeDeploy(event);
113                 }
114                 catch (HotDeployException hde) {
115                     _log.error(hde, hde);
116                 }
117             }
118 
119             _deployedServletContextNames.add(event.getServletContextName());
120 
121             _dependentEvents.remove(event);
122 
123             List<HotDeployEvent> dependentEvents =
124                 new ArrayList<HotDeployEvent>(_dependentEvents);
125 
126             for (HotDeployEvent dependentEvent : dependentEvents) {
127                 _doFireDeployEvent(dependentEvent);
128             }
129         }
130         else {
131             if (!_dependentEvents.contains(event)) {
132                 if (_log.isInfoEnabled()) {
133                     _log.info(
134                         "Queue " + event.getServletContextName() +
135                             " for deploy because its dependencies are not " +
136                                 "available");
137                 }
138 
139                 _dependentEvents.add(event);
140             }
141         }
142     }
143 
144     private void _fireDeployEvent(HotDeployEvent event) {
145 
146         // Capture events that are fired before the portal initialized. These
147         // events are later fired by flushPrematureEvents.
148 
149         if (_prematureEvents != null) {
150             _prematureEvents.add(event);
151 
152             return;
153         }
154 
155         // Fire current event
156 
157         _doFireDeployEvent(event);
158     }
159 
160     private void _fireUndeployEvent(HotDeployEvent event) {
161         for (HotDeployListener listener : _listeners) {
162             try {
163                 listener.invokeUndeploy(event);
164             }
165             catch (HotDeployException hde) {
166                 _log.error(hde, hde);
167             }
168         }
169 
170         _deployedServletContextNames.remove(event.getServletContextName());
171     }
172 
173     private void _flushPrematureEvents() {
174         for (HotDeployEvent event : _prematureEvents) {
175             _doFireDeployEvent(event);
176         }
177 
178         _prematureEvents = null;
179     }
180 
181     private void _registerListener(HotDeployListener listener) {
182         _listeners.add(listener);
183     }
184 
185     private void _unregisterListener(HotDeployListener listener) {
186         _listeners.remove(listener);
187     }
188 
189     private void _unregisterListeners() {
190         _listeners.clear();
191     }
192 
193     private static Log _log = LogFactoryUtil.getLog(HotDeployUtil.class);
194 
195     private static HotDeployUtil _instance = new HotDeployUtil();
196 
197     private List<HotDeployEvent> _dependentEvents;
198     private Set<String> _deployedServletContextNames;
199     private List<HotDeployListener> _listeners;
200     private List<HotDeployEvent> _prematureEvents;
201 
202 }