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.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 registerListener(HotDeployListener listener) {
50          _instance._registerListener(listener);
51      }
52  
53      public static void flushEvents() {
54          _instance._flushEvents();
55      }
56  
57      private HotDeployUtil() {
58          if (_log.isInfoEnabled()) {
59              _log.info("Initializing hot deploy manager " + this.hashCode());
60          }
61  
62          _listeners = new Vector();
63          _events = new Vector();
64      }
65  
66      private synchronized void _fireDeployEvent(HotDeployEvent event) {
67  
68          // Capture events that are fired before the portal initialized. These
69          // events are later fired by flushEvents.
70  
71          if (_events != null) {
72              _events.add(event);
73  
74              return;
75          }
76  
77          // Fire current event
78  
79          Iterator itr = _listeners.iterator();
80  
81          while (itr.hasNext()) {
82              HotDeployListener listener = (HotDeployListener)itr.next();
83  
84              try {
85                  listener.invokeDeploy(event);
86              }
87              catch (HotDeployException hde) {
88                  _log.error(hde, hde);
89              }
90          }
91      }
92  
93      private void _fireUndeployEvent(HotDeployEvent event) {
94          Iterator itr = _listeners.iterator();
95  
96          while (itr.hasNext()) {
97              HotDeployListener listener = (HotDeployListener)itr.next();
98  
99              try {
100                 listener.invokeUndeploy(event);
101             }
102             catch (HotDeployException hde) {
103                 _log.error(hde, hde);
104             }
105         }
106     }
107 
108     private void _registerListener(HotDeployListener listener) {
109         _listeners.add(listener);
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 static Log _log = LogFactoryUtil.getLog(HotDeployUtil.class);
134 
135     private static HotDeployUtil _instance = new HotDeployUtil();
136 
137     private List _listeners;
138     private List _events;
139 
140 }