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.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.Iterator;
29  import java.util.List;
30  import java.util.Vector;
31  
32  /**
33   * <a href="HotDeployUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Ivica Cardic
36   * @author Brian Wing Shun Chan
37   *
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 flushEvents() {
50          _instance._flushEvents();
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          _listeners = new Vector();
67          _events = new Vector();
68      }
69  
70      private synchronized void _fireDeployEvent(HotDeployEvent event) {
71  
72          // Capture events that are fired before the portal initialized. These
73          // events are later fired by flushEvents.
74  
75          if (_events != null) {
76              _events.add(event);
77  
78              return;
79          }
80  
81          // Fire current event
82  
83          Iterator itr = _listeners.iterator();
84  
85          while (itr.hasNext()) {
86              HotDeployListener listener = (HotDeployListener)itr.next();
87  
88              try {
89                  listener.invokeDeploy(event);
90              }
91              catch (HotDeployException hde) {
92                  _log.error(hde, hde);
93              }
94          }
95      }
96  
97      private void _fireUndeployEvent(HotDeployEvent event) {
98          Iterator itr = _listeners.iterator();
99  
100         while (itr.hasNext()) {
101             HotDeployListener listener = (HotDeployListener)itr.next();
102 
103             try {
104                 listener.invokeUndeploy(event);
105             }
106             catch (HotDeployException hde) {
107                 _log.error(hde, hde);
108             }
109         }
110     }
111 
112     private synchronized void _flushEvents() {
113         for (int i = 0; i < _events.size(); i++) {
114             HotDeployEvent event = (HotDeployEvent)_events.get(i);
115 
116             Iterator itr = _listeners.iterator();
117 
118             while (itr.hasNext()) {
119                 HotDeployListener listener = (HotDeployListener)itr.next();
120 
121                 try {
122                     listener.invokeDeploy(event);
123                 }
124                 catch (HotDeployException hde) {
125                     _log.error(hde, hde);
126                 }
127             }
128         }
129 
130         _events = null;
131     }
132 
133     private void _registerListener(HotDeployListener listener) {
134         _listeners.add(listener);
135     }
136 
137     private void _unregisterListeners() {
138         _listeners.clear();
139     }
140 
141     private static Log _log = LogFactoryUtil.getLog(HotDeployUtil.class);
142 
143     private static HotDeployUtil _instance = new HotDeployUtil();
144 
145     private List _listeners;
146     private List _events;
147 
148 }